#
# The following is a script to set up local apps support on LTSP through LDM
#
# The presumption is that the environment is set up such that NSS will look
# at extrafiles, ie. additional local files for passwd and group beyond
# those found in /etc.
#
# This will enable us to easily bypass the need for setting up local user
# authentication, and instead leverage the authentication already set up on
# the server.

if boolean_is_true "$LOCAL_APPS"; then

    # Set up local uids/gids

    LOCALAPPS_CACHE=/var/cache/ltsp-localapps
    mkdir -p ${LOCALAPPS_CACHE} 2>/dev/null

    # Cleanup 
    LOCALAPPSD_PIDFILE=/var/run/ltsp/ltsp-localappsd
    [ -r "$LOCALAPPSD_PIDFILE" ] && LOCALAPPSD_PID=$(cat $LOCALAPPSD_PIDFILE)
    # Kill PID
    if [ -n "$LOCALAPPSD_PID" ]; then
        pkill -P $LOCALAPPSD_PID
        rm $LOCALAPPSD_PIDFILE
    fi

    # Copy /etc/passwd and /etc/group to cache if it does not exist (should only happen on first login)
    for i in passwd group; do
        if [ ! -e "${LOCALAPPS_CACHE}/${i}" ]; then
            cp /etc/${i} "${LOCALAPPS_CACHE}/${i}"
        else
            cp "${LOCALAPPS_CACHE}/${i}" /etc/${i}
        fi
    done

    # Get logged in username if not set
    [ -z "$LDM_USERNAME" ] && LDM_USERNAME=$(ssh -S ${LDM_SOCKET} ${LDM_SERVER} 'echo ${USER}')

    # Get passwd info *just* for that user
    ssh -S ${LDM_SOCKET} ${LDM_SERVER} "/usr/bin/getent passwd ${LDM_USERNAME}" >>/etc/passwd

    # Get all group info and copy to TMPGROUP
    ssh -S ${LDM_SOCKET} ${LDM_SERVER} "/usr/bin/getent group" >>/etc/group

    # Now, some groups may have different gids on the server than the client chroot
    # So, let's prune out all the dups
    TMPGROUP="${LOCALAPPS_CACHE}/tmpgroup"
    gnames=""
    while read line; do
        gname=$(echo $line|cut -d: -f1)
        match=
        for e in $gnames; do
            if [ "$gname" = "$e" ]; then
                match=1
            fi
        done
        if [ -z "$match" ]; then
            echo "$line" >>${TMPGROUP}
            gnames="$gnames $gname"
        fi
    done </etc/group
    mv ${TMPGROUP} /etc/group
    chmod 644 /etc/group

    # Get the system groups that the user belongs to, so we can add him back in
    myGroups=$(ssh -S ${LDM_SOCKET} ${LDM_SERVER} /usr/bin/groups | tr ' ' ',')
    if [ -n "$myGroups" ]; then
        usermod -G "$myGroups" "${LDM_USERNAME}"
    fi

    # Now, let's mount the home directory

    # First, make the mountpoint
    LDM_HOME=$(getent passwd ${LDM_USERNAME}|cut -d: -f6)
    mkdir -p ${LDM_HOME}
    chown ${LDM_USERNAME}.${LDM_USERNAME} ${LDM_HOME}

    ## Maybe do this:
    ## export HOME=${LOCALAPPS_CACHE}

    # Mount the home directory
    sshfs -o allow_other,ControlPath=${LDM_SOCKET} ${LDM_SERVER}:${LDM_HOME} ${LDM_HOME}

    #Launch the ltsp-localappsd to handle the apps
    (
    echo $! > $LOCALAPPSD_PIDFILE

    # if cups is installed in the chroot, use LDM_SERVER for printing
    [ -d "/etc/cups" ] && echo "ServerName ${LDM_SERVER}" > /etc/cups/client.conf

    ltsp-localappsd

    # Clean up localapps menu
    if boolean_is_true "$LOCAL_APPS_MENU"; then
        [ -x "/usr/bin/ltsp-genmenu" ] && su - ${LDM_USERNAME} -c "/usr/bin/ltsp-genmenu remove"
    fi

    # Clean up cups config
    [ -r "/etc/cups/client.conf" ] && rm -f /etc/cups/client.conf

    # Copy back passwd and group
    for i in passwd group; do
        cp "${LOCALAPPS_CACHE}/${i}" /etc/${i}
    done
    
    # Unmount sshfs and remove the mount dir
    fusermount -uqz ${LDM_HOME}
    rmdir ${LDM_HOME}
    rm $LOCALAPPSD_PIDFILE
    )&

fi
