#!/bin/bash
# Original script by Robert Shingledecker for John Andrews Damn Small Linux
# October 2003
#
# rapidweather version 9, dated February 28, 2004
# For the Foxfire version of Damn Small Linux 0.6.1
# For the slave hard drive on the Dual Pentium Pro machine.
# This drive is partitioned into 2 partitions:
# /dev/hdb1 is for the backup tarball, and the filetool.lst for the Foxfire version of DSL 0.6.1
# /dev/hdb2 is for the backup tarball, and the filetool.lst for the MozillaFirebird version of DSL 0.6.1
# This file, filetool.sh, goes in /dev/hdb1, for the time being, we'll see how it works.
# With this setup, one can restore either the Foxfire version, or MozillaFirebird version
# on a bootup of Damn Small Linux 0.6.1 with the knoppix cheatcode:
# for MozillaFirebird version, #boot: knoppix restore /dev/hdb2
# for the Foxfire version, #boot: knoppix restore /dev/hdb1
#
# This script will create the /storage directory in the DSL filesystem for backup/restore.
# carefully modify this file to mount  the /storage directory on the  partition you want to use. 
#
# Here is the partition I am using: 
# using /dev/hdb1, a ext2 linux partition. 
# put this in line 40 below:
# 
# A simple script to save/restore configs, directories, etc defined by the user
# in the file filetool.lst
# Upon first successful run of this script a default filetool.lst will be
# copied to the BACKUP_DEVICE. Then the user can control which files/dirs will
# save/restore
#
HOME=/home/damnsmall

# Because it might exist, I am going to un-mount and remove the Storage directory
# so this script can proceed normally.
sudo umount /storage
sudo rmdir /storage

# Functions
# Get mounted backup device for save/restore of configs, etc.
getdevice() {
BACKUP_DEVICE=""
# For a restore/backup I have to create the storage directory within the DSL filesystem.  
sudo mkdir /storage
# Then, I'll mount it.
sudo mount /dev/hdb1 /storage
if [ $? == 0 ] ; then
  BACKUP_DEVICE=/storage
  return
fi

echo -n "Press enter to continue:" ; read ans
exit 1
return 
}
# End Functions

#Begin Main 
if [ -z $1 ] ; then 
  echo "Usage: filetool.sh {backup|restore}"
  echo -n "Press enter to continue:" ; read ans
  exit 1
fi

getdevice
if [ -z $BACKUP_DEVICE ] ; then
# should never get here. But what the heck.
  echo "Sorry, No mounted device found."  
  echo -n "Press enter to continue:" ; read ans
  exit 1
fi

if [ $1 == "backup" ] ; then
  if [ ! -f $BACKUP_DEVICE/filetool.lst ] ; then
  echo "You do not have a list of files to backup in storage."
  echo "Obtain one now and place it there." 
  echo -n "Press enter to continue:" ; read ans
  exit 1
fi
  cat $BACKUP_DEVICE/filetool.lst | sudo xargs tar -cvf - | gzip > $BACKUP_DEVICE/backup.tar.gz
  sudo umount $BACKUP_DEVICE
  echo "This is the Foxfire DSL Backup."
  echo -n "I have backed up your files, press enter to continue:" ; read ans
  exit 0
fi

if [ $1 == "restore" ] ; then
   if [ ! -f $BACKUP_DEVICE/backup.tar.gz ] ; then
      sudo umount $BACKUP_DEVICE
      echo "Sorry, could not find backup file."
      echo -n "Press enter to continue:" ; read ans
      exit 1
   fi
  gzip -dc $BACKUP_DEVICE/backup.tar.gz | sudo tar -xvf - -C /
  sudo umount $BACKUP_DEVICE
  echo "This is the Foxfire DSL Restore."
  echo -n "I have restored your files, press enter to continue:" ; read ans
  exit 0
fi
echo "I don't understand the command line parameter: $1"
echo "Usage: filetool.sh {backup|restore}"
echo -n "Press enter to continue:" ; read ans
exit 1