#!/bin/sh

# This script first starts faked (the deamon), and then it will run
# the requested programme with fake root privileges.

unset FAKEROOTKEY
KEY_PID=`faked`
FAKEROOTKEY=`echo $KEY_PID|cut -d: -f1`
PID=`echo $KEY_PID|cut -d: -f2`

if test -z "$FAKEROOTKEY" -o -z "$PID"; then
  echo Error while starting faked.
  exit 1
fi

LD_PRELOAD=/usr/lib/libtricks/libtricks.so.0

# Setting the following VPATH will cause libtricks to search
# binaries first in /usr/lib/libtricks/bin, and _then_ in /usr/bin.
# (same for /usr/sbin, /bin, and /sbin).
# This is used for `su' (it's setuid root, so LD_PRELOAD doesn't wrok
# for that binary), and for dpkg-shlibdeps, as we don't want the
# libtricks dependancy to be reported for every binary.
#
# The 'needexec' means that this trick is used _only_ by exec() and friends,
# so if you do `cat /usr/bin/su', you'll see the real /usr/bin
# binary, but if you run the binary, you'll get the one from libtricks.
# (see /usr/doc/libtricks/README for more info):
VPATH='needexec&chop("/(|usr/)(|s)bin/")=:/usr/lib/libtricks/bin/:/bin'

if test -z "$*"; then
  FAKEROOTKEY=$FAKEROOTKEY LD_PRELOAD=$LD_PRELOAD VPATH=$VPATH \
    ${SHELL:-/bin/sh}
else
  FAKEROOTKEY=$FAKEROOTKEY LD_PRELOAD=$LD_PRELOAD VPATH=$VPATH \
    "$@"
fi

kill -SIGTERM $PID