#!/bin/sh
# you can call this script on an esxi system on cli directly with below line
# it will check your version and download the appropriate update package
#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

# this script prepares the repo: it creates all directories of existing packages and downloads it's install script
# each install script can download all neccessary files itself , or you use the "full" parameter to download the complete repo

# generic
version=260515
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
BASEDIR=$(dirname $SCRIPTDIR)
LOG=$LOGDIR/log-$SCRIPTNAME.log
ts=$(date "+%Y%d%m-%H%M%S")
wgetparms="--no-check-certificate -q"
uri="https://svzone.de/files/esxi/packages"
port=443
# check if it is an esxi system
#hash esxcli >/dev/null 2>&1
#if [ $? != 0 ] ; then echo "this is not an esxi system"; exit 1; fi

#specific
mode=basic
if [ "$1" == "full" ] ; then mode=full; fi

#esxiver=$(vmware -l | grep -e "Update" | awk {'print $3'})
#esxivos=${esxiver:0:1}
#esxilvl=$(vmware -l | grep -e "Update" | awk {'print $5'})
# 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"
}

echo "downloading repository index..."
#if [ -e "$SCRIPTDIR/repo.html" ] ; then echo "deleting existing index"; rm "$SCRIPTDIR/repo.html"; fi
wget "$uri/repo.html" $wgetparms -O "$SCRIPTDIR/repo.html"

#for e in $(grep href index.html | cut -d"<" -f7- | cut -d">" -f2 | tail -n+3 | cut -d"/" -f1 | grep -v "repo.sh")
for e in $(cat repo.html)
do
        #echo "entry:$e"
        echo "$e" | grep -q "dir:"
        if [ $? == 0 ] ; then
                d=$(echo "$e" | grep "dir:" | cut -d":" -f2)
                echo "dir:$d"
                if [ ! -e "$SCRIPTDIR/$d" ] ; then
                        mkdir "$SCRIPTDIR/$d";
                        echo "created directory:$d"
                fi
                echo "$e" | grep -q "link:" | grep "install.sh"
                #wget "$link" -P "$SCRIPTDIR/$d" $wgetparms
        fi
        link=$(echo "$e" | grep "install.sh")
        if [ -n "$link" ] ; then
                echo "downloading $d/install.sh and make it executable"
                url=$(echo "$link" |  cut -d":" -f2-)
                wget "$url" -P "$SCRIPTDIR/$d" $wgetparms
                chmod +x $SCRIPTDIR/$d/install.sh
        fi

        if [ "$mode" == "basic" ] ; then continue; fi

        echo "$e" | grep -q "link:"
        if [ $? == 0 ] ; then
                link=$(echo "$e" | grep "link:" | cut -d":" -f2-)
                t=$(echo "link:$link" | grep -o "$d/.*")
                ext=${t: -2}
                echo "downloading:$t"
                wget "$link" -P "$SCRIPTDIR/$d" $wgetparms
                if [ "$ext" == "sh" ] ; then echo "making $t executable..."; chmod +x $SCRIPTDIR/$t; fi
        fi
done

# there is some bug with openssl wget calls which start processes which consumes a lot cpu
pkill openssl
exit
