#!/bin/bash


# THIS SCRIPT RELEASED UNDER GPL-3.0 LICENSE -> https://www.gnu.org/licenses/gpl-3.0.txt
# WRITTEN BY GIORDANO BOSCHETTI FROM GEEKOS DAW, AN GEEKOS ITALIA PROJECT
# DEPENDENCIES: mawk kdialog unzip
# UNDER OPENSUSE:
# sudo zypper in mawk kdialog unzip

export TEXTDOMAIN=airwindowsmanager

#VAR
link="http://www.airwindows.com/wp-content/uploads/LinuxVSTs.zip" #airwindowszip
filecheck=$HOME/.vst/airwindows/updatecheck #checkfile

#FIRST DIALOG
if [ -f "$filecheck" ]; then
choice=$(kdialog --combobox $"AirWindows VST's are installed, choose an option:" $"CHECK UPDATES" $"UNINSTALL" --default $"CHECK UPDATES");
if [ "$?" = 1 ]; then
exit
fi
if [ "$choice" = UNINSTALL ]; then
kdialog --title "AirWindowsManager" --yesno $"All files in $HOME/.vst/airwindows will be removed.\n\n Do you wish to continue?"
if [ "$?" = 1 ]; then
exit
fi
rm -rf $HOME/.vst/airwindows
kdialog --title "AirWindowsManager" --msgbox $"All AirWindows plugins have been removed"
exit
fi
else
kdialog --title "AirWindowsManager" --yesno $"This Script Install all AirWindows\n VST Plugins.\n\n Do you want continue? "
if [ "$?" = 1 ]; then
exit
fi
fi

#UPDATECHECK
rm /tmp/updatecheck
curl --head --silent --location  "$link" | grep -i "content-length:" | tr -d " \t" | cut -d ':' -f 2 > /tmp/updatecheck
chk1=`cksum /tmp/updatecheck | awk -F" " '{print $1}'`
chk2=`cksum $HOME/.vst/airwindows/updatecheck | awk -F" " '{print $1}'`


if [ -f "$filecheck" ]; then
if [ $chk1 -eq $chk2 ]; then
  kdialog --title "AirWindowsManager" --yesno $"No update found, do you want to reinstall?"
  if [ "$?" = 1 ]; then
  exit
  fi
else
  kdialog --title "AirWindowsManager" --yesno $"Update found! do you want to install the update?"
  if [ "$?" = 1 ]; then
  exit
  fi
fi
fi

#LICENSE HEADER
rm /tmp/LICENSE*
wget https://raw.githubusercontent.com/airwindows/airwindows/master/LICENSE -P /tmp
kdialog --title "AirWindowsManager" --textbox /tmp/LICENSE 709 512
#PROGRESS DIALOG AND DOWNLOAD
FILE=/tmp/LinuxVSTs.zip
if [ -f "$FILE" ]; then
rm /tmp/LinuxVSTs.zip*
rm -r /tmp/airwindows
fi 
a=$(kdialog --title "AirWindowsManager" --progressbar $"Download AirWindows Plugins... " 100);sleep 2
qdbus-qt5 $a  showCancelButton true
while read line ;do
read -r p t <<< "$line"
echo $p 
echo $t 
qdbus-qt5 $a Set org.kde.kdialog.ProgressDialog value $p
while [[  $(qdbus-qt5  $a wasCancelled) != "false" ]] ; do
echo "KILLING THE PROCESS AND CLEAN TMP"
rm -r /tmp/airwindows
rm /tmp/LinuxVSTs.zip*
rm /tmp/updatecheck
rm /tmp/LICENSE*
exit
qdbus-qt5 $a  org.kde.kdialog.ProgressDialog.close 
done
qdbus-qt5 $a org.kde.kdialog.ProgressDialog.setLabelText $"Downloading AirWindows Plugins." 
done< <(wget "$link" -P /tmp 2>&1 |mawk -W interactive '{ gsub(/\%/," "); print int($7)" "$9 }')
qdbus-qt5 $a  org.kde.kdialog.ProgressDialog.close

#INSTALL
cd /tmp
unzip LinuxVSTs.zip -d /tmp/airwindows
mkdir -p $HOME/.vst/airwindows
cp /tmp/airwindows/LinuxVSTs/* $HOME/.vst/airwindows
cp /tmp/airwindows/LinuxVSTs/what.txt $HOME/.vst/airwindows/
cp /tmp/updatecheck $HOME/.vst/airwindows/
cp /tmp/LICENSE $HOME/.vst/airwindows
rm -r /tmp/airwindows
rm /tmp/LinuxVSTs.zip*
rm /tmp/updatecheck
rm /tmp/LICENSE
kdialog --title "AirWindowsManager" --msgbox $"AirWindows VSTs Installed on $HOME/.vst/airwindows/"
kdialog --title "AirWindowsManager" --textbox $HOME/.vst/airwindows/what.txt 845 512
