#general initialisations.
#set -x
export version="V0.9"

#somebody reported he had to unset this for some stuff to work.
export -n POSIXLY_CORRECT

#some linux distributions (redhat) set $ENV to ~/.bashrc, this could
#break subshells started by tbackup if the user places specific stuff
#in .bashrc.  Then again, unsetting it could also break tbackup under
#other configurations.
export -n ENV

#add own mtools to path
export PATH=$bupdir/mtools205:$PATH

# add /auxbin to path if it exists.
if [ -e /auxbin ]; then
 export PATH=/auxbin:$PATH
fi

# running as root?
if [ $UID != 0 ]; then 
 echo "$0 should be run by root!!"
 exit 1
fi

#The SIGPIPE handler is set to SIG_IGN in some environments, this
#breaks some shell code.  Reset the handler.
trap exit SIGPIPE

export userwd=`pwd`

export batchmode=n

#switch on batch mode if no tty found.
if ! tty -s; then
  echo
  echo " [t$ttytitle is now running in batch mode.]"
  echo
  batchmode=y
fi

#routines for getting parameters
source paramrouts

# make temp dirs.
source makedirs

unset logpars_done
unset only_params

cd /tmp/tbup

export rmbulkdir='echo -n'
export rmupdates='echo -n'
export resetfloppy='echo -n'

#provide a clean abort.

function aborthandler
{
 trap "" 2
 echo
 echo -n "Aborting..."
 cd /tmp
 trap "" 15
 kill 0
 wait
 rm -rf /tmp/tbup
 eval $rmbulkdir
 eval $rmupdates
 eval $resetfloppy
 echo
 exit 1
}

#trap used when ^C pressed and when not running in batch mode.
trap aborthandler 2

#Kills anyone in my process group exept childs of process 1, i.e. the
#atrun command, and the parent shell, i.e. (we hope) the shel invoked
#by atrun.
#If we just kill everyone, the atrun command is killed before it can 
#mail the output with the error message.
#This horrible mess is due to the setpgid call being useless in shell
#programming.  Ugh.
function batchabort
{
 echo
 echo -n "Aborting..."
 cd /tmp
 pgid=`ps -axj|gawk '$2==p { printf $3 }' p=$$ - `
 victems=`ps -axj|gawk \
   '$3==pg { if(($1!=1)&&($2!=par)) printf $2" " }' pg=$pgid par=$PPID - `
 trap "" 15
 kill $victems 2>/dev/null
 set +x
 wait
 rm -rf /tmp/tbup
 eval $rmbulkdir
 eval $rmupdates
 eval $resetfloppy
 echo
 exit 1
}
export batchabort

#set correct signal handlers and abort command.
if [ $batchmode = n ]; then
 function doabort
  {  kill -2 0 }
else
 function doabort
  { batchabort }
fi
export doabort
oldbatchmode=$batchmode


# get parameters

#reset params file.
cat /dev/null >allpars

#get configuration parameters.
if [ -f /etc/tbackup/Config ]; then
 source /etc/tbackup/Config
else
 echo Error: configuration script /etc/tbackup/Config not found.
 echo aborting....
 cd /tmp
 rm -r /tmp/tbup
 exit 1
fi


#read defaults
source $bupbin/t$ttytitle.dea
if [ -f /etc/tbackup/t$ttytitle.dea ]; then
 source /etc/tbackup/t$ttytitle.dea
fi


#execute arguments, these may control the further execution
#by setting dea_,pre_, and checkpre_  parameters.

while [ "$*" != "" ]; do
 com="$1"
#strip off leading - if present.
 case $1 in -* ) 
  com=`echo $com | gawk '{ print substr($0,2) }'`
 esac
 shift
 if [ -f /etc/tbackup/$com\.arg ]; then
  source /etc/tbackup/$com\.arg
 else
  if [ -f $bupbin/$com\.arg ]; then
   source $bupbin/$com\.arg
  else
   echo Error: script $com\.arg not found in /etc/tbackup!
   echo aborting....
   cd /tmp
   rm -r /tmp/tbup
   exit 1
  fi
 fi
done

#set correct signal handlers and abort command if batchmode state changed.
if [ $batchmode != $oldbatchmode ]; then
 if [ $batchmode = n ]; then
  function doabort
   { kill -2 0 }
 else
  function doabort
   { batchabort }
 fi
 export doabort
fi

#running on virtual console? (if n, make an xterm for progress display).
if [ $progressxwindow = n ]; then
#override: log on a virtual console, even if running X.
 export virtcons=y
else
 export virtcons=n
 case "`tty`" in
  /dev/tty[0-9]* | /dev/console  )
     export virtcons=y
 esac
fi

