#!/bin/bash

err_nodisk(){
	echo "The given backup device does not exist or is not connected to"
	echo "the slug! Aborting."
	exit 100
}

err_tools(){
	echo "Not all tools neccessary for the script could be found."
	echo "At the moment, you need the following utils installed:"
	echo "- rsync"
	echo "- udev"
	exit 101
}

err_mdisk(){
	echo "The chosen backup target is already mounted."
	echo "For safety reason, installation will only work, if the backup device"
	echo "is NOT mounted during installation"
	exit 102
}

err_mount(){
	echo "Couldn't mount backup device on backup mountpoint!"
	echo "Make sure, that a useable filesystem exists on your backup destination!"
	exit 103
}

cat << EOF
(c) 2006 by -XoF- xof (at) devroot.de
This Software is provided as is - No warranty!
You'll use this software on your own risk.

The software is published under the terms of the GNU Public License.
A copy of the license can be found here:
http://www.gnu.org/licenses/gpl.txt

================================================================================

SLUG backup script - INSTALLER

This script will do the following:
- Querying the backup device (Note: The whole backup has to go to one 
  destination partition! Multiple destinations are not supported at the 
  moment!)
- Install the backup script in /usr/local/sbin
- Install the (tiny) config file in /etc
- Create an udev-rule for automatic backup on HDD plug

BEFORE you proceed, make sure that the backup device is connected to your slug,
has a filesystem on it, but is NOT mounted!

If you accept the terms and conditions of the GPL and want to proceed, 
press "c" to proceed, otherwise press "a" to abort:"
EOF

while [ "$answer" != "c" -a "$answer" != "C" -a "$answer" != "a" -a "$answer" != "A" ]
do	
	echo
	echo -n "Your choice: "
	read -n 1 answer
done

[ "$answer" = "a" -o "$answer" = "A" ] && exit

echo
echo -n "Checking for neccessary tools."
which rsync &>/dev/null || err_tools
mount | grep ^udev &>/dev/null || err_tools
echo "OK"


echo -n "Which device/partition will be used for backup (e.g. sdb1, sdc2)? "
read sb_dev

fdisk -l | grep "/dev/$sb_dev" &>/dev/null

[ $? -ne 0 ] && err_nodisk
mount | grep $sb_dev &>/dev/null && err_mdisk


sb_mp=""
while [ "x$sb_mp" = "x" ]
do
	echo -n "Mountpoint for backup device (will be created if nonexistant): "
	read sb_mp
done

[ -d /$sb_mp ] || mkdir /$sb_mp

mount /dev/$sb_dev /$sb_mp &>/dev/null || err_mount
umount /$sb_mp

echo "Going to install SBU..."
echo -n "Installing script: "
cat << EOS > /usr/local/sbin/backup.sh
#!/bin/bash

err_budir(){
    echo "Backup-Directory does not exist. Exiting."
    exit 100
}

err_config(){
    echo "Config-File cannot be read. Exiting."
    exit 101
}

err_mount(){
    echo "Backupdisk cannot be mounted. Aborting."
    exit 102
}

check_env(){
    [ -r /etc/backup.cf ] || err_config
    . /etc/backup.cf
    [ -d \$sbu_dir ] || err_budir
}

mount_disk(){
    echo "Mounting target."
    mount \$sbu_dev \$sbu_dir || err_mount
}

umount_disk(){
    echo "Unmounting target."
    cd /
    umount \$sbu_dev
}

run(){
    mount_disk
    echo "Starting rsync on \`date\`"
    /usr/bin/rsync -a -r -H --stats --delete-before --exclude=\$sbu_dir --exclude=/dev/ --exclude=/proc/ --exclude=/sys/ \$sbu_rsyncopts / \$sbu_dir
    echo "rsync finished on \`date\`"
    umount_disk
}

exec 1>>/var/log/backup.log
exec 2>>/var/log/backup.log
check_env
run
exit 0

EOS
chown root:root /usr/local/sbin/backup.sh
chmod 700 /usr/local/sbin/backup.sh
echo "OK"

echo -n "Writing config file: "
cat << EOC > /etc/backup.cf
sbu_dir=/$sb_mp
sbu_dev=/dev/$sb_dev
#sbu_rsyncopts="--exclude=/dir/to/exclude/"
EOC

echo "OK"

echo -n "Creating udev-rule /etc/udev/rules.d/70-backupdisk.rules: "
vend=`udevinfo -a -p /sys/block/sdb|grep -C 20 SUBSYSTEMS==\"scsi\" | grep ATTRS{vendor}|sed -n 's#ATTRS#SYSFS#p'`
echo "BUS==\"scsi\", KERNEL==\"sdb1\", ACTION==\"add\", $vend, RUN+=\"/usr/local/sbin/backup.sh\"" > /etc/udev/rules.d/70-backupdisk.rules
echo "OK"


cat << EOF


Installation finished. Next time you plug in your backup device, SBU will
automatically start the backup process.
Log output can be found in /var/log/backup.log

Happy slugging!
EOF

exit 0


