#!/bin/bash

# 
# backup.sh - Backup the Agenda 
# Copyright (C) 2001  Andrej Cedilnik
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 

# Special thanks:
#	Sean 'Shaleh' Perry <shalehperry@home.com> for bugfixing this
#		script.

# For faster execution create file .andydefaults in your home directory
# In this file you can write three defaults:
# 	AGENDAIP=10.1.1.3                        # the IP of your Agenda Vr3
#	BACKUP_DIR=/tmp/agenda_backup            # directory to backup to
#       NOTBACKUP="/mnt/ws /mnt/server /spool"   # do not backup

# You can run this script as:
#   ./backup.sh -x /mnt/ws /mnt/server /spool
# and it will not backup /mnt/ws /mnt/server and /spool
# or
# you can run this script as:
#   ./backup.sh -o /home /root
# and it will only backup /home and /root
#
# You can also run it as:
#   ./backup.sh -l 
# if you have old romdisk

VERSION=0.6

version() {
	echo "AndyDefaults Backup Version $VERSION by Andy"
}

gethelp() {
	version
	echo "Usage: $0 options [-x directories or files]"\
	"[-o directories or files]"
	echo "	-V version"
	echo "	-v verbose mode"
	echo "	-h this help"
	echo "  -x exclude files"
	echo "  -o only files"
	echo "  -l old romdisk (vr3 istead of root)"
}

##########################################################
# Check the network connection                           #
##########################################################
check_network() {
	if [ "$1" = "" ] 
	then
		echo "Usage: check_network <IP>"
		exit -2
	fi
	if uname -s | grep -i CYGWIN > /dev/null 2>&1
        then
        	# Wincrap
        	PING="ping -n 2"
        else
		# Something else
        	PING="ping -c 2 -q"
        fi
	while [ "$AGENDARUNNING" != "true" ]
	do
		echo -n "Checking for connection with Agenda Vr3: "
		if ping -c 2 -q $AGENDAIP 2>&1 | grep [^0]0% > /dev/null 2>&1
		then
			echo "found"
			AGENDARUNNING=true
		else
			echo "not found"
			echo "Start PPP communication with your Agenda Vr3 such as:"
			echo "/usr/sbin pppd /dev/ttyS0 $AGENDAWSIP:$AGENDAIP "\
				 "noauth nodetach debug 115200 local novj"
			echo -n "Press enter when done: "
			read line
		fi   
	done
}


exclude=
onlyfiles=
verbose=
CNOTBACKUP=
CONLYBACKUP=
oldagenda=

while getopts hvVxol opt
do
	case "$opt" in
		v) verbose=true ;;
		h) gethelp
		   exit 0;;
		V) version
		   exit 0;;
		x) exclude=true;;
		o) onlyfiles=true;;
		l) oldagenda=true;;
		*) gethelp
		   exit -1;;
	esac
done

if [ "$exclude" = "true" -a "$onlyfiles" = "true" ]
then
	echo "You can only use -x or -o but not both"
	exit -1
fi

shift $(($OPTIND - 1))

if [ "$exclude" = "true" ]
then
	CNOTBACKUP=$*
	if [ "$CNOTBACKUP" = "" ]
	then
		echo "You have to specify files or directories to exclude"
		exit -1;
	fi
fi

if [ "$onlyfiles" = "true" ]
then
	CONLYBACKUP=$*
	if [ "$CONLYBACKUP" = "" ]
	then
		echo "You have to specify files or directories to backup"
		exit -1;
	fi
fi

version
echo "This script can potentially make your system useless."
echo -n "Are you sure you want to continue? (yes|No) : "

read line lala
if [ "$line" != "yes" -a "$line" != "Yes" -a "$line" != "YES" ]
then
	echo "Exiting..."
	exit
fi

if [ -f $HOME/.andydefaults ] 
then
	. $HOME/.andydefaults
fi

if [ "$NOTBACKUP" != "" -a "$onlyfiles" = "true" ]
then
	echo "You can only use -o and NOTBACKUP option at the same time"
	exit -1
fi

if [ "$AGENDAIP" = "" ]
then
	echo -n "Agenda side IP (10.1.1.3): "
	read AGENDAIP
	if [ "$AGENDAIP" = "" -o -z $AGENDAIP ]
	then
		AGENDAIP="10.1.1.3"
	fi
fi

while [ "$BACKUP_DIR" = "" ] 
do 
    echo -n "Directory to put backup files: "
	read BACKUP_DIR
done

if [ ! -d $BACKUP_DIR ] 
then
	echo "Creating backup directory: $BACKUP_DIR"
	mkdir $BACKUP_DIR
fi

check_network $AGENDAIP

EXCLUDE=""
INCLUDE=""
if [ "$CONLYBACKUP" = "" ]
then
	for a in /dev /ramdisk /flashdisk /README $NOTBACKUP $CNOTBACKUP
	do
		EXCLUDE="$EXCLUDE --exclude=$a"
	done
else
	for a in $CONLYBACKUP
	do 
		INCLUDE="$INCLUDE --include=$a"
		EXCLUDE="--exclude=*"
	done
fi

if [ "$oldagenda" = "true" ]
then
	TAG=vr3
else
	TAG=root/flash
fi

echo "Backuping your Agenda files: "
if [ "$verbose" = "" ]
then
	rsync -abz --progress $INCLUDE $EXCLUDE $AGENDAIP::$TAG \
		$BACKUP_DIR
else
	rsync -abzv $INCLUDE $EXCLUDE $AGENDAIP::$TAG $BACKUP_DIR
fi
echo " done"

