#!/bin/sh
#
# 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 "https://svzone.de/files/srv/scripts/esxi-packages/repo.sh" --no-check-certificate ; chmod +x repo.sh ; ./repo.sh
#
VERSION=260515
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log

uri="https://svzone.de/files/esxi/packages"
port=443
## check for esxcli and  enable http outgoing communication
if [ $(hash esxcli >/dev/null 2>&1) ] && [ $(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
# 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"
#params=""
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
        echo "$SCRIPTNAME $version";
        echo "self      - update this script";
        echo "update    - update script collection";
        exit 0;
fi
if [ "$1" = "self" ] ; then
        echo "my-location:$SCRIPTDIR"
        cp -p "$SCRIPTDIR/!get.sh" "$SCRIPTDIR/!get.sh.bkp"
        if [ $? == 0 ] ; then rm "$SCRIPTDIR/!get.sh"; fi
        wget https://svzone.de/files/srv/scripts/esxi/!get.sh $params
        if [ -e "$SCRIPTDIR/!get.sh" ] ; then chmod +x !get.sh; echo $?; fi
        exit $?
fi
if [ "$1" = "update" ] ; then
	# better use wget -O $file to overwrite
        echo "doing update..."
        # if an update is requested, delete the files and download them again
        # exceptions are files beginning with ! , containing local , ending on .log , starting with ghettoVCB , ending on .vms
        #find . -type f ! -name "!*" ! -name "*local*" ! -name "*.log" ! -name "ghettoVCB*" ! -name "*.vms" -exec rm -rf {} \;
        echo $?
fi
# check for default sub directories
if [ ! -e "$SCRIPTDIR/packages" ] ; then
        if [ ! -e "$SCRIPTDIR/packages" ]; then mkdir "$SCRIPTDIR/packages"; fi
        echo "created packages directory"
fi
wget "$uri/repo.sh" $params -O "$SCRIPTDIR/packages/repo.sh"
chmod +x "$SCRIPTDIR/packages/repo.sh"
cd "$SCRIPTDIR/packages"
$SCRIPTDIR/packages/repo.sh
exit
