#!/bin/sh
#
# The following script works for LTSP5.
#
# This software is licensed under the Gnu General Public License.
# The full text of which can be found at http://www.LTSP.org/license.txt
#
#
#       To customize the kiosk session, you can add "homedir" files
#       to $chroot/usr/local/share/ltspkiosk/home/ 
#       AND to add startup scripts that run as the user, you can add 
#       them as executables or symlinks to executables in:
#       $chroot/usr/local/share/ltspkiosk/startup

PATH=/bin:$PATH; export PATH
. /usr/share/ltsp/screen-x-common

[ -n "$1" ] && KIOSK_EXE=$1
[ -n "$2" ] && KIOSK_OPTIONS=$2

if [ -z "${KIOSK_EXE}" ]; then
    if [ -x "/usr/bin/firefox" ]; then
        KIOSK_EXE=/usr/bin/firefox
    elif [ -x "/usr/bin/google-chrome" ]; then
        KIOSK_EXE=/usr/bin/google-chrome
    elif [ -x "/usr/bin/opera" ]; then
        KIOSK_EXE=/usr/bin/opera
    else
        KIOSK_EXE=unknown
    fi
fi
 
if boolean_is_true "${KIOSK_DAEMON:-"False"}"; then
    export XINITRC_DAEMON="True"
fi

if [ -x /usr/share/ltsp/xinitrc ]; then
    xinitrc=/usr/share/ltsp/xinitrc
fi

KIOSKUSER=${KIOSKUSER:-"ltspkiosk"}
if [ -z "$(getent passwd ${KIOSKUSER})" ]; then
    # create a ltspkiosk user
    adduser --no-create-home --disabled-password --gecos ,,, ${KIOSKUSER}
fi

# Create a tmpdir to be our homedir
TMPDIR=$(mktemp -d /tmp/.kiosk-XXXXXX)
chown ${KIOSKUSER} ${TMPDIR}

# Edit passwd homedir entry for programs that look it up from there
sed -i -e '\|'${KIOSKUSER}'|s|[^:]*\(:[^:]*\)$|'$TMPDIR'\1|' /etc/passwd

su - ${KIOSKUSER} -c "XINITRC_DAEMON=${XINITRC_DAEMON} KIOSK_WM=${KIOSK_WM} xinit $xinitrc /usr/share/ltsp/kioskSession ${KIOSK_EXE} ${KIOSK_OPTIONS} -- ${DISPLAY} vt${TTY} ${X_ARGS} -br" >/dev/null

rm -rf ${TMPDIR}
