#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to start ezio in client machine.
# (1) ezio >= 2.0.16 (for daemon and always on)
# (2) Add torrent in leecher, e.g.,
#     ezio_add_torrent.py sda1.torrent /dev/sda1; ezio_add_torrent.py sda2.torrent /dev/sda2 … 
# (3) ezio_ui.py (for stop ezio automatically)
# 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

USAGE() {
    echo "$ocs - To start ezio in the Clonezilla client machine"
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs [OPTION] TORRENT_FILE DEVICE"
    echo "TORRENT_FILE is the torrent file"
    echo "DEVICE is the source raw device name, e.g., sda1, sda2, or /dev/sda1, /dev/sda2..."
    echo
    echo "Options:"
    echo "-c, --max_connect NUM    Max total connections number. If not assigned, $ezio_seed_max_connect will be used."
    echo "-u, --max_upload NUM     Max upload connections number. If not assigned, $ezio_seed_max_upload will be used."
    echo "-w, --wait SECS        The interval to keep uploading and waiting for other peer (sec). If not assigned, $ezio_upload_timeout will be used."
    echo
    echo "Ex:"
    echo "To start the leecher in client machine for /dev/sda1 with sda1.torrent, /dev/sda2 with sda2.torrent, use"
    echo "   $ocs sda1.torrent /dev/sda1 sda2.torrent /dev/sda2"
    echo
} # end of USAGE

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

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

while [ $# -gt 0 ]; do
  case "$1" in
    -c|--max_connect)
         shift; 
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           ezio_seed_max_connect="$1"
           shift;
         fi
         [ -z "$ezio_seed_max_connect" ] && USAGE && exit 1
         ;;
    -u|--max_upload)
         shift; 
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           ezio_seed_max_upload="$1"
           shift;
         fi
         [ -z "$ezio_seed_max_upload" ] && USAGE && exit 1
         ;;
    -w|--wait)
         shift; 
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           ezio_upload_timeout="$1"
           shift;
         fi
         [ -z "$ezio_upload_timeout" ] && 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

#
torrent_imgpath_pairs=$*
torrent_param=$#
if [ $((torrent_param%2)) -ne 0 ]; then
  # The inputted parameters must be in a pair, e.g., "sda1.torrent /dev/sda1"; "sda2.torrent /dev/sda2"
  [ "$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"
pkill -f -15 "^ezio-static"
ezio >$ocs_log_dir/ezio-leecher.log 2> $ocs_log_dir/ezio-leecher.err &
countdown 10
echo $msg_delimiter_star_line
# Parse the inputted parameters.
while [ $# -gt 0 ]; do
  bt_f=$1; shift
  rawdev=$(format_dev_name_with_leading_dev $1); shift  # e.g., /dev/sda1
  if [ ! -e "$bt_f" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Input torrent file $bt_f not found."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
  fi
  if [ ! -b "$rawdev" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Input block device \"$rawdev\" not found."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
  fi
  ezio_add_torrent_cmd="ezio_add_torrent.py -c $ezio_seed_max_connect -u $ezio_seed_max_upload \"$bt_f\" $rawdev"
  echo "Running: $ezio_add_torrent_cmd"
  eval $ezio_add_torrent_cmd
  sleep 0.5
done
sleep 1
cols="$(LC_ALL=C tput cols)"
lines="$(LC_ALL=C tput lines)"
if [ "$cols" -lt 80 -o "$lines" -lt 24 ]; then
  echo "Terminal is $cols x $lines. Use Ezio simple output mode..."
  ezio_cli_cmd="ezio_cli.py -w $ezio_upload_timeout"
  echo "Running: $ezio_cli_cmd"
  eval $ezio_cli_cmd
else
  clear
  echo "Terminal is $cols x $lines. Use Ezio TUI mode..."
  ezio_ui_cmd="ezio_ui.py -w $ezio_upload_timeout"
  echo "Running: $ezio_ui_cmd"
  eval $ezio_ui_cmd
  clear
fi

wait # wait for all processes to finish before exit
