#!/bin/ash

# Script to restore catalog from a previous cbkp full archive
# not a safe as cbkp script but still ... :)
# (see SLEEP_TIMING warnings)

# device to restore from, replace with your device
# /dev/rft0 for rewindable ftape
CAT_RESTORE_DEV=/dev/rft0

# where to restore, replace with your mounting point
CAT_RESTORE_ROOT_DIR=/mnt

#####################################################################
# some warnings first

echo "Catalog restore about to begin."
echo "If you have ToolDisk mounted and restore from tape then"
echo "exit this script (^C or Ctrl C) and unmount it."
echo "Make sure you have the partition you want to restore"
echo "mounted on $CAT_RESTORE_ROOT_DIR"
echo "Otherwise just press ENTER to start restoring."
read ans
echo "Restoring catalog from archive ..."


# cd to target directory first
cd $CAT_RESTORE_ROOT_DIR

#####################################################################
# START OF TAR SECTION, comment it *ALL* if you use another archiver
# (search for END OF TAR SECTION) 

# uncomment one and only one line:
# CAT_RESTORE_VERBOSE=" " # non verbose tar
CAT_RESTORE_VERBOSE="-v"  # verbose tar

# uncomment one and only one line:
CAT_RESTORE_COMPRESS=" " # non compression tar
# CAT_RESTORE_COMPRESS="-z"  # compresion tar

# start catalog restore in backgrownd
# such that we can watch for it's progress

tar -x \
    $CAT_RESTORE_VERBOSE \
    $CAT_RESTORE_COMPRESS \
    -f $CAT_RESTORE_DEV \
    0000_catalog.txt &

# get tar's PID for later kiling
# tar doesn't know to stop after the file retrival 
# so it will try to search trough all archive

# we are using *ash* !
TAR_PID=`jobid %1`

SLEEP_TIMING_ONE=30
SLEEP_TIMING_TWO=10
#____________________________________________________________________
# WARNING: take care on sleep timings:
#
#  * if first timing is too short CAT_LENGTH_OLD=CAT_LENGTH_NEW=0 !!!
#     and retreival will stop.
#  * if second timing is badly chosen then it may be possible that
#    CAT_LENGTH_OLD(just before switching traks) = 
#    = CAT_LENGTH_NEW(just afrer tracks were switched)
#    and, again, retreival will stop.
#
#  Not likely but it may happen.
#____________________________________________________________________

# loop for tar kiling after catalog retrieval
# wait a little bit till the retrieval will start (tape is slow)
# also at start it may take some time to retreive the tape directory

sleep 30

# will watch the length of file, 
# when it stops growing then retrieval is finished

CAT_LENGTH_NEW=`ls -l $CAT_RESTORE_ROOT_DIR/0000_catalog.txt | tr -s [:space:] | cut -f 5 -d " "`

while [ "$CAT_LENGTH_OLD" != "$CAT_LENGTH_NEW" ]
do
  # wait for retrieval, also it takes some time to switch tape tracks
  sleep 10
  # get new length
  CAT_LENGTH_OLD=$CAT_LENGTH_NEW
  CAT_LENGTH_NEW=`ls -l $CAT_RESTORE_ROOT_DIR/0000_catalog.txt | tr -s [:space:] | cut -f 5 -d " "`
done

# catalog does not grow anymore so the retrieval is finished, kill tar
kill $TAR_PID

# END OF TAR SECTION
#####################################################################
# some warnings at end

# remove catalog name from archive
echo "Catalog 0000_catalog.txt restored on $CAT_RESTORE_ROOT_DIR"
echo "Edit the catalog if you want to make a partial restore"
echo "___________________________________________________________"
echo "The name of the catalog (0000_catalog.txt) is in the catalog"
echo "on first line"
echo ""
echo "REMOVE IT !"
echo ""
echo "Otherwise it will be overwritten (at restore time)" 
echo "with unpredictable results."
echo "___________________________________________________________"
echo "Then use Restore script to do the actual restore"
echo "Of course you can edit the catalog to do a partial restore,"
echo "if you know what are you doing."
echo "Done."

# return to home dir (you are root, of course)
cd /root
