#!/usr/local/bin/bash

#-----------------------------------------------------------------------
# Copyright (c) 2019-2024 Alexander Vereeken. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This file is a part of Mizutamari - Wine front-end for FreeBSD

Game=$(grep Game $LDIR/$APPNAME | cut -f2 -d"=")

Program=$(grep Program $LDIR/$APPNAME | cut -f2 -d"=")

DependsOn=$(grep DependsOn $LDIR/$APPNAME | cut -f2 -d"=")

RecommendedWine=$(grep RecommendedWine $LDIR/$APPNAME | cut -f2 -d"=")

NoWoW64=$(grep NoWoW64 $LDIR/$APPNAME | cut -f2 -d"=")

UserDir=$(grep UserDir $LDIR/$APPNAME | cut -f2 -d"=")

if [[ $Game == *"Yes"* ]]; then
if [[ $MetacityCompoSwitch == *"On"* ]]; then
metacity --compositor none
fi
if [[ $MATECompoSwitch == *"On"* ]]; then
gsettings set org.mate.Marco.general compositing-manager false
fi
if [[ $XFWMCompoSwitch == *"On"* ]]; then
xfconf-query -c xfwm4 -p /general/use_compositing -s false
fi
if [[ $KDECompoSwitch == *"On"* ]]; then
qdbus-qt5 org.kde.KWin /Compositor suspend
fi
fi

if [[ -z $Game &&  -z $Program ]]; then
echo -e "\e[3;5;0;31mThe application $APPNAME doesnt exist or the application type have been not set.\e[30;48;5;82m\e[0m"
exit
fi

if [[ $NoWoW64 == *"Yes"* ]]; then
export WINE_NO_WOW64=1
fi

if [[ $TODO == *"Installation"* ]]; then

DownloadSite=$(grep DownloadSite $LDIR/$APPNAME | cut -f2 -d"=")

DownloadFile=$(grep DownloadFile $LDIR/$APPNAME | cut -f2 -d"=")

Winetricks=$(grep Winetricks $LDIR/$APPNAME | cut -f2- -d"=")

DXVK=$(grep DXVK $LDIR/$APPNAME | cut -f2 -d"=")

VKD3D_Proton=$(grep VKD3D_Proton $LDIR/$APPNAME | cut -f2 -d"=")

ENVSETUP=$(grep ENVSETUP $LDIR/$APPNAME | cut -f2- -d"=")

ARGSETUP=$(grep ARGSETUP $LDIR/$APPNAME | cut -f2 -d"=")

NoSetup=$(grep NoSetup $LDIR/$APPNAME | cut -f2 -d"=")

NoDownload=$(grep NoDownload $LDIR/$APPNAME | cut -f2 -d"=")

Extract=$(grep Extract $LDIR/$APPNAME | cut -f2 -d"=")

ExtractTo=$(grep ExtractTo $LDIR/$APPNAME | cut -f2 -d"=")

Broken=$(grep Broken $LDIR/$APPNAME | cut -f2 -d"=")

Broken_Reason=$(grep Broken_Reason $LDIR/$APPNAME | cut -f2 -d"=")

SetupFile=$(grep SetupFile $LDIR/$APPNAME | cut -f2 -d"=")

Move=$(grep Move $LDIR/$APPNAME | cut -f2 -d"=")

SHA256=$(grep SHA256 $LDIR/$APPNAME | cut -f2 -d"=")

if [[ -z $SHA256 ]]; then
echo "# Installation has been cancelled."
warn "Please provide SHA256 to verify integrity!"
exit
fi

if [[ $Broken == *"Yes"* ]]; then
echo "# Installation has been cancelled."
info "Sorry but $APPNAME was marked as broken.

Reason: $Broken_Reason"
exit
fi

if [ -z "$RecommendedWine" ]
then
true
else
info "The maintainer recommends to use $RecommendedWine, so if have issues change to this instead."
fi

