#!/bin/sh
#-------------------------#
# major SaX start command #
# all errors are reported #
# in ~/StartLog           #
#-------------------------#

function TestLink {
 LINK_SET="/usr/X11R6/bin/xinit"
 POINT_TO=`readlink $LINK_SET 2>/dev/null`

 if [ ! -z $POINT_TO ];then
  NAME=`basename $POINT_TO`

  if [ $NAME = "xinit4" ];then
   #echo "4.0..."
   echo /.../
   echo -e "Your system is configured for \033[32mXFree86 version 4\033[m\017."
   echo "SaX is only suitable for the (re)configuration of XFree86 version 3."
   echo "For (re)configuration of XFree86 version 4, please use:"
   echo 
   echo "     /usr/X11R6/bin/sax2"
   echo 
   echo "Do you want to continue with the" 
   echo -e "\033[32mXFree86 version 3\033[m\017 setup now ?"
   echo 
   echo -n "[ yes | <no> ]  "
   read answer

   if [ -z "$answer" ];then
    answer="no"
   fi

   if [ "$answer" = "yes" ]; then
    /usr/X11R6/bin/switch2xf86-3x
    return
   else
    exit 0
   fi
  fi
 fi
}

if [ "$1" = --help ];then
 echo "usage: SaX [ options ]"
 echo "options:"
 echo "   [ -m | --memory < Memory, extension [k,K|m,M|g,G] > ]"
 echo "   [ -r | --ramdac < Ramdac Speed > ]"
 echo "   [ -s | --server < X Server > ]"
 echo "   [ -d | --device < Mouse Device > ]"
 echo "     If Mouse is not avaliable use -d /dev/null"
 echo "   [ -p | --prot  < Mouse Protocol > ]"
 echo "   [ -h | --hsync < Hsync range > ]"
 echo "   [ -v | --vsync < Vsync range > ]"
 echo "   [ -l | --lang  < Language > ]" 
 echo "     possible languages:"
 echo "      < --lang  de    -> German     >"
 echo "      < --lang  en    -> English    >"
 echo "      < --lang  es    -> Spanish    >"
 echo "      < --lang  fr    -> French     >"
 echo "      < --lang  el    -> Greek      >"
 echo "      < --lang  id    -> Indonesia  >"
 echo "      < --lang  it    -> Italian    >"
 echo "      < --lang  hu    -> Hungarian  >"
 echo "      < --lang  nl    -> Dutch      >"
 echo "      < --lang  pl    -> Polish     >"
 echo "      < --lang  pt    -> Portuguese >"
 echo "      < --lang  pt_BR -> Brazilian  >"
 echo "      < --lang  br    -> Breton     >" 
 echo "      < --lang  ro    -> Romanian   >"
 echo "      < --lang  ru    -> Russian    >"
 echo "      < --lang  sk    -> Slovak     >"          
 echo "      < --lang  ca    -> Catalan    >"
 echo "      < --lang  cs    -> Czech      >"
 echo "      < --lang  ja    -> Japanese   >"
 echo "      < --lang  ko    -> Korean     >"
 echo "   [ -w | --wm    < Set Windowmanager, none for switch off > ]"
 echo "     Note: Usage only for fvwm based windowmanagers !"
 echo "   [ -a | --auto  < Resolution > ]"
 echo "   [ -o | --out   < off > ]"
 echo "     Oppress any terminal output"
 echo "   [ -g | --gpm   < on | off > ]"
 echo "     If you want to use gpm as repeater use -g on"
 echo "     the default value is off"
 echo "   [ -C | --chipset < set the chipset > ]"
 echo "   [ -O | --options < set a comma separated option list > ]" 
 echo "   [ -f | --force   < startup > ]"
 echo "     this will force the execution of SaX on a still" 
 echo "     running X-Server. Note: If there is no Server up"
 echo "     the SaX startup will fail"
 echo 
 echo "     Note: Auto configuration is still experimental" 
 echo "   [ -M | --model   < XkbModel  > ]"
 echo "   [ -L | --layout  < XkbLayout > ]"
 echo "   [ -R | --rules   < XkbRules  > ]"
 exit 0
fi

TestLink

if [ -z "$DISPLAY" ];then
 export DISPLAY=:0
fi

if [ ! "$UID" = 0 ];then
 echo -n "sax: root "
 sux -c "/usr/X11R6/bin/SaX $*"
 if [ ! "$?" = 0 ];then 
  echo
  exit 1
 fi
 exit 0
fi

#-----------#
# paths     #
#-----------#
SAX=/var/X11R6/sax/bin/SaX.tcl
LOG=~root/StartLog

rm -rf /tmp/sax          2>/dev/null
rm -rf /tmp/.sax_dir     2>/dev/null

rm -f $LOG               2>/dev/null
touch $LOG               2>/dev/null
chmod a+w $LOG           2>/dev/null

$SAX $* 2>> $LOG

RETCODE=`echo $?`

if [ ! "$RETCODE" = "0" ];then
 echo "sax: an error occured"
 echo "sax: for more information please look at:"
 echo "sax: ~/StartLog and ~/ServerLog"
fi
exit $RETCODE
