#!/bin/sh

# $0 - this command
# $1 - user name (assume to be part of the /home/${USERNAME}
# $2 - admin status ("inst_only" - exo_user, "admin" - full admin)

if [ $# -lt 2 ]; then
    echo "Missing parameters"
    echo "Usage: $0 <user_name> <admin_status>"
    echo "   admin_status: inst_only - exo_user"
    echo "                 admin     - full admin/support user"
    return 1
fi

USER_NAME="$1"
ADMIN_STAT="$2"

INSTALL_PATH=/usr/lib/demeter_evprep/
ASSET_PATH=${INSTALL_PATH}/data/flutter_assets/resources
HOME_PATH=/home/${USER_NAME}
EXT_DEFAULT_PATH=/usr/local/share/gnome-extensions/extensions.tar.gz
EXT_PATH=${HOME_PATH}/.local/share/gnome-shell/extensions
CONF_PATH=${HOME_PATH}/.config
EXO_PATH=${HOME_PATH}/ExoPrep
PASSWD='exoUser'
if [ "${ADMIN_STAT}" = "admin" ]; then
    PASSWD='exoSupport3301'
fi

# Normal ExoUser does not have sudo but provision is made for OTA upgrades
has_user=$(grep '${USER_NAME}' /etc/passwd || true)
if [ "${has_user}" = "" ]; then
    PASS=$(openssl passwd -6 "${PASSWD}")
    groupadd ${USER_NAME}
    useradd ${USER_NAME} \
        -p ${PASS} \
        -g ${USER_NAME} \
        -m \
        -s /bin/bash \
        --home-dir /home/${USER_NAME}
fi

# always force permission updates
usermod -aG dialout,netdev,audio,disk,video,cdrom,input,plugdev,tty,lp ${USER_NAME}
chsh -s /bin/bash ${USER_NAME}

case "${ADMIN_STAT}" in
    "inst_only")
        cat > "/etc/sudoers.d/allow-${USER_NAME}-inst-priv" <<EOF
${USER_NAME} ALL = (ALL) NOPASSWD: /usr/bin/dpkg, /usr/bin/apt-get, /usr/bin/apt, /usr/bin/systemctl
EOF
        ext_app_list=""
        ;;

    "admin")
        usermod -aG sudo ${USER_NAME}
        ext_app_list=" , 'org.gnome.Terminal.desktop' "
        ;;
    *)
        # else nothing
        ;;
esac


mkdir -p ${EXO_PATH}

# don't override the config file here if it exists
if [ ! -f ${EXO_PATH}/exoPrepGUI.config  ]; then
    cp ${ASSET_PATH}/text/exoPrepGUI.config ${EXO_PATH}
fi

# always force reinstall some of the components:
FORCE_UPDATE_LIST=" audio methods "
for the_path in ${FORCE_UPDATE_LIST}; do
    mkdir -p ${EXO_PATH}/${the_path}
    cp -f ${ASSET_PATH}/${the_path}/* ${EXO_PATH}/${the_path}/
done

# always force reinstall autostart programs
mkdir -p ${CONF_PATH}/autostart
ln -sf /usr/share/applications/Scale1p5.desktop ${CONF_PATH}/autostart/50-scale-screen.desktop
ln -sf /usr/share/applications/sleep-inhibitor.desktop ${CONF_PATH}/autostart/55-sleep-inhibitor.desktop
ln -sf /usr/share/applications/demeter_evprep.desktop ${CONF_PATH}/autostart/60-demeter_evprep.desktop

# bypass welcome screen
echo "yes" > ${CONF_PATH}/gnome-initial-setup-done

chown ${USER_NAME}:${USER_NAME} ${HOME_PATH}
chown -R ${USER_NAME}:${USER_NAME} ${CONF_PATH}
chown -R ${USER_NAME}:${USER_NAME} ${EXO_PATH}


## setup the Overview Dash "dock" with our application list

TMP_PATH=/tmp/setup-${USER_NAME}-gnome/favorites.dconf
TMP_FILE=${TMP_PATH}/favorites.dconf

# keeps this as root, no need to change owner on this file
mkdir -p ${TMP_PATH}
# yes gnome wants all this one a single line...
cat > ${TMP_FILE} <<EOF
[org/gnome/shell]
favorite-apps=[ 'org.gnome.Settings.desktop', 'org.gnome.Extensions.desktop', 'org.gnome.Nautilus.desktop', 'firefox-esr.desktop', 'demeter_evprep.desktop' ${ext_app_list} ]
EOF
sudo -u ${USER_NAME} dbus-run-session dconf load / < ${TMP_FILE}


## unpack extensions & enable them
mkdir -p ${EXT_PATH}
tar xf ${EXT_DEFAULT_PATH} -C ${EXT_PATH}
# fix permission
chown -R ${USER_NAME}:${USER_NAME} ${HOME_PATH}/.local

sudo -u ${USER_NAME} dbus-run-session dconf write \
    /org/gnome/shell/enabled-extensions \
    "['no-overview@fthx', 'improvedosk@nick-shmyrev.dev', 'appindicatorsupport@rgcjonas.gmail.com']"

