#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: Program to dump AoE image to DRBL server.

DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

#
device=$1
img_part=$2
img_name_prefix=$3

#
USAGE() {
   echo "Usage: $0 disk_device image_partition image_name"
   echo "Ex: $0 /dev/hda /dev/hda1 winaoe"
}

[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "///NOTE/// This program is still under testing! It might be changed in the future!"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

for i in device img_part img_name_prefix; do
  eval var_tmp=\$$i
  if [ -z "$var_tmp" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$i is nothing!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    USAGE
    echo "Program terminited!"
    exit 1
  fi
done

# Stop local mounting service
/etc/init.d/mkswapfile stop

# Check if the image name contains reserved characters
if [ -n "$(echo $img_name_prefix | grep -iE "aoe-[[:digit:]]+-[[:digit:]]+")" ]; then
  img_name_p1="$(echo $img_name_prefix | sed -e "s/aoe-[[:digit:]]*-[[:digit:]]*//g")"
  img_name_p2="$(echo $img_name_prefix | sed -e "s/$img_name_p1//g")"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "The input name contains reserved characters: $img_name_p2"
  echo "Please reassign the input name without $img_name_p2"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Program terminiated!"
  exit 1
fi

# Dump image to drbl server
# Ref: http://www.etherboot.org/wiki/sanboot/transfer
# Example:
#  Disk /dev/sdX: 80.0 GB, 80026361856 bytes
#  255 heads, 63 sectors/track, 9729 cylinders
#  Units = cylinders of 16065 * 512 = 8225280 bytes
#        
#  Device Boot      Start         End      Blocks   Id  System
#  /dev/sdX1   *        1        1825    14659281    7  HPFS/NTFS

mkdir -p $sanboot_img_dir

bs="$(fdisk -l $device | grep -iE '^Units = cylinders' | awk -F'=' '{print $3}' | sed -e 's/bytes//g' -e 's/[[:space:]]//g')"
count_end="$(fdisk -l $device | grep -iE "^$img_part" | awk -F' ' '{ print $(NF-3)}')"
count_start="$(fdisk -l $device | grep -iE "^$img_part" | awk -F' ' '{ print $(NF-4)}')"
count="$(echo "scale=0; $count_end - $count_start + 1" | bc -l)"
for i in bs count; do
 eval var_tmp=\$$i
 if [ -z "$var_tmp" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$i is nothing!"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   USAGE
   exit 1
 fi
 if [ -n "$(echo $var_tmp | grep -iE '[^[:digit:]]')" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$i not digits! Program terminated!"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
 fi
done

# Assign vbladed <shelf> <slot> to image name. Find a free one
# Here we will set the file name as IMAGENMAE.aoe-<shelf>-<slot>, e.g. image1.aoe-1-1
shelf_no=""
slot_no=""
for i in `seq 0 $aoe_shelf_max`; do
  for j in `seq 0 $aoe_slot_max`; do
    if [ -z "$(unalias ls &>/dev/null; ls -alFh $sanboot_img_dir/*.aoe-$i-$j-for-* 2>/dev/null)" ]; then
        shelf_no=$i
	slot_no=$j
    fi
    [ -n "$slot_no" ] && break
  done
  [ -n "$shelf_no" ] && break
done
echo "Available AoE shelf #: $shelf_no, slot #:$slot_no"

# MAC address of the ethx linked to drbl server
ethx_link_2_drbl="$(get-port-link-2-drbl-srv)"
if [ -n "$ethx_link_2_drbl" ]; then
  # To avoid strange character ":" in the file name, we use "-"
  mac_add="$(drbl-get-macadd $ethx_link_2_drbl | tr "[A-Z]" "[a-z]" | tr ":" "-")"
else
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "The ethernet port connected to DRBL server was NOT found!"
  echo "Something went wrong!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Program terminated!"
  exit 1
fi
aoe_img_name="${img_name_prefix}.aoe-${shelf_no}-${slot_no}-for-${mac_add}"
#
echo "Dumping the $device to server as $sanboot_img_dir/$aoe_img_name by:"
echo dd if=$device of=$sanboot_img_dir/${aoe_img_name} bs=$bs count=$count
echo "This might take a while... "
part_size="$(ocs-get-part-info $img_part size)"
trigger_dd_status_report $img_part $part_size &
dd_report_sig_pid=$!
start_time="$(date +%s%N)"
dd if=$device of=$sanboot_img_dir/${aoe_img_name} bs=$bs count=$count
end_time="$(date +%s%N)"
kill -9 $dd_report_sig_pid &>/dev/null
echo "done!"
