#!/bin/sh
# esxcli network firewall ruleset set -e true -r httpClient
# wget "https://svzone.de/files/esxi/scripts/!get.sh" --no-check-certificate -O !get.sh; chmod +x !get.sh ;./!get.sh
#if [ -e "!get.sh" ] ; then sig="459cee3b63c7d51ef88f17b3"; if [ "$sig" != $(tail -n+5 \!get.sh | sha256sum | awk {'print $1'} | awk {'print substr($1,0,24)'}) ] ; then echo "integrity check failed! (sig:$sig)"; exit 1; fi; fi
uri="https://svzone.de/files/esxi/scripts"
port=443
#
# how to install on esxi node:
#
# open firewall for outgoing traffic first, then load the script referencing all scripts/files and download them somewhere to your datastore:
# esxcli network firewall ruleset set -e true -r httpClient
# wget "$uri/!get.sh" --no-check-certificate ; chmod +x !get.sh ; ./!get.sh 
#
VERSION=260521
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
BASEDIR=$(dirname $SCRIPTDIR)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log
list(){
	if [ "$1" == "gen" ] ; then if [ -e "$BASEDIR/packages/esc.html" ] ; then rm -f $BASEDIR/packages/esc.html; fi; fi;
	for file in $(ls -1 *.sh | grep -vE "ghettoVCB*"); do
                #echo $file;
		verinfo=$(sh $SCRIPTDIR/$file -v | cut -d" " -f1-2);
		if [ "$1" == "gen" ] ; then echo "$verinfo" | tee -a $BASEDIR/packages/esc.html; else echo "$verinfo"; fi
        done
}
## check for esxcli and  enable http outgoing communication
if hash esxcli >/dev/null 2>&1 ; then continue; else echo "[ERROR] this is not an esxi system"; exit 1; fi
if [ $(esxcli network firewall ruleset list -r httpClient | grep httpClient | awk {'print $2'}) = "false" ] ; then
	esxcli network firewall ruleset set -e true -r httpClient
	# for scp/ssh
	#esxcli network firewall ruleset set --ruleset-id sshClient --enabled=true
fi
cd $SCRIPTDIR
cwdcheck(){ if [ $(echo "$SCRIPTDIR" | cut -d"/" -f1-3) != "/vmfs/volumes" ] ; then echo "wrong location, not beneath /vmfs/volumes";exit 1; fi }
destcheck(){ server=$(echo "$uri" | cut -d"/" -f3);  nc -z $server $port >/dev/null 2>&1; if [ $? != 0 ] ; then echo "$server unreachable via $port"; exit 1; fi }
# if website has no valid cert or unknown ca in your esxi version, you need no-check-certificate as param, otherwise remove
params="--no-check-certificate -q"
files="bkp-run.sh bkp-space.sh cronjob.sh esc.sh esxi-backup.sh esxi-config.sh esxi-firmware.sh esxi-upgrade.sh \
	esxi-repo.sh esxi-sboot.sh ghetto-abort.sh test-storage.sh list-restore.sh list.sh"
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then 
	echo "$SCRIPTNAME $version"; 
	echo "self	- updates this script itself"; 
	echo "update	- updates the script collection"; 
	echo "check	- compare local scripts vs. repository versions"
	echo "ghetto	- download ghetto scripts from github"
	exit 0; 
fi
if [ "$1" = "self" ] ; then
	cwdcheck
	echo -n "my-location:$SCRIPTDIR self-update: "
	destcheck
	#server=$(echo "$uri" | cut -d"/" -f3)
        #nc -z $server $port >/dev/null 2>&1
        #if [ $? != 0 ] ; then echo "$server unreachable via $port"; exit 1; fi
	cp -p "$SCRIPTDIR/!get.sh" "$SCRIPTDIR/!get.sh.bkp"
	if [ $? == 0 ] ; then rm "$SCRIPTDIR/!get.sh"; fi
	wget -q $uri/!get.sh $params
	rc=$?
	if [ -e "$SCRIPTDIR/!get.sh" ] ; then chmod +x "$SCRIPTDIR/!get.sh"; fi
	if [ "$rc" == 0 ] ; then echo "[OK]"; exit 0; else echo "[ERROR]"; exit 1; fi
