#!/usr/local/bin/bash

# -----------------------------------------------------------------------
# Copyright (c) 2019-2025 Alexander Vereeken <Alexander88207>. 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 - User-friendly Wine front-end.

# -----------------------------------------------------------------------

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"=")

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

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

VKD3D_Proton=$(grep VKD3D_Proton $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"=")

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"=")

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

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

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

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

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

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

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

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

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

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

function DisableDLL {
  $WINEBIN reg.exe ADD "HKEY_CURRENT_USER\Software\Wine$2\DllOverrides" /v "$1" /t "REG_SZ" /d "" /f
}

function BuiltinDLL {
  $WINEBIN reg.exe ADD "HKEY_CURRENT_USER\Software\Wine$2\DllOverrides" /v "$1" /t "REG_SZ" /d "builtin" /f
}

function NativeDLL {
  $WINEBIN reg.exe ADD "HKEY_CURRENT_USER\Software\Wine$2\DllOverrides" /v "$1" /t "REG_SZ" /d "native" /f
}

function NativeBuiltinDLL {
  $WINEBIN reg.exe ADD "HKEY_CURRENT_USER\Software\Wine$2\DllOverrides" /v "$1" /t "REG_SZ" /d "native,builtin" /f
}

function RMDLL32 {
  rm $APPDIR/drive_c/windows/syswow64/$1
}

function RMDLL64 {
  rm $APPDIR/drive_c/windows/system32/$1
}

IQ=$(yad --question --width=800 --window-icon=$ICO --title "$NAME - $TODO" --text  "You are now in the process of installing the following:

Name:			$APPNAME
Description: 	$APPDESC
Publisher:		$APPPUBL
Version:		$APPVERS

Do you want to continue?"; echo $?)
if [[ $IQ == *"0"* ]]; then
true
fi
if [[ $IQ == *"1"* ]]; then 
echo "# Installation has been cancelled."
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
if [[ -z $SHA256 ]]; then
echo "# Installation has been cancelled."
warn "Please provide SHA256 to verify integrity!"
exit
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 [[ $Local_Setup == *"Yes"* ]]; then
LSQ=$(question "The installation of $APPNAME requieres an local copy, like from an CD-ROM for instance. Are you ready to select the setup executable?")
if [[ $LSQ == *"0"* ]]; then
SetupFile=$(yad --file --title="$NAME - Select your setup executable" --window-icon=$ICO)
if [[ -z "$SetupFile" ]]; then
echo "# Installation has been cancelled."
exit
fi

fi
if [[ $LSQ == *"1"* ]]; then
echo "# Installation has been cancelled."
exit
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"
if [[ $DXVK_Always == *"Ask"* ]]; then
DXVKQ=$(question "Do you want to install and enable DXVK? [DX8-11 over Vulkan]")
if [[ $DXVKQ == *"0"* ]]; then
DXVK_VER=$(yad --window-icon=$ICO --entry --title="$NAME - Setting up DXVK" --text="Enter the version of DXVK that you want to use, example value: 2010 for v2.1 or leave it empty to use the latest version." --entry-text "")
fi
fi
if [[ $DXVKQ == *"0"* ]] || [[ $DXVK_Always == *"Yes"* ]] ; then
winetricks dxvk$DXVK_VER
fi
if [[ $DXVKQ == *"1"* ]] || [[ $DXVK_Always == *"No"* ]] ; then
echo -e "\e[3;5;0;31mIt was choosen not to set up DXVK.\e[30;48;5;82m\e[0m"
fi
fi

if [[ $VKD3D_Proton == *"Yes"* ]]; then
echo 75
echo "# Setting up VKD3D_Proton"
if [[ $VKD3D_Proton_Always == *"Ask"* ]]; then
VKD3DPQ=$(question "Do you want to install and enable VKD3D_Proton? [DX12 over Vulkan - Proton version]")
fi
if [[ $VKD3DPQ == *"0"* ]] || [[ $VKD3D_Proton_Always == *"Yes"* ]] ; then
winetricks vkd3d
VKD3D_Proton_VERSION=$(question "Since vkd3d_proton version 2.14, a bug has been detected with Mesa 24 causes the graphics driver amdgpu to crash when opening games. If you don't have an newer version yet, you can install mesa-devel.
It is recommended to use version 2.13, but it can happen that in very recent games other problems occur that require a newer version.
Do you want to continue using version 2.13? https://github.com/HansKristian-Work/vkd3d-proton/issues/2277")
if [[ $VKD3D_Proton_VERSION == *"0"* ]]; then
cd $CDIR
if [ ! -d $CDIR/vkd3d-proton-2.13 ]; then
download https://github.com/HansKristian-Work/vkd3d-proton/releases/download/v2.13/vkd3d-proton-2.13.tar.zst
tar -xf vkd3d-proton-2.13.tar.zst
fi
cp $CDIR/vkd3d-proton-2.13/x64/d3d12.dll $APPDIR/drive_c/windows/system32/
cp $CDIR/vkd3d-proton-2.13/x86/d3d12.dll $APPDIR/drive_c/windows/syswow64/
cp $CDIR/vkd3d-proton-2.13/x64/d3d12core.dll $APPDIR/drive_c/windows/system32/
cp $CDIR/vkd3d-proton-2.13/x86/d3d12core.dll $APPDIR/drive_c/windows/syswow64/
fi
fi
if [[ $VKD3DPQ == *"1"* ]] || [[ $VKD3D_Proton_Always == *"No"* ]] ; then
echo -e "\e[3;5;0;31mIt was choosen not to set up VKD3D_Proton.\e[30;48;5;82m\e[0m"
fi
fi

echo 80
echo "# Preparing setup"

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

if [[ -n "${Misc_Setup}" ]]; then
eval $Misc_Setup
fi

echo 90
echo "# Running installation"

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

if [[ -n "${NUPKG}" ]]; then
cd $APPDIR
NUPKGN=$(ls | grep $NUPKG)
7zz x "$APPDIR/$NUPKGN"
fi

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

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

if [[ $LAA == *"Yes"* ]]; then
if [ -n "$LAA_EXE" ]; then
LAA_Patch "$APPDIR/$LAA_EXE"
else
if [[ $UserDir == *"Yes"* ]]; then
LAA_Patch "$APPDIR/drive_c/users/$USER/$EXE"
else
LAA_Patch "$APPDIR/$EXE"
fi
fi
fi

if [[ -n "${Misc_Final}" ]]; then
eval $Misc_Final
fi

if [[ $DRAS == *"Yes"* ]]; then
$DefaultWinePath/wineserver -k
fi

echo 100
echo "# Installation is done!"
fi

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

cd "$APPDIR"

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

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

WRKDIR=$(grep WRKDIR $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[1;35mStarting 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