#!/bin/bash
# Program:
#   DRBL virt module
# Author: 
#   Jazz, Rock {jazz, rock}@nchc.org.tw
# Version:
#    1.0
# History:
#   2010/07/20  Rock    First release (1.0)

# [Variable Declation]
cpu_flag="no"
Linux_bit=""
Linux_Distribution=""
Linux_Version=""
VMM_select=""

# [Check Root]
function check_root(){      
  if [ $USER != "root" ]; then
    echo -e "Please change root to run it!"
    exit
  fi
} 

function check_root_run(){
    if [ $USER != "root" ]; then
    echo -e "Please change root to run it!"
    sudo su -c ~/"$0" "$@"
    exit
fi
}


# [Check CPU support]
function cpu_check(){
egrep '(vmx|svm)' --color=always /proc/cpuinfo > /dev/null
if [ $? == 0 ]; then
    cpu_flag="yes"
fi
}

# [Check System Version]
function check_systemInfo(){
Linux_bit=$(uname -m)
Linux_Distribution=$(lsb_release -i | awk '{print $3}')
Linux_Version=$(lsb_release -r | awk '{print $2}')
}

# [Select Hypervisior(Xen/KVM)]
function VMM_select(){
echo ""
if [ $cpu_flag == "yes" ]; then
    while [ "$VMM_select" != "1" -a "$VMM_select" != "2"  ]
    do
       read -p "Which VMM/Hypervisior will be used in DRBL (1)Xen (2)KVM: " VMM_select
    done
	if [ $VMM_select == "1"  ]; then
	  echo "Xen" > $Work_Home/etc/hypervisior
	else
	  echo "KVM" > $Work_Home/etc/hypervisior
	fi	
else
    echo "Xen will be the only Hypervisior in your DRBL"
    echo -e "Because CPU don't support virtualization, it can't choose KVM to be the Hypervisior"
    echo ""
    VMM_select=1
    echo "Xen" > $Work_Home/etc/hypervisior
fi  
}   


# [Create drbl-virt path]
function install_drbl-virt_dir(){
    mkdir -p $Work_Home/.tmp
    mkdir -p $Work_Home/etc
}


# [Check Hypervisior]
function check_hypervisior(){
    hypervisior=$(cat $Work_Home/etc/hypervisior)    
}


# [Check Debian Xen package]
function check_debian_xen(){
aptitude update
check_debian_xen_pkg=$(aptitude search xen-linux-system)
if [ -n $check_debian_xen_pkg  ]; then
    check_debian_xen_pkg="yes"
else
    check_debian_xen_pkg="no"
fi
}


# [Check Xen kernel]
function check_xen_nu(){
Kernels=$(ls /boot | grep vmlinuz)
declare -i Kernels_nu=$(echo $Kernels | wc -w)
Kernel_choose=""

Xens=$(ls /boot | grep -i ^xen)
declare -i Xen_nu=$(echo $Xen | wc -w)
Xen_choose=""

jude="no"

while [ $jude != "yes" -a $jude != "y" ]
do
    declare -i i=1
    declare -i j=1
    echo ""
    
    if [ $Kernels_nu -gt 1  ]; then
        for Kernel in $Kernels
        do
            echo "($i) $Kernel" 
            i=i+1
        done

        read -p "Xen Kenrel is (1/2/...): " Kernel_choose
        Xen_Kernel=$(echo $Kernels | cut -d " " -f${Kernel_choose})

    else
        Xen_Kernel=$Kernels
    fi

    if [ $Xen_nu -gt 1  ]; then
        for Xen in $Xens
        do
            echo "($i) $Xen"
            j=j+1
        done

        read -p "Xen is (1/2/...): " Xen_choose
        Xen=$(echo $Xens | cut -d " " -f${Xen_choose})
    else
        Xen=$Xens
    fi

    read -p "Are you sure (yes/no): " jude
done

#echo "debug: Xen_Kernel=$Xen_Kernel"
}


function debain-lenny_xen_patch(){
echo xen.independent_wallclock=1 >> /etc/sysctl.conf
echo loop max_loop=255 >> /etc/modules
echo xenblktap >> /etc/modules
ln -s /usr/lib/$/bin/tapdisk /usr/sbin
}


function get_DRBL_eth(){
eths=$(ls /etc/drbl | grep macadr-eth[0-9] | grep [0-9].txt | cut -d "-" -f2 | cut -d "." -f1)
eths_nu=$(echo $eths | wc -l)


}

function get_Host_IP_range(){
if [ ! -e /usr/bin/ipcalc ] || [ ! -e /opt/drbl/bin/drbl-get-network ] || [ ! -e /opt/drbl/bin/drbl-get-ipadd ]; then
    echo "Don't find /usr/bin/ipcalc /opt/drbl/bin/drbl-get-network & /opt/drbl/bin/drbl-get-ipadd"
    exit 0
fi

# get eth network
eth_address=$(/opt/drbl/bin/drbl-get-ipadd $1)
eth_netmask=$(/opt/drbl/bin/drbl-get-netmask $1)
eth_network=$(/opt/drbl/bin/drbl-get-network $eth_address $eth_netmask)
eth_network_f3=$(echo $eth_network | cut -d "." -f 1-3)

IP_nu=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | wc -l)
Host_first_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==1 {print $1}" | awk '{print 1}')
Host_last_IP=$(cat /etc/drbl/IP_HOST_TABLE | grep $eth_network_f3 | awk "NR==$IP_nu {print $1}"| awk '{print $1}')

}

function get_VM_IP_range(){
echo ""
echo "DRBL client IP range -> $1 [$Host_first_IP ~ $Host_last_IP]"

declare -i Host_last_IP_4=$(echo $Host_last_IP | cut -d "." -f4)
Host_last_IP_4=$(($Host_last_IP_4+1))
VM_first_IP=$(echo $Host_last_IP | cut -d "." -f 1-3)
VM_first_IP="${VM_first_IP}.${Host_last_IP_4}"

echo "The VM IP Range will start from -> [$VM_first_IP]"
jude="no"
while [ $jude != "yes" -a $jude != "y" ];
do
    echo ""
    echo "Input VM number/IP range in $1 (ex. 40)."
    read -p "(If you have 10 PCs(4-core), suggestion is 30): " VM_IP_range
    read -p "Is it correct? (yes/no): " jude
done

#echo "VM_first_IP=$VM_first_IP"
#echo "VM_IP_range=$VM_IP_range"
}

function get_VM_prefix_name(){ 
jude="no"
while [ $jude != "yes" -a $jude != "y" ];
do
    echo ""
    read -p "Input Prefix name for VM in $1 (ex. drblvm): " VM_prefix_name
    read -p "Is it correct? (yes/no): " jude
done
#echo "VM_prefix_name=$VM_prefix_name"
}

function create_VM_IP_table(){
#/etc/drbl-virt/etc/IP_VM_eth

}




