#!/bin/sh
# Chroots to Debian and runs a shell, or any command-line as non-root.

# By Alan M Bruce (qole), Benson Mitchell and Thomas Perl
#
# GPL licensed; keep code free!

if [ "`whoami`" = "root" ] ; then
  echo "please don't run me as root!"
  exit 9
fi

#Pull in the config, if possible...
[ -f /home/user/.chroot ] && . /home/user/.chroot

#This comes from the config file... If not, fall back on 'user'
#[ "x$DEBUSER" != x ] || DEBUSER=user
DEBUSER=$USER

CHROOTUSER=$DEBUSER

#If display's not given, go to primary display. If it's set, let's not
#mangle it to allow convenient use of USB VGA out, or multiple Xomaps.
[ "x$DISPLAY" != x ] || export DISPLAY=:0.0

#This enables long-hold-to-right-click in all GTK+ apps.
#Maybe we'd be better to add libgtkstylus.so anyway, but for now, we'll
#just leave it if they've got any non-empty GTK_MODULES 
[ "x$GTK_MODULES" != x ] || export GTK_MODULES=libgtkstylus.so

#Note use of su below, so no Debian-side script req'd to drop privileges.
#With no args, use a shell; su will get the right one, with no -c
#With args, just run them as $DEBUSER.

if [ $# = 0 ] ; then
  echo Starting Debian shell... >/dev/stderr
  sudo /sbin/debian su $DEBUSER
else
  # Workaround to allow "debbie" to be used as login shell
  if [ "$1" == "-c" ]; then
    shift
  fi
  echo Starting Debian $* >/dev/stderr
  sudo /sbin/debian su $DEBUSER -c "$*"
fi
