#!/bin/bash
#
# This program is part of the three backup (tbackup) package,
# (c) 1993,1994 Koen Holtman.

export bupbin=/usr/lib/tbackup/bin
export bupdir=/usr/lib/tbackup
export PATH=$bupbin\:$PATH
export ttytitle=backup

#possibly set to y by floppy.wp
export descrdown=n

source tinit

unset nofinalise

#get method names and parameters

source getbuppars

#add name and date to descr file
echo "Made with tbackup ($version)" >>descr
printf "by %s@%s.%s, on " `whoami` `hostname` `domainname` >>descr
date >>descr

source makelogtty

#exit if pars argument given.
if [ "$only_params" = "y" ]; then
 cp /tmp/tbup/allpars /etc/tbackup/allpars
 echo
 echo The supplied parameters are recorded in the file /etc/tbackup/allpars.
 echo
 echo Cleaning up....
 cd /tmp
 rm -rf /tmp/tbup
 eval $rmbulkdir
 exit 0
fi


echo
echo Backup description:
gawk '{ print "     " $0; }' descr
if [ $makeindexfile = y ]; then
 echo "Backup index file is $indexfilename."
fi

echo
echo "Backup in progress..."
echo

# make named pipes

mkfifo -m 700 choice archive

#make descr2k file 

gawk "{ if (\$0 == \"---\") { print \"- --\" } else { print } }" descr >tmp
echo "---" >>tmp
cat rpars >>tmp
echo "<<<" >>tmp

if [ $descrdown = n ]; then
 yes "This is a filler string to get a 2k file..................."| \
 cat tmp - | head -c2k - >descr2k 2>/dev/null
else
 yes "This is a filler string to get a 1.5k file................."| \
 cat tmp - | head -c1536 - >descr2k 2>/dev/null
fi

rm tmp


# execute backup
# choosemethod and packmethod are supposed to start background tasks.
# writemethod should return if the archive is written.

cd /tmp/tbup
source $choosemethod\.cx
cd /tmp/tbup
source $packmethod\.px
cd /tmp/tbup
source $writemethod\.wx

#finalise methods
#the choose method can now update partial backup dates an the like.

if [ "$nofinalise" != y ]; then

 if [ -f $bupbin/$choosemethod\.cf ]; then
  cd /tmp/tbup
  source $choosemethod\.cf
 fi
 if [ -f $bupbin/$packmethod\.pf ]; then
  cd /tmp/tbup
  source $packmethod\.pf
 fi
 if [ -f $bupbin/$writemethod\.wf ]; then
  cd /tmp/tbup
  source $writemethod\.wf
 fi

else
#something went wrong in the write method.
#Simulate a ^C. This gets rid of running choose and pack processes.
#Unfortunately, the backup progress window is also destroyed immediately.
kill -2 0 

fi

# if making one, stop flowpause etc that take care of index file.
if [ $makeindexfile = y ]; then
  kill $holdfifopid
fi

echo >logttydev
echo "---------Backup completed---------" >logttydev

echo "Cleaning up..."
cd /tmp

#sleep 1 added to prevent race condition (?) in the kernel.
sleep 1

rm -rf /tmp/tbup
eval $rmbulkdir
eval $resetfloppy
sync

echo
echo "Backup completed."


