#!/bin/sh

# Copyright (C) 2008 José L. Redrejo Rodríguez, jredrejo at-no-spam debian.org.
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
#for this to work, previously, on the ltsp chroot a ltsp user
#should have been created using:
# "useradd ltsp -d /var/tmp/ltsp -g fuse -u 65533"
# Also, very imporant, sshfs package has to be version >=2.0-1
# with Debian #430225 fixed, otherwise this won't work

set -e
if [ -f /usr/share/ltsp/ltsp-common-functions ]; then
    . /usr/share/ltsp/ltsp-common-functions
else
    # backwards compatibility for older ltsp versions
    . /usr/share/ldm/ltsp-common-functions
fi

rm -Rf /var/tmp/ltsp

LOGIN=`ps -AF|grep ssh|grep @|cut -f1 -d@|rev|cut -f1 -d" "|rev`
FUSEGID=`grep fuse /etc/group|cut -f3 -d:`
if [ -z "$LOGIN" ]; then
	exit 1
fi
echo $LOGIN>/var/run/ltsplogin

#Home for the local ltsp user:
mkdir -p /var/tmp/${LOGIN} 
chown ltsp:fuse /var/tmp/${LOGIN} 
ln -sf /var/tmp/${LOGIN} /var/tmp/ltsp

#here comes the magic:
sshfs -o nonempty,uid=65533,gid=${FUSEGID},allow_other,ControlPath=${LDM_SOCKET}   ${LDM_SERVER}: /var/tmp/${LOGIN}

/usr/bin/localapp &


