#!/bin/bash

# This script is written 2007 by hermocom

# This script performs a backup.
# It determines the kind of backup from various parameters.
# If all necessary conditions are met, the entire weeXpc
# is backed up to an USB drive.
# If not all these conditions are met, an internal backup
# is done: Only /mnt/card/Documents is backed up to /mnt/ide3.
# 
# During backup, suspend is disabled and screensaver is 
# activated.
# After backup, original state is restored.
#
# Before the backup, the needed and available disk capacity
# on the target medium are estimated.
# If there is not enough space available, backup is not done.
# The compression factor of gzip is assumed to be 1.33 for 
# these calculations.


DATESTRING=`date +%Y-%m-%d_%Hh%M`
BACKUPDIR="/mnt/usbstorage/weexpc_backup/${DATESTRING}"
BACKUPDIRINT="/mnt/ide3/Documents_backup"
TARCMD="tar --totals --one-file-system -cpPf -"

STATUS_SUSPEND=`xset q | grep Standby | awk '{print $6;}'`
STATUS_STANDBY=`xset q | grep Standby | awk '{print $2;}'`
STATUS_DPMS=`xset q | grep "DPMS is" | awk '{print $3}'`
STATUS_XSCRSV=`pidof xscreensaver`

function set_dpms {
echo Disabling Suspend, enabling screensaver timeout 60 sec...
/usr/X11R6/bin/xset dpms 60 0 0
/usr/X11R6/bin/xset -dpms
/usr/X11R6/bin/xset s blank
/usr/X11R6/bin/xset s 60
/usr/X11R6/bin/xset s on
if [ "`pidof xscreensaver`" != "" ]; then
  /usr/local/bin/xscreensaver-command -exit 2>/dev/null
fi
}

function reset_dpms {
echo Restoring Suspend and Screensaver settings...
/usr/X11R6/bin/xset dpms $STATUS_STANDBY 0 $STATUS_SUSPEND
if [ $STATUS_DPMS = "Disabled" ]; then
    /usr/X11R6/bin/xset -dpms
else
    /usr/X11R6/bin/xset +dpms
fi

if [ ! -z "$STATUS_XSCRSV" ]; then
    sudo -u zaurus xscreensaver -nosplash 2>/dev/null &
fi
  if [ "$STATUS_STANDBY" = "0" ]; then
    /usr/X11R6/bin/xset s noblank
    /usr/X11R6/bin/xset s 0
    /usr/X11R6/bin/xset s off
  else
    /usr/X11R6/bin/xset s blank
    /usr/X11R6/bin/xset s $STATUS_STANDBY
    /usr/X11R6/bin/xset s on
  fi

}

function internal_backup {
echo "====> PERFORM SMALL INTERNAL BACKUP OF /mnt/card TO /mnt/ide3"
USED_DOCS=`du -s /mnt/card/Documents | awk '{print $1;}'`
FREE_IDE3=`df | grep /mnt/ide3 | awk '{print $4;}'`
NEED_COMPR=$[$USED_DOCS*3/4]
NEED_COMPR_MB=$[$NEED_COMPR/1024]

if [ $NEED_COMPR -gt $FREE_IDE3 ]; then
	echo There is not enough space on /mnt/ide3!
	echo Please make sure there are $NEED_COMPR_MB MB available!
	exit 1
fi

if [ $USED_DOCS -gt $FREE_IDE3 ]; then
	echo Maybe not enough space on /mnt/ide3!
	echo Trying anyway. It could be enough...
fi

echo
echo Okay. Creating backup in ${BACKUPDIRINT}.
echo

mkdir -p ${BACKUPDIRINT}

set_dpms

echo ==== Creating Backup of /mnt/card/Documents....
${TARCMD} /mnt/card/Documents | \
gzip > ${BACKUPDIRINT}/${DATESTRING}_mnt_card_Documents.tgz 
echo

reset_dpms

echo Done. Created backup file: 
echo ${BACKUPDIRINT}/${DATESTRING}_mnt_card_Documents.tgz
}


