#!/bin/sh
# Copyright (c) 2000 SuSE GmbH Nuernberg, Germany.  All rights reserved.
#
# Author: Marcus Schaefer <sax@suse.de>, 2000
#
# SaX (sax) configuration level 3
#
# CVS ID:
# --------
# Status: Up-to-date
#
trap "signal" EXIT HUP INT TERM PIPE QUIT ABRT SEGV

#========================================
# Functions
#----------------------------------------
function signal {
	killall dots.pl 2>/dev/null >/dev/null
	test -n "$TMPFILE" && rm -f $TMPFILE
}
function isTTY {
	case `tty` in
		/dev/tty*)
			return 0
		;;
		*)
			return 1
		;;
    esac
}
#========================================
# Init
#----------------------------------------
ERR="/var/log/SaX.log"
SHC="/bin/sh"
SUX="sux"
REI=""

#========================================
# Main
#----------------------------------------
for param;do
	if [ $param = "-v" ] || [ $param = "--version" ];then
		/usr/sbin/sax.sh -v
		exit 0
	fi
	if [ $param = "-h" ] || [ $param = "--help" ];then
		/usr/sbin/sax.sh -h
		exit 0
	fi
	if [ $param = "-H" ] || [ $param = "--cmdhelp" ];then
		/usr/sbin/sax.sh --cmdhelp
		exit 0
	fi
done

#========================================
# Check console mode
#----------------------------------------
if isTTY;then
	# echo "SaX: running in console mode, re-init cache"
	REI="-r"
fi

#========================================
# lookup tmp file for language info...
#----------------------------------------
if [ ! "$UID" = 0 ];then
	TMPFILE=`mktemp -q /tmp/sax_lenv.XXXXXX`
	if [ $? -ne 0 ]; then
	echo "SaX: Can't create tmp file, exiting..."
	exit 1
	fi 
fi

#========================================
# test for root privileges...
#----------------------------------------
if [ ! "$UID" = 0 ];then
	#========================================
	# export LANG user environment...
	#----------------------------------------
	echo $LANG >> $TMPFILE

	echo -n "SaX: root "
	if [ -f "$SUX" ];then
		sux -c "/usr/sbin/SaX2 $*"
	else
		xhost + localhost > /dev/null 2>/dev/null
		su - -c "/usr/sbin/SaX2 $*"
	fi

	if [ ! "$?" = 0 ];then
	echo
	if [ ! -f "$SUX" ];then
		xhost - localhost 2>/dev/null >/dev/null
	fi
	exit 1
	fi

	if [ ! -f "$SUX" ];then
		xhost - localhost 2>/dev/null >/dev/null
	fi
	exit 0
fi

#========================================
# run the main sax start script...
#----------------------------------------
/usr/sbin/sax.sh $* $REI
