#!/bin/sh
#
# Ssfs users SHell - Chroot user into the virtual disk on login. This tool must
# be installed on the server and in the vdisk chroot, it is executed when logging
# in and when chrooting.
#
# Copyright (C) SliTaz GNU/Linux - BSD License
# Author: Christophe Lincoln <pankso@slitaz.org>
#

# Handle rsync first, user is chrooted after.
case "$@" in                                        
	*rsync*) exec /bin/sh "$@" ;;
	*help) echo "Usage: $(basename $0) [args]" && exit 0 ;;
esac

# Ssfs server config doesn't exist in chroot.
if [ -f /etc/ssfs/ssfs-server.conf ]; then
	. /etc/ssfs/ssfs-server.conf
	root=$SSFS_CHROOT
	# Make sure it's a valid Ssfs user.
	[ -d "$root/home/$USER" ] || exit 0
	exec chroot $root /bin/ssfs-sh "$@"
else
	# Chroot will drop user into /, so set new HOME and cd. Set also
	# some env variables but don't source any profile.
	clear && echo -e \
		"\n\033[1m  Welcome to the Ssfs SHell $USER\033[0m\n"
	PS1='\u@ssfs\e[0m:\e[1;34m\w\e[0m\$ '
	PATH='/bin'
	HOME=/home/$USER
	SYNC=$HOME/Sync
	SHELL=/bin/sh
	export PS1 PATH HOME SYNC SHELL
	cd $HOME
	exec /bin/sh "$@"
fi
