#!/bin/bash

set -e

# This script installs a MS/worker/campus-manager/cache/MV through envsetup on a machine

# Prerequisites
# 1. create DNS entries
#	alpha-auto.ubicast.net
#	cm-alpha-auto.ubicast.net
#	monitor-alpha-auto.ubicast.net
# 2. create machine in panel
# 3. envsetup-launcher.sh

# log
LOGFILE=/root/$(date +%F)_envsetup.log
exec > >(tee -i ${LOGFILE})

## envsetup integration
init() {
    python3 /root/envsetup/envsetup.py 1
    python3 /root/envsetup/envsetup.py 2
    python3 /root/envsetup/envsetup.py 22
    python3 /root/envsetup/envsetup.py 23
    python3 /root/envsetup/envsetup.py 24
    python3 /root/envsetup/envsetup.py 4
}

monitor() {
    python3 /root/envsetup/envsetup.py 3
    python3 /root/envsetup/envsetup.py 6
    python3 /root/envsetup/envsetup.py 7
    python3 /root/envsetup/envsetup.py 31
}

mediaserver() {
    python3 /root/envsetup/envsetup.py 3
    python3 /root/envsetup/envsetup.py 5
    python3 /root/envsetup/envsetup.py 8
    python3 /root/envsetup/envsetup.py 61
    python3 /root/envsetup/envsetup.py 6
    python3 /root/envsetup/envsetup.py 41
    python3 /root/envsetup/envsetup.py 42
    python3 /root/envsetup/envsetup.py 6
}

worker() {
    python3 /root/envsetup/envsetup.py 62
}

campusmanager() {
    python3 /root/envsetup/envsetup.py 3
    python3 /root/envsetup/envsetup.py 6
    python3 /root/envsetup/envsetup.py 51
    python3 /root/envsetup/envsetup.py 6
}

cache() {
    python3 /root/envsetup/envsetup.py 71
    python3 /root/envsetup/envsetup.py 72
    python3 /root/envsetup/envsetup.py 73
    python3 /root/envsetup/envsetup.py 42
}

tests() {
    python3 /root/envsetup/envsetup.py t
}

backup_server() {
    python3 /root/envsetup/envsetup.py 81
}

reconf_default() {
    python3 /root/envsetup/envsetup.py 104
}

reconf_client() {
    python3 /root/envsetup/envsetup.py 101
}

reconf_recette() {
    python3 /root/envsetup/envsetup.py 102
}

reconf_smtp() {
    python3 /root/envsetup/envsetup.py 103
}

WOWZA_LICENSE() {
    python3 /root/envsetup/envsetup.py 105
}

exportvm() {
    KEY=~/.ssh/ubicast_support
    VM_HYPER=$(grep VM_HYPER ${CONF} | awk -F "=" '{print$2}')
    VM_STORE=$(grep VM_STORE ${CONF} | head -1 | awk -F "=" '{print$2}')
    VM_STORE_LOCAL=$(grep VM_STORE_LOCAL ${CONF} | awk -F "=" '{print$2}')

    if ( test -z $VM_NAME ); then
        VM_NAME=$(cat ${CONF} | egrep ^ETC_HOSTNAME | head -1 | awk -F "=" '{print$2}')
    else
        VM_NAME=$(grep VM ${CONF} | awk -F "=" '{print$2}')
    fi

    ssh -i ${KEY} -o User=root ${VM_HYPER} "VBoxManage export ${VM_NAME} -o ${VM_STORE}/${VM_NAME}.ovf --ovf10"

    # vbox conf file created

    # generating vmware conf file
    cp ${VM_STORE_LOCAL}/${VM_NAME}.ovf ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<vssd:VirtualSystemType>virtualbox-2.2</vssd:VirtualSystemType>@<vssd:VirtualSystemType>vmx-07</vssd:VirtualSystemType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<rasd:Caption>sataController0</rasd:Caption>@<rasd:Caption>SCSIController</rasd:Caption>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<rasd:Description>SATA Controller</rasd:Description>@<rasd:Description>SCSIController</rasd:Description>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<rasd:ElementName>sataController0</rasd:ElementName>@<rasd:ElementName>SCSIController</rasd:ElementName>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<rasd:ResourceSubType>AHCI</rasd:ResourceSubType>@<rasd:ResourceSubType>lsilogic</rasd:ResourceSubType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
    sed -i "s@<rasd:ResourceType>20</rasd:ResourceType>@<rasd:ResourceType>6</rasd:ResourceType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf

    # recherche n° ligne paragraphe à supp.
    LIG=$(grep -n '<rasd:AddressOnParent>3</rasd:AddressOnParent>' ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf | awk -F ":" '{print$1}')
    LIG0=$(( $LIG - 1 ))
    LIG1=$(( $LIG0 + 9 ))
    sed -i "${LIG0},${LIG1}d" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf

    # converting disk to qemu image
    qemu-img convert -c -O qcow2 ${VM_STORE_LOCAL}/${VM_NAME}-disk1.vmdk ${VM_STORE_LOCAL}/${VM_NAME}.qcow2

    echo -e "${CYAN}Files are available at ${VM_STORE_LOCAL}${NC}"
}

