updateTightVNC

By Thomas_H 2021/02/14

#!/bin/bash
#
##############################################################################
# first we declare some variables. A good programmer is a lazy programmer :)
##############################################################################
opsiproduct_path="/home/opsiproducts"                   # as recommended by uib it's always /home/opsiproducts
product="tightvnc"                              # just an example
setupname="tightvnc-gpl-setup-64bit.msi"                # just an example
geturl="https://www.tightvnc.com/download.php"          # move the rest beginning from the last slash to var urladd!
urladd="/download.php"          # minimum a slash / we add this at the end of $geturl
# Attention! following grepvar-string is a regular expression!! Leave the [^"]* at the end!!
grepvar=https://.*/tightvnc-[0-9.]*-gpl-setup-64bit\.msi\"      # A string, the grep should look for in the website. This will be part of the download-url! grep tries to add the version-number
searchstr='/tightvnc-'                  # what's behind the version-number of the downloadfile? We need to exctract the version-number for the config -file
alt_ver=$(date +"%y%m%d")                               # this is a dummy-version. Some programs don't include a version number in their name
startpos=35                                             # at which position is the first digit of the version-number?
##############################################################################
#
# ATTENTION! FOR TESTING FIRST ALL REAL COMMANDS ARE COMMENTED OUT BY # --- !! WHEN CHANGING TO PRODUCTION YOU'LL HAVE TO REMOVE THE # --- THEN!
#
# first, let's remove the last valid exe-file
rm $opsiproduct_path/$product/CLIENT_DATA/$setupname

# get the new setup-file from website and rename it to a standard file name (so no need to change the setup.ins :P )

if [ "$grepvar" == "" ]; then
    setupprg=$setupname
else
# here we try to find the full name of the new setup-file. For example we know start and ending of the file, but not the rest.
# Example: Setup-file-name is always "freecommanderxe-<version>_setup.exe" we try to find out, what is <version>. grep will find it, so we have a
# "freecommander-791_setup.exe". Yay. This will be added to the download-link.
#
    setupprg=$( wget --no-check-certificate $geturl$urladd -q -O- | grep -om1 $grepvar | sed 's/"//g' ) # we have added a sed to delete the " at the end.
fi

echo setupprg=$setupprg
wget --no-check-certificate "$setupprg" -O $opsiproduct_path/$product/CLIENT_DATA/$setupname

# Do we need to unzip the file first? If so, watch the right name of setup.exe is written in setup.ins
# --- unzip -o $opsiproduct_path/$product/CLIENT_DATA/$setupname  # -o -overwrite files without prompting!
# --- rm  $opsiproduct_path/$product/CLIENT_DATA/$setupname

# let's change the directory to the package
cd $opsiproduct_path/$product/
# remove the *.opsi-files
rm *.opsi
rm *.zsync
rm *.md5

# to find out, which version it is, we do a little bit of awk
if [ $startpos != 0 ]; then
    pos=$(echo $setupprg | grep -b -o $searchstr | awk 'BEGIN {FS=":"}{print $1}')
    echo pos=$pos
    # well done, we found it.
    version=$(echo $setupprg | cut -c$startpos-$pos)
else
    version=$alt_ver
fi
echo Version=$version
# now let's change the /OPSI/control file
sed "s/version:.*/version: $version /g" $opsiproduct_path/$product/OPSI/control > $opsiproduct_path/$product/OPSI/control.new
mv $opsiproduct_path/$product/OPSI/control.new $opsiproduct_path/$product/OPSI/control

# make new package, but quiet
opsi-makepackage -q
opsi-package-manager -i *.opsi