umask 002
root=$(r -e 'suppressMessages(library(cran2deb));cat(system.file(package="cran2deb"),file=stdout())')
cmd=$1
-shift
+
+CRAN2DEB_CACHE=/var/cache/cran2deb
+CRAN2DEB_CONFDIR=/etc/cran2deb
+if [ -r /etc/cran2deb/config ]; then
+ . /etc/cran2deb/config;
+fi;
+if [ -r ~/.cran2deb/config ]; then
+ CRAN2DEB_CONFDIR=~/.cran2deb
+ . ~/.cran2deb/config;
+fi;
+export CRAN2DEB_CACHE CRAN2DEB_CONFDIR
+
+
+if [ "x" = "x$cmd" ]; then
+ cmd="help"
+fi
+
if [ ! -x "$root/exec/$cmd" ]; then
echo unknown command $cmd
exit 1
fi
-"$root/exec/$cmd" "$root" $*
+
+shift
+
+# there's no reason not to run cran2deb as a local user
+# precmd=""
+# if [ "$(id -un)" != c2d ]; then
+# precmd="sudo -u c2d -E"
+# fi
+
+if [ "web" = "$cmd" ]; then
+ # web uses getopt and would be irritated by the "$root" argument
+ $precmd "$root/exec/$cmd" $*
+else
+ $precmd "$root/exec/$cmd" "$root" $*
+fi