#!/bin/sh
VERSION=260517
ESCSCRIPT=$(readlink -f $0)
ESCSCRIPTNAME=$(basename $ESCSCRIPT)
ESCSCRIPTDIR=$(dirname $(readlink -f $0))
BASEDIR=$(dirname $ESCSCRIPTDIR)
LOGDIR=$ESCSCRIPTDIR/logs
if [ "$1" = "-v" ] ; then echo "$VERSION $ESCSCRIPTNAME $ESCSCRIPTDIR";exit 0; fi
# todo wrap in a function
hash esxcli >/dev/null 2>&1
if [ $? != 0 ] ; then echo "this is not an esxi system"; exit 1; fi
#esxcli system version get | grep Version | grep 8 >/dev/null
#if [ $? != 0 ] ; then echo "this is not an esxi 8 system"; exit 1; fi

if [ ! -e "$LOGDIR" ] ; then mkdir "$LOGDIR"; fi

# a pipe separated list of vm names or partial names, pay attention to matching, verify with ./list.sh show
#excludes=""
#bkpdir=$(echo "$VM_BACKUP_VOLUME" | cut -d"=" -f2)
#bkpname=$(echo "$bkpdir" | cut -d"/" -f4)

function ts(){ ts=$(date "+%Y%m%d-%H%M%S"); }

function cmd(){
        ts
        echo -n "$ts $SCRIPTNAME cmd> $@ <rc " >> $LOG
        #echo "$ts $SCRIPTNAME cmd> $@" | tee -a $LOG
        eval "$@"
        rc=$?
        echo $rc >> $LOG
        return $rc
}

# EsxiVersionInfos - get detailed versions infos
function evi(){
	evi=$(esxcli system version get)
	#echo "$evi"
	epro=$(echo "$evi"| grep "Product" | awk {'print $2"-"$3'})
	ever=$(echo "$evi"| grep "Version" | awk {'print $2'})
	evst=${ever:0:1};evnd=${ever:2:1};evrd=${ever:4:1};
	ebas=${ever:0:1}
	ebld=$(echo "$evi"| grep "Build" | awk {'print $2'} | cut -d"-" -f2)
	eupd=$(echo "$evi"| grep "Update" | awk {'print $2'})
	epat=$(echo "$evi"| grep "Patch" | awk {'print $2'})
	#echo "product:epro:$epro main:ebas:$ebas version:ever:$ever build:ebld:$ebld update:eupd:$eupd patch:epat:$epat evst:$evst evnd:$evnd evrd:$evrd"
}

function vmlist(){
        if [ -z "$1" ] ; then cmd=all; else cmd=$1; fi
        #echo command:$cmd
        allvmsids=$(vim-cmd vmsvc/getallvms | awk '$3 ~ /^\[/ {print $1"|"$2}')
        #echo "$allvmsids"
        for e in $(echo "$allvmsids") ; do
                #echo $e
                id=$(echo $e | cut -d"|" -f1)
                name=$(echo $e | cut -d"|" -f2)
                if [ "$cmd" == "all" ] ; then echo "id:$id name:$name"; fi
                if [ "$cmd" == "ids" ] ; then echo "$id"; fi
                if [ "$cmd" == "vms" ] ; then echo "$name"; fi
        done
}

if [ "$1" == "jobs" ] ; then
	# TODO maybe integrate with persist-esc.py ?
	echo "doing your custom jobs/tasks..."
	# this will add a cronjob which starts a specific vm all x minutes
	# define details like times with "cronjob.sh edit" and afterwards "cronjob.sh restart" to make them active
	#$SCRIPTDIR/cronjob.sh new 'vim-cmd vmsvc/power.on $(vim-cmd vmsvc/getallvms | grep "vm-name" | awk {"print $1"})'
fi

# import and overwrite vars with your own local settings which are update safe
if [ -e "$SCRIPTDIR/settings.local" ] ; then
        source "$SCRIPTDIR/settings.local";
else
cat << EOF > $SCRIPTDIR/settings.local
#!/bin/sh
#
# do your custom settings here to overwrite settings from esc.sh
# this file will not be touched during updates
#
# used by: cronjob.sh
host="example.settings.local"
# used by: list.sh
# a pipe separated list of vm names or partial names, pay attention to matching then
excludes="vpn.test|anyname"
# set your backup target directory here, this will be used for ghetto.local
bkpdir=/vmfs/volumes/disk-nfs/backups
bkpname=\$(echo "\$bkpdir" | cut -d"/" -f4)
EOF
fi


# gehttoVCB  settings ; pay attention the vars are predefined in the ghetto scripts, do not use same names
# if a local ghetto config already exists, use it
if [ -e "$SCRIPTDIR/ghetto.local" ] ; then
        source "$SCRIPTDIR/ghetto.local";
else
        echo "VM_BACKUP_VOLUME=$bkpdir" >> "$SCRIPTDIR/ghetto.local"
        echo "VM_BACKUP_ROTATION_COUNT=5" >> "$SCRIPTDIR/ghetto.local"
        echo "RSYNC_LINK=1" >> "$SCRIPTDIR/ghetto.local"
fi

# to prevent some vars beeing filled by processes and consuming time, just do it if not defined otherwise alread

# define my hostname ; default read from system
if [ -z "$host" ] ; then host=$(hostname -s); fi