if [ -z "$DependsOn" ]
then
true
else
if [ -d "$ProjectPath/$DependsOn" ]; then
echo "$DependsOn is installed good.."
else
info "Please install $ProjectPath/$DependsOn first"
exit
fi
fi

echo 20
echo "# Downloading $DownloadFile"

if [[ -z $DownloadFile ]];
then
if [[ $NoDownload == *"Yes"* ]]; then
echo "No downloadfile in use"
else
warn "No file to download was set, setup will be aborted."
exit
fi
else
if [[ -v DownloadSite ]];
then
echo -e "\e[3;5;0;36mDownload Site is $DownloadSite\e[30;48;5;82m\e[0m"
else
if [[ $NoDownload == *"Yes"* ]]; then
echo "No downloadsite in use"
else
warn "No downloadsite was set, setup will be aborted."
exit
fi
fi
echo -e "\e[3;5;0;33mDownload File is $DownloadFile\e[30;48;5;82m\e[0m"
if [ -f "$CDIR/$DownloadFile" ]; then
echo -e "\e[3;5;0;32m$DownloadFile already exists, skipping download.\e[30;48;5;82m\e[0m"
echo 25
echo "# Verify integrity"
CHECKSUM=$(sha256sum --quiet $CDIR/$DownloadFile)
if [[ $CHECKSUM == *"$SHA256"* ]]; then
echo "Your file is save!"
else
FALSESUM=$(question "$DownloadFile did not pass the integrity check. Please try a library update. If it doesn't help, please inform the maintainer so that security can be guaranteed.

Possible reasons: $DownloadFile got damaged/modified/rerolled/updated... do you want to continue anyway?")
if [[ $FALSESUM == *"0"* ]]; then
true
fi
if [[ $FALSESUM == *"1"* ]]; then
echo "# Installation was cancelled."
exit
fi
fi
else
download $DownloadSite$DownloadFile
echo 25
echo "# Verify integrity"
CHECKSUM=$(sha256sum --quiet $CDIR/$DownloadFile)
if [[ $CHECKSUM == *"$SHA256"* ]]; then
echo "Your file is save!"
else
FALSESUM=$(question "$DownloadFile did not pass the integrity check. Please try a library update. If it doesn't help, please inform the maintainer so that security can be guaranteed.

Possible reasons: $DownloadFile got damaged/modified/rerolled/updated... do you want to continue anyway?")
if [[ $FALSESUM == *"0"* ]]; then
true
fi
if [[ $FALSESUM == *"1"* ]]; then
echo "# Installation was cancelled."
exit
fi
fi
fi
fi

if [ -z "$Winetricks" ]
then
echo "No Wintricks in use"
else
echo 50
echo "# Running winetricks to install/set: $Winetricks"
winetricks $Winetricks
fi

if [[ $DXVK == *"Yes"* ]]; then
echo 70
echo "# Setting up DXVK"
DXVK=$(question "Do you want to install and enable DXVK? [DX8-11 over Vulkan]")
if [[ $DXVK == *"0"* ]]; then
DXVK_VER=$(zenity --window-icon=$ICO --entry --title="$NAME - Setting up DXVK" --text="Enter the version of DXVK that you want to use, possible values: 0054, 1092, 2030 or leave it empty to use the latest version." --entry-text "")
winetricks dxvk$DXVK_VER
fi
if [[ $DXVK == *"1"* ]]; then
echo -e "\e[3;5;0;31mUser have decided to not install and enable DXVK\e[30;48;5;82m\e[0m"
fi
fi

if [[ $VKD3D_Proton == *"Yes"* ]]; then
echo 75
echo "# Setting up VKD3D_Proton"
VKD3D_Proton=$(question "Do you want to install and enable VKD3D_Proton? [DX12 over Vulkan - Proton version]")
if [[ $VKD3D_Proton == *"0"* ]]; then
winetricks vkd3d
fi
if [[ $VKD3D_Proton == *"1"* ]]; then
echo -e "\e[3;5;0;31mUser have decided to not install and enable VKD3D_Proton\e[30;48;5;82m\e[0m"
fi
fi

echo 80
echo "# Preparing setup"

if [[ $NoSetup == *"Yes"* ]]; then
mkdir $APPDIR
fi

if [ -f "$LDIR/Scripts/$APPNAME-Setup" ]; then
bash $LDIR/Scripts/$APPNAME-Setup
fi

echo 90
echo "# Running installation"

if [[ $Extract == *"Yes"* ]]; then
if [ -z "$ExtractTo" ]
then
cd "$APPDIR" && 7zz x -y $CDIR/$DownloadFile
else
cd "$ProjectPath/$ExtractTo" && 7zz x -y $CDIR/$DownloadFile
fi
fi

if [[ $NoSetup == *"Yes"* ]]; then
true
else
if [ -z "$SetupFile" ]
then
env $ENVSETUP $WINEBIN "$CDIR/$DownloadFile" $ARGSETUP
else
env $ENVSETUP $WINEBIN "$ProjectPath/$SetupFile" $ARGSETUP
fi
fi

if [[ $Move == *"Yes"* ]]; then
mv $CDIR/$DownloadFile $APPDIR
fi

if [ -f "$LDIR/Scripts/$APPNAME-Final" ]; then
bash $LDIR/Scripts/$APPNAME-Final
fi
echo 100
echo "# Installation is done!"
fi

if [[ $TODO == *"Launcher"* ]]; then

cd "$APPDIR"

EXE=$(grep EXE $LDIR/$APPNAME | cut -f2 -d"=")

ENVS=$(grep ENVS $LDIR/$APPNAME | cut -f2- -d"=")

ARGS=$(grep ARGS $LDIR/$APPNAME | cut -f2 -d"=")

WRKDIR=$(grep WRKDIR $LDIR/$APPNAME | cut -f2 -d"=")

DXVK=$(grep DXVK $LDIR/$APPNAME | cut -f2 -d"=")

VKD3D_Proton=$(grep VKD3D_Proton $LDIR/$APPNAME | cut -f2 -d"=")

if [[ -v WRKDIR ]];
then
cd "$APPDIR/$WRKDIR"
fi

if [[ $DXVK == *"Yes"* ]]; then
export DXVK_STATE_CACHE_PATH=$CDIR
fi

if [[ $VKD3D_Proton == *"Yes"* ]]; then
export VKD3D_SHADER_CACHE_PATH=$CDIR
fi

if [[ -v EXE ]];
then
if [[ $UserDir == *"Yes"* ]]; then
CheckEXE="$APPDIR/drive_c/users/$USER/$EXE"
else
CheckEXE="$APPDIR/$EXE"
fi
if [ ! -f "$CheckEXE" ]; then
warn "No launch executable has been found, the installation may not have been successful or the executable path has not been updated!"
exit
fi
echo -e "\e[3;5;0;34mStarting now: $APPNAME"
if [[ $UserDir == *"Yes"* ]]; then
env $ENVS $WINEBIN "$APPDIR/drive_c/users/$USER/$EXE" $ARGS
else
env $ENVS $WINEBIN "$APPDIR/$EXE" $ARGS
fi
else
warn "No executable to launch was set, action will be aborted."
exit
fi
fi

if [[ $TODO == *"Uninstallation"* ]]; then
WINEPREFIX=$APPDIR $DefaultWinePath/wineserver -k
remove $APPDIR
fi

if [[ $Game == *"Yes"* ]]; then
if [[ $MetacityCompoSwitch == *"On"* ]]; then
metacity --compositor xrender
fi
if [[ $MATECompoSwitch == *"On"* ]]; then
gsettings set org.mate.Marco.general compositing-manager true
fi
if [[ $XFWMCompoSwitch == *"On"* ]]; then
xfconf-query -c xfwm4 -p /general/use_compositing -s true
fi
if [[ $KDECompoSwitch == *"On"* ]]; then
qdbus-qt5 org.kde.KWin /Compositor resume
fi
fi