function full_backup {
echo "====> PERFORM FULL BACKUP TO USB"
# Capacity calculations... (values in kB)
USED_CARD=`df | grep /mnt/card | awk '{print $3;}'`
USED_ROOT=`df | grep /dev/root | awk '{print $3;}'`
USED_IDE2=`df | grep /mnt/ide2 | awk '{print $3;}'`
USED_IDE3=`df | grep /mnt/ide3 | awk '{print $3;}'`
FREE_USB=`df | grep /mnt/usbstorage | awk '{print $4;}'`
NEED_SUM=$[$USED_CARD+$USED_ROOT+$USED_IDE2+$USED_IDE3]
NEED_SUM_MB=$[$NEED_SUM/1024]
NEED_COMPR=$[$NEED_SUM*3/4]
NEED_COMPR_MB=$[$NEED_COMPR/1024]


if [ $NEED_COMPR -gt $FREE_USB ]; then
	echo There is not enough space on the USB device!
	echo Please make sure there are $NEED_COMPR_MB MB available!
	echo Or start $0 again
	echo with parameter "internal" for internal backup.
	exit 1
fi

if [ $NEED_SUM -gt $FREE_USB ]; then
	echo Maybe not enough space on USB storage device!
	echo Trying anyway. It could be enough...
fi



set_dpms

/etc/rc.d/init.d/mount_loopdev stop

echo
echo Okay. Creating backup in ${BACKUPDIR}.
echo Backup can take some hours. Please let AC adapter connected!
echo

mkdir -p ${BACKUPDIR}

echo ==== Creating Backup of /mnt/card....
${TARCMD} /mnt/card/ | gzip > ${BACKUPDIR}/mnt_card.tgz && \
echo ==== Creating Backup of root filesystem....&& \
${TARCMD} /bin /etc /home /lib /media /opt /root /sbin /usr /var | \
gzip > ${BACKUPDIR}/root.tgz && \
echo ==== Creating Backup of /mnt/ide2....&& \
${TARCMD} /mnt/ide2/ | gzip > ${BACKUPDIR}/ide2.tgz && \
echo ==== Copying /mnt/ide3 contents... please wait! && \
cp -a /mnt/ide3 ${BACKUPDIR}

/etc/rc.d/init.d/mount_loopdev start

reset_dpms

echo Done.
echo Backed everything up in ${BACKUPDIR}.
}



# MAIN SCRIPT:


if [ $UID -gt 0 ]; then
	echo NEED ROOT PERMISSIONS! Exiting.
	exit 1
fi

mount | grep "/mnt/card" > /dev/null
if [ $? -eq 1 ]; then
	echo SD card not mounted! Exiting.
	exit 1
fi

apm | grep "On-line" &> /dev/null
if [ $? -eq 0 ]; then
	echo AC adapter is connected.
	TEST_AC_OK=1
else
	echo AC adapter is NOT connected.
	TEST_AC_OK=0
fi


mount | grep "/mnt/usbstorage" &> /dev/null
if [ $? -eq 0 ]; then
	echo USB storage device is mounted.
	TEST_USB_OK=1
else
	echo USB storage device is NOT mounted.
	TEST_USB_OK=0
fi



BEGINTIME=`date +%s`

if [ "$1" = "internal" ]; then
  internal backup
  exit 0
fi

if [[ $TEST_USB_OK -eq 1 && $TEST_AC_OK -eq 1 ]]; then
  full_backup
else
  internal_backup
fi
ENDTIME=`date +%s`

DURATION_SECTOT=$[$ENDTIME-$BEGINTIME]
DURATION_H=$[$DURATION_SECTOT/3600]
DURATION_H_SECS=$[$DURATION_H*3600]
DURATION_SECTOT_NOHOURS=$[$DURATION_SECTOT-$DURATION_H_SECS]
DURATION_M=$[$DURATION_SECTOT_NOHOURS/60]

echo Duration of this backup was ${DURATION_H}h${DURATION_M}m

exit 0