fi
if [ "$1" = "check" ] ; then
	cmd="wget https://svzone.de/files/esxi/packages/esc.html $params -O $SCRIPTDIR/packages/esc.html"
        #echo "$cmd"
	echo "updating esc repository..."
	IFS=$'\n'
	for entry in $(list); do 
		locvers=$(echo "$entry" | cut -d" " -f1)
		locfile=$(echo "$entry" | cut -d" " -f2)
		repo=$(grep $locfile $SCRIPTDIR/packages/esc.html)
		repvers=$(echo "$repo" | cut -d" " -f1)
		repfile=$(echo "$repo" | cut -d" " -f2)
		if [ $repvers -gt $locvers ] ; then update="[YES]"; else update=no; fi
		echo "locfile:$locfile locver:$locvers repver:$repvers updateable:$update"
		#grep $file $SCRIPTDIR/packages/esc.html
	done
	unset IFS
	exit
fi
if [ "$1" = "update" ] ; then
	cwdcheck
        echo "doing update..."
	for file in $files; do
		if [ -n "$2" ] ; then 
			if [ "$2" != "$file" ]; then continue; else 
				echo -n "updating only $file: ";
				wget $uri/$file $params -O $file
				if [ $? == 0 ] ; then echo "[OK]"; else echo "[ERROR]"; fi
				exit
			fi; 
		fi
		echo -n "$file: "
		cmd="wget $uri/$file $params -O $file"
		#echo "$cmd"
		eval "$cmd"
		if [ $? == 0 ] ; then echo "[OK]"; else echo "[ERROR]"; fi 
	done
	exit
#	find $SCRIPTDIR -maxdepth 1 -type f ! -name "!*" ! -name "*.bkp" ! -name "*local*" ! -name "*.log" ! -name "ghettoVCB*" ! -name "*.vms" -print
fi
if [ "$1" == "ghetto" ] ; then
	#echo -n "downloading ghetto scripts: "
	#wget $uri/ghettoVCB-restore.sh $params -O "$SCRIPTDIR/ghettoVCB-restore.sh"
	#echo -n $?
	#wget $uri/ghettoVCB.sh $params -O "$SCRIPTDIR/ghettoVCB.sh"
	#echo -n $?
	#exit
	echo -n "downloading ghetto scripts from github: "
	wget https://raw.githubusercontent.com/lamw/ghettoVCB/master/ghettoVCB-restore.sh $params -O ghettoVCB-restore.sh
	echo -n $?
	wget https://raw.githubusercontent.com/lamw/ghettoVCB/master/ghettoVCB.sh $params -O ghettoVCB.sh
	echo $?
	exit
fi
echo "downloading files ..."
destcheck
for file in $files; do
	echo -n "downloading $file: "
	cmd="wget $uri/$file $params -O $file"
	#echo "$cmd"
	eval "$cmd"
	if [ $? == 0 ] ; then echo "[OK]"; else echo "[ERROR]"; fi
done
wget $uri/!get.sh $params
wget $uri/autostart.sh $params
wget $uri/ghettoVCB-restore.sh $params
wget $uri/ghettoVCB.sh $params
wget $uri/settings.local $params
echo
echo "doing preparation tasks..."
# make them executable
chmod +x *.sh
# check for default sub directories
if [ ! -e "$SCRIPTDIR/vmconfigs/!TEMPLATE" ] ; then 
	if [ ! -e "$SCRIPTDIR/vmconfigs" ]; then mkdir "$SCRIPTDIR/vmconfigs"; fi
	echo "#VMDK_FILES_TO_BACKUP=\"disk1.vmdk,/vmfs/volumes/6471ef6a-051a67bb-ca46-3cecef954948/name/name/disk2.vmdk\"" > "$SCRIPTDIR/vmconfigs/!TEMPLATE"
	echo "#power off is required e.g. if phy. devices like usb drives or gpu are passed through" >> "$SCRIPTDIR/vmconfigs/!TEMPLATE"
	echo "#POWER_VM_DOWN_BEFORE_BACKUP=1" >> "$SCRIPTDIR/vmconfigs/!TEMPLATE"
fi
if [ ! -e "$SCRIPTDIR/logs" ] ; then mkdir "$SCRIPTDIR/logs"; fi
exit
