#!/bin/sh
#
# VMTux.net
#
# /usr/sbin/vmt-config
#
. /etc/vmt/vmt-colors

if [ "$(id -u)" -ne 0 ]; then echo "${BRed}Please run as root.${Reset}" ; exit 1; fi

PATH=/bin:/sbin

umask 022

DIALOG=/usr/local/bin/dialog
MKFS=/usr/local/sbin/mkfs.ext4

function MenuDialog()
{
  OLDIFS=$IFS
  exec 3>&1
  IFS=:
  set $menu
  selection=$(${DIALOG} \
    --backtitle "$BACKTITLE" \
    --title "$TITLE" \
    --clear \
    --cancel-label "Exit" \
    --menu "$MENUTEXT" 14 70 0 "$@" 2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  IFS=$OLDIFS
}

function CreateDataDisk()
{
  TITLE="VMTux: Create Data Disk"
  MENUTEXT=$(fdisk -l | grep Disk | grep -v contain | awk -F, '{ gsub(/:/, "", $1); print $1 }')
  DISKS=$(fdisk -l | grep Disk | grep -v valid | awk '{ gsub(/:/, "", $2); print $2 }')
  IFS=$'\n'
  menu=""
  i=0
  for disk in $DISKS; do
    if [ "$i" -gt 0 ]; then menu="${menu}:"; fi
    menu="${menu}${disk}:Create Data Disk on $disk"
    let i++
  done

  if [ -z "$menu" ]; then menu="r:return"; fi
  MenuDialog
  if [ "$exit_status" != 0 ]; then return; fi
  if [ "${selection}" == "r" ]; then return; fi
  (  
    echo "delete first partition from ${selection}"
    echo -e "d\nw\n" | fdisk ${selection} >/dev/null 2>&1
    echo "creating first partition on ${selection}"
    echo -e "n\np\n1\n2048\n\nw\n" | fdisk ${selection} >/dev/null 2>&1
    i=0
    while [ "$i" -lt 60 ]; do
      sleep 1
      let i++
      if [ -e ${selection}1 ]; then
        echo "making ext4 filesystem on ${selection}1"
        ${MKFS} -L data ${selection}1 >/dev/null 2>&1
        echo
        echo "Now it is a goot time to reboot (or make swap disk)"
        break
      fi
    done
  ) | ${DIALOG} --title "Creating Data Disk" "$@" --programbox 20 70
}

function CreateSwapDisk()
{
  TITLE="VMTux: Create Swap Disk"
  MENUTEXT=$(fdisk -l | grep Disk | grep -v contain | awk -F, '{ gsub(/:/, "", $1); print $1 }')
  DISKS=$(fdisk -l | grep Disk | grep -v valid | awk -F, '{ gsub(/:/, "", $1); print $1 }' | awk '{ print $2 }')
  IFS=$'\n'
  menu=""
  i=0
  for disk in $DISKS; do
    if [ "$i" -gt 0 ]; then menu="${menu}:"; fi
    menu="${menu}${disk}:Create Swap partition on $disk"
    let i++
  done

  if [ -z "$menu" ]; then
    menu="r:return"
  fi
  MenuDialog
  if [ "$exit_status" != 0 ]; then return; fi
  if [ "${selection}" == "r" ]; then return; fi
  (  
    echo "delete first partition from ${selection}"
    echo -e "d\nw\n" | fdisk ${selection} >/dev/null 2>&1
    echo "creating first partition on ${selection}"
    echo -e "n\np\n1\n\n\nw\n" | fdisk ${selection} >/dev/null 2>&1
    i=0
    while [ "$i" -lt 60 ]; do
      sleep 1
      let i++
      if [ -e ${selection}1 ]; then
        echo "making swap on ${selection}1"
        mkswap -L swap ${selection}1 >/dev/null 2>&1
        echo
        echo "Now it is a goot time to reboot (or make data disk)"
        break
      fi
    done
  ) | ${DIALOG} --title "Creating Data Disk" "$@" --programbox 20 70
}

while :
  do
    TITLE="Disk options"
    BACKTITLE="VMTux.net"
    MENUTEXT="choose an option"

    menu="1:create data disk\
:2:create swap disk\
:3:Reboot\
:q:Quit"

    MenuDialog
    if [ "$exit_status" != 0 ]; then clear ; exit 0 ; fi

    case  $selection  in
      1) CreateDataDisk ;;
      2) CreateSwapDisk ;;
      3) clear ; reboot ; exit 0 ;;
      q) clear ; exit 0 ;;
    esac
  done

exit 0
