#!/bin/bash
# /usr/sbin/lsdoomconfig - configuration script for suid bit of lsdoom.
# based upon gnuplotconfig by tibor simko <simko@debian.org>.
# modified for lsdoom by Joe Drew <hoserhead@woot.net>
# note: this script modifies `/etc/lsdoom.conf' configuration file.

set -e
set -C

if [ "root" != "`whoami`" ]
then 
   echo "Sorry, only root can run this script.  Exiting."
   exit 1
fi

echo "Lsdoom (LxDoom for SVGAlib) suid configuration:"
echo

if [ ! -e /etc/lsdoom.conf ] 
then
	old=n
else
	if grep -q "^is_suid=y.*$" /etc/lsdoom.conf; then old=y; else old=n; fi
fi

echo
echo "In order to use svgalib and gain access to the graphics hardware,"
echo "lsdoom must be run as root. If you want any users other than root"
echo "to be able to play the console version of LxDoom, it must be setuid."
echo "However, this poses a significant security risk, as lsdoom could have"
echo "significant flaws or other security defects which have, as yet, gone"
echo "undetected."
echo "If you're not sure, say 'no', install lxdoom-x11 and play lxdoom in X"
echo "only."

while true; do

echo
if [ "$old" = "y" ] 
then 
   echo "  Lsdoom is currently set up as setuid root! Beware!"
else 
   echo "  Lsdoom is not set up as setuid root.  Good."
fi

echo
echo -n "Do you want lsdoom to be setuid root?  (y/n/?) [n] "
read yn
echo
test -n "$yn" || yn="n"
case "$yn" in
   [Nn]*)
          echo "Good choice. Making sure the setuid bit is gone..."
          chmod u-s /usr/games/lsdoom
	  if [ -e /etc/suid.conf -a -x /usr/sbin/suidunregister ]; then
		 echo "Unregistering lsdoom from suidmanager database."
		 suidunregister -s lxdoom-svga /usr/games/lsdoom
          fi
	  echo "Ok, now lsdoom isn't setuid."
	  set +e
	  rm /etc/lsdoom.conf > /dev/null 2>&1
	  echo "is_suid=n" > /etc/lsdoom.conf
	  echo "You can change this configuration at any time by running"
	  echo "/usr/sbin/lsdoomconfig ."
          exit 0
          ;;
   [Yy]*)
            if [ -e /etc/suid.conf -a -x /usr/sbin/suidregister ]; then
               echo "Hmm, you seem to have suidmanager installed.  Will use it."
               suidregister -s lxdoom-svga /usr/games/lsdoom root root 4755
               echo "Okay, lsdoom is now set up and registered as setuid root."
            else
               echo "Hmm, you don't seem to have suidmanager installed."
               echo "Please consider installing suidmanager in the future."
               chown root.root /usr/games/lsdoom
               chmod 4755 /usr/games/lsdoom
               echo "Okay, lsdoom is now manually set up as setuid root."
            fi
	    set +e
	    rm /etc/lsdoom.conf >/dev/null 2>&1
	    echo "is_suid=y" > /etc/lsdoom.conf
	    echo "You can change this configuration at any time by running"
	    echo "/usr/sbin/lsdoomconfig ."
            exit 0
          ;;
esac

done
