#!/bin/bash
# script for live-settings

set -e

if [ -f /etc/default/distro ]; then
    . /etc/default/distro
fi

all_users_common_settings() {
    # we run all users settings
    if [ -f /usr/share/tuxedo-common-livesettings/all-users-settings ] ;then
        /usr/share/tuxedo-common-livesettings/all-users-settings
    fi
}

gen_adduser_conf() {
    sed -i 's/^DIR_MODE=.*/DIR_MODE=0700/' /etc/adduser.conf

    unset GROUPS
    for g in ${FLL_LIVE_USER_GROUPS}; do
        if getent group ${g} >/dev/null; then
            GROUPS="${GROUPS} ${g}"
        fi
    done

    sed -i -e 's/^#\?\(EXTRA_GROUPS=\).*/\1"'"${GROUPS# }"'"/' \
           -e 's/^#\?\(ADD_EXTRA_GROUPS=\).*/\11/' \
            /etc/adduser.conf
}

add_live_user() {
    adduser --gecos ${FLL_LIVE_USER} ${FLL_LIVE_USER} \
<< EOF
tux
tux
EOF
    passwd -d ${FLL_LIVE_USER}
}

user_home_common_settings() {
    if [ -f /usr/share/tuxedo-common-livesettings/user-home-settings ]; then
        su "${FLL_LIVE_USER}" -c /usr/share/tuxedo-common-livesettings/user-home-settings
    fi
}

hack_bashrc() {
    grep -s -q 'alias su' /home/${FLL_LIVE_USER}/.bashrc && return 0
    printf "\nalias su='sudo su'\n" \
        >> /home/${FLL_LIVE_USER}/.bashrc
    chown ${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/.bashrc
}

hack_sddmservice() {
    if [ /lib/systemd/system/sddm.service ]; then
       sed -i '/ExecStartPre/d' /lib/systemd/system/sddm.service
       systemctl daemon-reload
    fi
}

hack_systemd() {
    sed -i 's/#DefaultTimeoutStopSec\=..s/DefaultTimeoutStopSec\=10s/g' /etc/systemd/user.conf
    sed -i 's/#DefaultTimeoutStopSec\=..s/DefaultTimeoutStopSec\=10s/g' /etc/systemd/system.conf
    systemctl daemon-reload
}

setup_calamares() {
    mkdir -p /home/${FLL_LIVE_USER}/Desktop
    if [ -e /usr/share/tuxedo/calamares.desktop ]; then
        cp -f /usr/share/tuxedo/calamares.desktop /home/${FLL_LIVE_USER}/Desktop
        chmod +x /home/${FLL_LIVE_USER}/Desktop/calamares.desktop
    fi
}

setup_repair() {
    mkdir -p /home/${FLL_LIVE_USER}/Desktop
    if [ -e /usr/share/tuxedo/automated_repair.desktop ]; then
        cp -f /usr/share/tuxedo/automated_repair.desktop /home/${FLL_LIVE_USER}/Desktop
        chmod +x /home/${FLL_LIVE_USER}/Desktop/automated_repair.desktop
        cp -f /usr/share/tuxedo/chroot-to-install.desktop /home/${FLL_LIVE_USER}/Desktop
        chmod +x /home/${FLL_LIVE_USER}/Desktop/chroot-to-install.desktop
    fi
}

fix_keyboard() {
    mkdir -p /home/${FLL_LIVE_USER}/.config/autostart
    chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/.config/autostart
    #cp -f /usr/share/tuxedo-common-livesettings/keyboardfix.sh /home/${FLL_LIVE_USER}/
    #chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/keyboardfix.sh
    #cp -f /usr/share/tuxedo-common-livesettings/keyboardfix.sh.desktop /home/${FLL_LIVE_USER}/.config/autostart/
    #chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/keyboardfix.sh
    #chmod +x /home/${FLL_LIVE_USER}/.config/autostart/keyboardfix.sh.desktop
    #chmod +x /home/${FLL_LIVE_USER}/keyboardfix.sh
    #cp -f /usr/share/tuxedo/firstrun.desktop /home/${FLL_LIVE_USER}/.config/autostart/
    #cp -af /usr/share/tuxedo/kdeglobals-oem /home/${FLL_LIVE_USER}/.config/kdeglobals
    #cp -af /usr/share/tuxedo/kglobalshortcutsrc-oem /home/${FLL_LIVE_USER}/.config/kglobalshortcutsrc
    #chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/.config/kdeglobals
    #chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/.config/kglobalshortcutsrc
    #chown ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}/.config/autostart/firstrun.desktop
    #chmod +x /home/${FLL_LIVE_USER}/.config/autostart/firstrun.desktop
}

chown_user_dir() {
    # central point setting the ownership of users home
    chown -R ${FLL_LIVE_USER}.${FLL_LIVE_USER} /home/${FLL_LIVE_USER}
}


case "$1" in
    configure)
    all_users_common_settings
    if [ -d /home/${FLL_LIVE_USER} ]; then
        echo "                                     "
        echo "*************************************"
        echo "                                     "
        echo " Something went terrible wrong:      "
        echo "  /home/$FLL_LIVE_USER exist!        "
        echo " That should not be, please check    "
        echo " involved packages and pyfll.        "
        echo " This dir must not be created by     "
        echo " somehow or somewhat - only adduser  "
        echo " should do so.                       "
        echo "                                     "
        echo "*************************************"
        echo "                                     "
        exit 1
    fi
    gen_adduser_conf
    add_live_user
    #setup_calamares
    #setup_repair
    hack_sddmservice
    hack_systemd
    hack_bashrc
    user_home_common_settings
    fix_keyboard
    chown_user_dir
    ;;
    *)
        echo "livesettings  called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac


exit 0
