#!/bin/bash

# This program is part of the tbackup package,
# (c) 1993,1994,1995,1998 Koen Holtman.
#
# The tbackup package is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
# No warranty. See the GNU General Public License for more details.

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, on " `whoami` `hostname` >>descr
date >>descr

source makelogtty

source handlepars

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
#This old code crashes on some Linux systems, though it shouldn't.
# yes "This is a filler string to get a 2k file..................."| \
# cat tmp - | head -c2k - >descr2k 2>/dev/null
 gawk '{ print } END { while(1) \
 print "This is a filler string to get a 2k file..................."}' \
 tmp | head -c2k - >descr2k 2>/dev/null
else
 gawk '{ print } END { while(1) \
 print "This is a filler string to get a 1.5k file................."}' \
 tmp | head -c1536 - >descr2k 2>/dev/null
fi

rm tmp

#Remove possible update scripts left in $packhome to prevent them from
#being packed in the archive.
#XXX source rmupdates

# 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.
doabort 

fi

source texit
