#!/bin/sh
#< Selective system backup script 
# KW 08/11/04

# Usage: cpiobak [-o] outputfile.cpio.gz

# Requires: $HOME/etc/main_backup.conf - a list of full pathnames

print_usage()
{
  echo "Usage: $(basename $0) [-o] outputfile.cpio.gz" && exit 1
}

_config=$HOME/etc/main_backup.conf

if [ "$#" -eq "0" -o "$#" -gt "2" ]; then
  print_usage
fi

if [ ! -e "${_config}" ]; then
  echo "Error: Configuration file ${_config} cannot be found" && exit 1
fi

# check for -o
case ${1} in
  "-o")  _overwrite="yes"
         shift
         ;;
  *)     # file does not exist
         ;;
esac 

echo "${1}" | grep ".*[.]cpio[.]gz$" > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
  echo "Error: output file must have .cpio.gz extension" && exit 3
fi

if [ -e "${1}" ]; then
  if [ "$_overwrite" != "yes" ]; then
     echo "Error: ${1} already exists, use -o to overwrite" && exit 2
  fi
fi

# that's the initial validation over with!

_first=0
while read line
do
  if [ ! -d "$line" ]; then
    echo "Warning: $line not a directory, skipping"
  else 
    if [ "$_first" -eq "0" ]; then
      find $line -print | cpio -ov > ${1%*.*} 
      _first=1
    else
      find $line -print | cpio -oAv -F ${1%*.*}
    fi
  fi
done < ${_config}

if [ "${_overwrite}" = "yes" ]; then
  rm -f ${1}
fi
# now gzip
gzip ${1%*.*}

exit 0