exportvm_local(){
VM_STORE=/home/jallary/ubicast/TMP/ENVOI
VM_STORE_LOCAL=/home/jallary/ubicast/TMP/ENVOI
# depuis poste local
VBoxManage export ${VM_NAME} -o ${VM_STORE}/${VM_NAME}.ovf --ovf10

# generating vmware conf file
cp ${VM_STORE_LOCAL}/${VM_NAME}.ovf ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<vssd:VirtualSystemType>virtualbox-2.2</vssd:VirtualSystemType>@<vssd:VirtualSystemType>vmx-07</vssd:VirtualSystemType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<rasd:Caption>sataController0</rasd:Caption>@<rasd:Caption>SCSIController</rasd:Caption>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<rasd:Description>SATA Controller</rasd:Description>@<rasd:Description>SCSIController</rasd:Description>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<rasd:ElementName>sataController0</rasd:ElementName>@<rasd:ElementName>SCSIController</rasd:ElementName>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<rasd:ResourceSubType>AHCI</rasd:ResourceSubType>@<rasd:ResourceSubType>lsilogic</rasd:ResourceSubType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf
sed -i "s@<rasd:ResourceType>20</rasd:ResourceType>@<rasd:ResourceType>6</rasd:ResourceType>@" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf

# recherche n° ligne paragraphe à supp.
LIG=$(grep -n '<rasd:AddressOnParent>3</rasd:AddressOnParent>' ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf | awk -F ":" '{print$1}')
LIG0=$(( $LIG - 1 ))
LIG1=$(( $LIG0 + 9 ))
sed -i "${LIG0},${LIG1}d" ${VM_STORE_LOCAL}/${VM_NAME}_vmware.ovf

# converting disk to qemu image
qemu-img convert -c -O qcow2 ${VM_STORE_LOCAL}/${VM_NAME}-disk1.vmdk ${VM_STORE_LOCAL}/${VM_NAME}.qcow2

echo -e "${CYAN}Files are available at ${VM_STORE_LOCAL}${NC}"
}

case "$1" in
    "ms")
        init
        monitor
        campusmanager
        mediaserver
        tests
    ;;

    "w")
        init
        worker
        tests
    ;;

    "cm")
        init
        monitor
        campusmanager
        tests
    ;;

    "cache")
        init
        cache
    ;;

    "mv")
        init
        backup_server
    ;;

    "reconf")
        reconf_default
    ;;

    "client")
        reconf_client
        reconf_recette
        reconf_smtp
    ;;

    "recette")
        reconf_recette
    ;;

    "wrecette")
        WOWZA_LICENSE
        reconf_recette
    ;;

    "exportvm")
        CONF=$(echo "$2")
        exportvm
    ;;
    
    "exportvm_local")
		VM_NAME=$(echo "$2")
        exportvm_local
    ;;
   

    *)
        echo "
Usage: $0 [arg]
ms          build a MediaServer & CM
w           build a Worker
cm          build a Campus-Manager
cache       build a Cache server (munin needs manual configuration on master AND node)
mv          build a MediaVault

--------------------------------------
reconf      install a tool for post-shipping configuration
client	    deploy client configuration
--------------------------------------
recette     run tests
wrecette    deploy wowza license key and run tests


After usage a log file will be generated under /root/$(date +%F)_envsetup.log

From admin machine :
Usage: $0 exportvm
exportvm        [conf_file]    export VM from an hypervisor
exportvm_local  [vm name]      export VM from local vbox
"
    ;;
esac
