#!/bin/bash
#
# This script is written 2006 by Hd Luc
# zbackup creates a flashable archive from the current system
#
#########################################################################

version=0.16
MODEL=`cat /proc/deviceinfo/product`
VERSION="pdaXii13 for $MODEL"
DATESTAMP=`date +%Y%m%d%H%M`
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 
{
  /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 
{
  /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
}

### begin
if [ "$1" = "-v" ] || [ "$1" = "-version" ] || [ "$1" = "--version" ]; then
  echo "`basename $0` v$version ($VERSION)"
  exit 0 
fi       

if [ -f /media/realroot/sbin/init ] || [ -f /media/fakeroot/usr/sbin/cfdisk ]; then 
  if [ "`mount|grep data`" != "" ]; then
    LOC=/data
  else
    LOC=/mnt/card
  fi
  NAME=pdaXrom-C3K-backup-$DATESTAMP.tgz
else
  LOC=/mnt/card
  NAME=initrd.bin
fi

if [ "$1" != "" ] && [ -d $1 ]; then
  LOC=$1
fi       

if [ "`echo $LOC|grep /mnt/card`" != "" ]; then
  mount|grep "/mnt/card" > /dev/null
  if [ $? -eq 1 ]; then
	echo "SD card not mounted! Exiting."
	exit 1
  fi
  FREE_SD=`df | grep /mnt/card | awk '{print $4;}'`
  if [ 100000 -gt $FREE_SD ]; then
    echo "There is only $[FREE_SD/1024] MB available on /mnt/card!"
    exit 1
  fi
fi

if [ "`echo $LOC|grep /data`" != "" ]; then
  FREE_HDD=`df | grep /data | awk '{print $4;}'`
  if [ 200000 -gt $FREE_HDD ]; then
    echo "There is only $[FREE_HDD/1024] MB available on /data!"
    exit 1
  fi
fi

if [ ! -d $LOC ]; then
  echo "$LOC does not exists! Exiting."
fi

echo "backing up $VERSION..."

if [ ! -f /etc/directories ]; then
  touch /etc/directories
fi
for i in `ls -1 --color=never /mnt/`
do
  if [ "`grep /mnt/$i /etc/directories`" =  "" ]; then
    echo /mnt/$i >> /etc/directories
  fi
done

/etc/rc.d/init.d/mntloop stop 2>/dev/null
if [ "`pidof X`" != "" ]; then
  set_dpms
fi

apm|grep "On-line" &> /dev/null
if [ $? -eq 0 ]; then
  echo ""
  echo "  AC adapter is connected."
  echo ""
else
  echo ""
  echo "  AC adapter is NOT connected!"
  echo "Please plug in to power source as backup may take over an hour..."
  echo ""
fi

echo ""
echo "Started backup at `date`"
echo "... please wait ..."

if [ -f /media/realroot/sbin/init ]; then 
  tar czf $LOC/$NAME /bin /etc /home /lib /root /sbin /usr 2>/dev/null
else
  if [ -f /media/fakeroot/usr/sbin/cfdisk ]; then 
    cd /media/fakeroot
    tar czf $LOC/$NAME /bin /etc /home /lib /root /sbin usr 2>/dev/null
    cd -
  fi

  if [ ! -d /mnt/backup ]; then
      mkdir -p /mnt/backup
  fi

  if [ "`mount|grep usr`" != "" ]; then
    umount /usr 2>/dev/null
  fi

  mount -t squashfs -o loop /home/.rootfs.squashfs /mnt/backup

  for i in bin home root etc lib sbin usr
  do
    mount --bind /$i /mnt/backup/$i 
  done
  tar cf $LOC/home_custom.tar /home/root/ 2>/dev/null
  mount --bind $LOC/home_custom.tar /mnt/backup/root/.home_default.tar 

  mkfs.jffs2 -n -e 16 -o $LOC/$NAME.jffs2 -d /mnt/backup

  umount /mnt/backup/root/.home_default.tar 
  rm $LOC/home_custom.tar
  for i in bin home root etc lib sbin usr
  do
    umount /mnt/backup/$i 
  done

  if [ -d /media/fakeroot/usr/bin ]; then
    mount --bind /media/fakeroot/usr /usr 2>/dev/null
  fi

  cat /mnt/backup/.initrdheader $LOC/$NAME.jffs2 > $LOC/$NAME

  rm $LOC/$NAME.jffs2
  umount /mnt/backup
  echo ""
  echo "Warning: the generated initrd.bin file may not work if the size of your root partition is less than 100MB."
  echo ""
fi

echo "Done! - backup finished at `date`"
echo "      - backup file is $LOC/$NAME"
echo " "

/etc/rc.d/init.d/mntloop start 2>/dev/null
if [ "`pidof X`" != "" ]; then
  reset_dpms
fi
