#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to check the Clonezilla image integrity
# (1) ezio (Start daemon and always on, add "-F" for image format, not raw device)
# (2) Add torrent in seeder, e.g.,
#     ezio_add_torrent_seeder.py sda1.torrent /dev/sda1; ezio_add_torrent.py sda2.torrent /dev/sda2 … 
# Ref: https://hackmd.io/1i7cRjd8Rc2ywzXtxCc1LQ

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Load the config in ocs-live.conf. This is specially for Clonezilla live. It will overwrite some settings of /etc/drbl/drbl-ocs.conf, such as $DIA...
[ -e "/etc/ocs/ocs-live.conf" ] && . /etc/ocs/ocs-live.conf

# The way to start ezio in seeder
# img:
# Ezio v1.0: -T sda1.torrent -L /dev/sda1 -T sda2.torrent -L /dev/sda2
# Ezio v2.0:
# -> 3 steps:
# ezio -F
# ezio_add_torrent.py sda1.torrent /some/sda1_image_path
# ezio_add_torrent.py sda2.torrent /some/sda2_image_path

# raw-dev:
# Ezio v1.0:
# raw-dev: ezio -U -T sda1.torrent -L /dev/sda1 -T sda2.torrent -L /dev/sda2
# Ezio v2.0:
# -> 3 steps:
# ezio
# ezio_add_torrent.py sda1.torrent /dev/sda1
# ezio_add_torrent.py sda2.torrent /dev/sda2

USAGE() {
    echo "$ocs - To start ezio in the Clonezilla source machine"
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs [OPTION] TORRENT_FILE DIR|DEVICE"
    echo "TORRENT_FILE is the torrent file"
    echo "DIR is the image files, generated by partclone_create_torrent.py."
    echo "DEVICE is the source raw device name, e.g., sda1, sda2, or /dev/sda1, /dev/sda2..."
    echo
    echo "Options:"
    echo "-m, --mode img|raw-dev   Run seeding in image or raw-dev format."
    echo "-g, --log-dir DIR        Use DIR as the log dir. Default value is \"$btlog_dir\""
    echo
    echo "Ex:"
    echo "To start the seeder in source machine for /dev/sda1 with sda1.torrent, /dev/sda2 with sda2.torrent, use"
    echo "   $ocs sda1.torrent /dev/sda1 sda2.torrent /dev/sda2"
    echo
    echo "To start the seeder in source machine for image files in /home/partimage/btzone/my-img with sda1.torrent, /home/partimage/btzone/my-img  with sda2.torrent, use"
    echo "   $ocs sda1.torrent /home/partimage/btzone/my-img sda2.torrent /home/partimage/btzone/my-img"
    echo
} # end of USAGE

####################
### Main program ###
####################

ocs_file="$0"
ocs=`basename $ocs_file`

while [ $# -gt 0 ]; do
  case "$1" in
    -m|--mode)
         shift; 
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           ezio_mode="$1"
           shift;
         fi
         [ -z "$ezio_mode" ] && USAGE && exit 1
         ;;
    -g|--log-dir)
         shift; 
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           btlog_dir="$1"
           shift;
         fi
         [ -z "$btlog_dir" ] && USAGE && exit 1
         ;;
    -*)  echo "${0}: ${1}: invalid option in program $ocs_file." >&2
         USAGE >& 2
         exit 2 ;;
    *)   break ;;
  esac
done

#
ask_and_load_lang_set

# Create log dir
mkdir -p $ocs_log_dir

#
ezio_pid_log="$btlog_dir/ezio-seeder.pid"

# Process the rest parameters.
torrent_imgpath_pairs=$*
torrent_param=$#
if [ $((torrent_param%2)) -ne 0 ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Wrong number of inputted parameters in program $ocs_file."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi
if [ -z "$torrent_imgpath_pairs" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "No inputted parameters in program $ocs_file."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi

pkill -f -15 "^ezio"
rm -f $ezio_pid_log
case "$ezio_mode" in
  img) ezio_daemon_opt="-F";
esac
ezio $ezio_daemon_opt >$ocs_log_dir/ezio-seeder.log 2>$ocs_log_dir/ezio-seeder.err &
ezio_pid="$!"
echo "$ezio_pid" > $ezio_pid_log
sleep 2
countdown 10
echo
echo $msg_delimiter_star_line
# Parse the rest parameters.
while [ $# -gt 0 ]; do
  bt_f=$1; shift
  imgp_or_rawdev=$1; shift
  if [ ! -e "$bt_f" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Input torrent file $bt_f not found."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
  fi
  case "$ezio_mode" in
    img)# Check the dir 
        if [ ! -d "$imgp_or_rawdev" ]; then
          [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
          echo "Input torrent dir \"$imgp_or_rawdev\" not found."
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
          exit 1
        fi
        ;;
    raw-dev) # Check the block device
	imgp_or_rawdev="$(format_dev_name_with_leading_dev $imgp_or_rawdev)"  # e.g., /dev/sda1
        if [ ! -b "$imgp_or_rawdev" ]; then
          [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
          echo "Input block device \"$imgp_or_rawdev\" not found."
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
          exit 1
        fi
        ;;
  esac
  ezio_add_torrent_cmd="ezio_add_torrent_seed.py $bt_f $imgp_or_rawdev"
  echo "Running: $ezio_add_torrent_cmd"
  eval $ezio_add_torrent_cmd
done
