#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Ref: http://sourceforge.net/forum/forum.php?thread_id=1759263&forum_id=394751
# In this example, it will allow your user to use clonezilla live to choose 
# (1) backup the image of /dev/hda1 (or /dev/sda1) to /dev/hda5 (or /dev/sda5)
# (2) restore image in /dev/hda5 (or /dev/sda5) to /dev/hda1 (or /dev/sda1)
# Here we assume the filesystems are ntfs.

# When this script is ready, you can run
# /opt/drbl/sbin/ocs-iso -g en -k NONE -s -m ./custom-ocs
# to create the iso file for CD/DVD. or
# /opt/drbl/sbin/ocs-live-dev -g en -k NONE -s -c -m ./custom-ocs
# to create the zip file for USB flash drive.

# Begin of the scripts:
# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions
. /opt/drbl/sbin/ocs-functions
# load the setting for clonezilla live.
. /etc/ocs/ocs-live.conf
# Load language files. For English, use "en". For Chinese, use "tw.UTF-8"
ask_and_load_lang_set en

# The above is almost necessary, it is recommended to include them in your own custom-ocs.
# From here, you can write your own scripts.

# functions
decide_sda_or_hda() {
  if [ -n "$(grep -Ew sda1 /proc/partitions)" -a -n "$(grep -Ew sda5 /proc/partitions)" ]; then
   disk=sda
  elif [ -n "$(grep -Ew hda1 /proc/partitions)" -a -n "$(grep -Ew hda5 /proc/partitions)" ]; then
   disk=hda
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "/dev/hda1 and /dev/hda5 do not exist or /dev/sda1 and /dev/sda5 do not exist!"
    echo "Program terminated!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
  # src_part: hda1 or sda1, tgt_part: hda5 or sda5
  src_part=${disk}1
  tgt_part=${disk}5
}
action_backup() {
  mkdir -p /home/partimag/
  ntfs-3g /dev/$tgt_part /home/partimag/
  RETV=$?
  if [ "$RETV" -eq 0 ]; then
    # If you want to run it in batch mode, add option "-b" in the ocs-sr command
    # For more options about ocs-sr, run "ocs-sr -h"
    /opt/drbl/sbin/ocs-sr -l en --use-ntfsclone -z3 -p true saveparts backup "$src_part" 
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Fail to mount /dev/$tgt_part as /home/partimag!"
    echo "Program terminated!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
}
 
action_restore() {
  mkdir -p /home/partimag/
  if ! mountpoint /home/partimag/ &>/dev/null; then
    ntfs-3g /dev/$tgt_part /home/partimag/
  fi
  if mountpoint /home/partimag/ &>/dev/null; then
    # If you want to run it in batch mode, add option "-b" in the ocs-sr command
    # For more options about ocs-sr, run "ocs-sr -h"
    /opt/drbl/sbin/ocs-sr -l en -p true --no-fdisk --no-restore-mbr restoreparts backup "$src_part" 
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Fail to mount /dev/$tgt_part as /home/partimag!"
    echo "Program terminated!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
} 

##################
###### MAIN ######
##################
TMP=$(mktemp /tmp/menu.XXXXXX)
trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
$DIA --backtitle "$msg_nchc_free_software_labs" --title  \
"$msg_nchc_clonezilla" --menu "$msg_choose_mode:" \
0 0 0 \
"Backup"  "Backup" \
"Restore" "Restore" \
2> $TMP
mode="$(cat $TMP)"
[ -f "$TMP" ] && rm -f $TMP

# find the device and partition
decide_sda_or_hda

#
case "$mode" in
  Backup)
    action_backup;;
  Restore)
    action_restore;;
  *)
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Program terminated!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
esac
