#!/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.

# An Wine-frontend which should help you to install and use applications based
# on the given library without obstacles on FreeBSD. 

# Some things have value because they are pointless, right?

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

if [ $(id -u) -eq 0 ]
then
echo "Running it as root is unsupported and insecure."
exit
fi

# Some global settings

#printf %b '\e]11;#000000\a'
export NAME=Mizutamari
export EXECNAME=Mizuma
export VER="2024.11"
export ALH=500
export WTH=500
export FBSDVER=$(uname -r | cut -c -2)
GPU=$(glxinfo | grep "OpenGL vendor string")
OSN=$(uname)
export ARCH=$(uname -m)
WM=$(xprop -id $(xprop -root -notype | awk '$1=="_NET_SUPPORTING_WM_CHECK:"{print $5}') -notype -f _NET_WM_NAME 8t | grep "WM_NAME = " | cut -f2 -d"=" | cut -d ' ' -f 2 | awk 'FNR%2' | sed 's/"//g')
WAYCK=$(echo $XDG_SESSION_TYPE)

# Create Configuration file

TODO=Configuration
if [ ! -f "$HOME/.config/$NAME.conf" ]; then
echo "# Specify the home of $NAME" >> $HOME/.config/$NAME.conf
echo "ProjectPath=$HOME/.local/share/$NAME" >> $HOME/.config/$NAME.conf
echo "# Specify the default wine" >> $HOME/.config/$NAME.conf
echo "DefaultWinePath=/usr/local/bin" >> $HOME/.config/$NAME.conf
echo "# This option enables automatic handling of wine's 32-bit libraries, please disable that option now if you are not using binary packages!" >> $HOME/.config/$NAME.conf
echo "AllowLib32Maintenance=Yes" >> $HOME/.config/$NAME.conf
echo "# Automatically turn the compositor off/on. Brings little better performance and is needed for FreeSync." >> $HOME/.config/$NAME.conf
echo "MetacityCompoSwitch=Off" >> $HOME/.config/$NAME.conf
echo "MATECompoSwitch=Off" >> $HOME/.config/$NAME.conf
echo "XFWMCompoSwitch=Off" >> $HOME/.config/$NAME.conf
echo "KDECompoSwitch=Off" >> $HOME/.config/$NAME.conf
echo "# Turn this off if you don't want to get notifications about using the fallback driver." >> $HOME/.config/$NAME.conf
echo "GPUDriverWarning=On" >> $HOME/.config/$NAME.conf
echo "# Wine version settings per application." >> $HOME/.config/$NAME.conf

anr=$(zenity --question --width=400 --window-icon=$ICO --title "$NAME - $TODO" --text "A new $NAME configuration file have been created, do you want to review it?"; echo $?)
if [[ $anr == *"0"* ]]; then
content=$(zenity --text-info --width=1000  --height=500 --filename=$HOME/.config/$NAME.conf --editable --window-icon=$ICO --title "$NAME - $TODO" --text "Close this window if you are done editing.")
[ $? -eq 0 ] && echo "$content" > $HOME/.config/$NAME.conf

fi

fi

# Read Configuration file
export ProjectPath=$(grep ProjectPath $HOME/.config/$NAME.conf | cut -f2 -d"=")
export DefaultWinePath=$(grep DefaultWinePath $HOME/.config/$NAME.conf | cut -f2 -d"=")
AllowLib32Maintenance=$(grep AllowLib32Maintenance $HOME/.config/$NAME.conf | cut -f2 -d"=")
export MetacityCompoSwitch=$(grep MetacityCompoSwitch $HOME/.config/$NAME.conf | cut -f2 -d"=")
export MATECompoSwitch=$(grep MATECompoSwitch $HOME/.config/$NAME.conf | cut -f2 -d"=")
export XFWMCompoSwitch=$(grep XFWMCompoSwitch $HOME/.config/$NAME.conf | cut -f2 -d"=")
export KDECompoSwitch=$(grep KDECompoSwitch $HOME/.config/$NAME.conf | cut -f2 -d"=")
GPUDriverWarning=$(grep GPUDriverWarning $HOME/.config/$NAME.conf | cut -f2 -d"=")

# Display GhostBSD as OS
if [ -f "/etc/pkg/GhostBSD.conf" ]; then
OSN=GhostBSD
fi

# Misc
DATE=$(date)
DATEY=$(date +%Y)
DATEDM=$(date +%d-%m)
COPYRIGHT=$(echo 2019-$DATEY, Alexander Vereeken)
export WINEDLLOVERRIDES=winemenubuilder.exe=d
export DBIN=fetch

# Attach Directories to the $ProjectPath

if [ -z "$DDIR" ]
then
export DDIR=$ProjectPath/Data
else
if [[ $DDIR == *"$ProjectPath/Data"* ]]; then
echo "Default Data path in use"
else
echo "Custom Data path in use"
fi
fi

if [ -z "$LDIR" ]
then
export LDIR=$ProjectPath/Library
else
if [[ $LDIR == *"$ProjectPath/Library"* ]]; then
echo "Default Library path in use"
else
echo "Custom Library path in use"
fi
fi

if [ -z "$GDIR" ]
then
export GDIR=$ProjectPath/Games
else
if [[ $GDIR == *"$ProjectPath/Games"* ]]; then
echo "Default Games path in use"
else
echo "Custom Games path in use"
fi
fi

if [ -z "$PDIR" ]
then
export PDIR=$ProjectPath/Programs
else
if [[ $PDIR == *"$ProjectPath/Programs"* ]]; then
echo "Default Programs path in use"
else
echo "Custom Programs path in use"
fi
fi

if [ -z "$CDIR" ]
then
export CDIR=$ProjectPath/Cache
else
if [[ $CDIR == *"$ProjectPath/Cache"* ]]; then
echo "Default Cache path in use"
else
echo "Custom Cache path in use"
fi
fi

# Functions Easy

function download {
		cd $CDIR && $DBIN $1
           }

function warn {
               zenity --warning --width=400 --window-icon=$ICO --title "$NAME - $TODO" --text "$1"
           }
           
function info {
               zenity --info --width=400 --window-icon=$ICO --title "$NAME - $TODO" --text "$1"
           }

function question {
               zenity --question --width=400 --window-icon=$ICO --title "$NAME - $TODO" --text "$1"; echo $?
           }

function notify {
               zenity --notification --window-icon=$ICO --text "$1"
           }

function remove {
               rm -d -r -f "$1"
           }

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

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

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

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

# When $NAME is first started.

if [ ! -d "$ProjectPath" ]; then
TODO="Setting up $NAME"
mkdir -p $ProjectPath
info "Welcome to $NAME and thank you for using it!"
info "The current wine and wine-devel port maintainers are looking
for new maintainers/contributors for their ports.
If you think this is something for you, please create a ticket on the FreeBSD Bugzilla."
if [[ $OSN != "FreeBSD" ]]; then
info "You are about to use $NAME on an unsupported system ($OSN). Please be aware that unexpected errors may occur and that your bug reports can be marked as invaild."
fi
if [[ $WAYCK == *"wayland"* ]]; then
info "Using Wine with Wayland is currently unsupported, you may have issues."
fi
SC=$(question "Do you want a shortcut of $NAME in your application menu?")
if [[ $SC == *"0"* ]]; then
if [ ! -d "$HOME/.local/share/applications" ]; then
mkdir $HOME/.local/share/applications
fi
echo "[Desktop Entry]
Comment=Wine-based Application Launcher for FreeBSD
Exec=bash /usr/local/bin/$EXECNAME
Icon=$CDIR/$NAME.png
Categories=Game;
Name=$NAME
StartupNotify=false
Terminal=false
TerminalOptions=
Type=Application
" >> $HOME/.local/share/applications/$NAME.desktop 
if [[ $SC == *"1"* ]]; then 
echo "Shortcut will be not created"
fi
fi
fi

# Creating the subdirectories

if [ ! -d "$GDIR" ]; then
mkdir $GDIR
fi

if [ ! -d "$PDIR" ]; then
mkdir $PDIR
fi

if [ ! -d "$DDIR" ]; then
mkdir $DDIR
fi

if [ ! -d "$CDIR" ]; then
mkdir $CDIR
fi

# Icon

export ICO=$CDIR/$NAME.png
if [ ! -f "$ICO" ]; then
download https://codeberg.org/Alexander88207/$NAME/raw/branch/main/Media/$NAME.png
fi

# 4GB Patch

if [ ! -f $DDIR/4gb_patch.exe ]; then
cd $DDIR
fetch https://ntcore.com/files/4gb_patch.zip
7zz x 4gb_patch.zip
rm 4gb_patch.zip
fi

# Advanced Exit
function aexit {
if [ -z "$CLI" ]
then
exec $EXECNAME
else
exit
fi
}

# Export functions
export -f download
export -f warn
export -f info
export -f question
export -f notify
export -f remove

export -f DisableDLL
export -f BuiltinDLL
export -f NativeDLL
export -f NativeBuiltinDLL

# Library loader

function library-loader {
TODO="Library"
mkdir -p $LDIR
cd $LDIR
fetch https://codeberg.org/Alexander88207/Mizutamari/archive/main.zip
7zz x main.zip
mv $LDIR/mizutamari/Library/* $LDIR
cd $LDIR/
rm -r -d $LDIR/mizutamari
rm main.zip
find $CDIR -iname *.7z -exec rm {} \;
find $CDIR -iname *.zip -exec rm {} \;
find $CDIR -iname *.exe -exec rm {} \;
find $CDIR -iname *.msi -exec rm {} \;
find $CDIR -iname *.rar -exec rm {} \;
if [ "$(ls -A $LDIR)" ]; then
echo -e "\e[3;5;0;32mLibrary successfully loaded.\e[30;48;5;82m\e[0m"
notify "Library successfully loaded"
else
warn "The library could not be loaded successfully, check your internet connection?"
exec $EXECNAME
fi
}

if [[ -z "$Session" ]]; then
if [ "$(ls -A $LDIR 2> /dev/null)" ]; then
true
else
LQ=$(question "The library of Mizutamari is currently empty, do you want to download the library now?")
export Session=1
fi
if [[ $LQ == *"0"* ]]; then
library-loader
fi
fi

# Small check to ensure that the user knows that no correct drivers are running.

if [[ $GPUDriverWarning == *"On"* ]]; then
if [[ $GPU == *"VMware, Inc."* ]] || [[ $GPU == *"Mesa/X.org"* ]]; then
info "$NAME have detected that you are using the fallback drivers, please check your gpu drivers. If you run $NAME in a virtual machine, have enabled software rendering or you don't care then you can ignore this message and disable it in the config file."
fi
fi

# CLI
Install() {
CLI=1
export APPNAME=$1
export TODO=Installation
}

Launch() {
CLI=1
export APPNAME=$1
export TODO=Launcher
}

Uninstall() {
CLI=1
export APPNAME=$1
export TODO=Uninstallation
}

"$@"

# Maintenance of 32-Bit libraries
if [[ $AllowLib32Maintenance == *"Yes"* ]]; then
# Checking the internet connection.
nc -z 1.1.1.1 80  >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
if [ ! -f "$HOME/.i386-wine-pkg/usr/local/libdata/ldconfig/wine" ]; then
echo -e "\e[3;5;0;96mSetting up 32-Bit libraries ...\e[30;48;5;82m\e[0m"
/usr/local/share/wine/pkg32.sh install -y wine mesa-dri | zenity --auto-close --percentage=15 --progress --title="$NAME - 32-Bit maintenance" --text="Installing 32-Bit libraries"
MesaDEV=$(pkg info mesa-devel)
if [[ $MesaDEV == *"Name           : mesa-devel"* ]]; then
MD=$(question "It looks like that you have installed mesa-devel, do you like to install mesa-devel for the Wine 32-Bit part too?")
if [[ $MD == *"0"* ]]; then
/usr/local/share/wine/pkg32.sh install -y mesa-devel | zenity --auto-close --percentage=20 --progress --title="$NAME - 32-Bit maintenance" --text="Installing 32-Bit mesa-devel"
fi
fi
fi
if [ -z "$SndWine" ]
then
true
else
if [ ! -d "$HOME/.i386-wine-pkg/usr/local/wine-proton" ]; then
echo -e "\e[3;5;0;96mSetting up 32-Bit libraries for wine-proton ...\e[30;48;5;82m\e[0m"
/usr/local/share/wine/pkg32.sh install -y $SndWine
fi
fi
if [[ -z "$UpdateCheck" ]]; then
echo -e "\e[3;5;0;96mLooking for updates...\e[30;48;5;82m\e[0m"
/usr/local/share/wine/pkg32.sh upgrade -y | zenity --auto-close --percentage=25 --progress --title="$NAME - 32-Bit maintenance" --text="Looking for and installing updates"
/usr/local/share/wine/pkg32.sh autoremove -y | zenity --auto-close --percentage=75 --progress --title="$NAME - 32-Bit maintenance" --text="Cleaning up" && /usr/local/share/wine/pkg32.sh clean -a -y | zenity --auto-close --percentage=95 --progress --title="$NAME - 32-Bit maintenance" --text="Cleaning up"
export UpdateCheck=1
fi
else
echo -e "\e[3;5;0;96mMaintenance of the 32-Bit libraries has been skipped, no internet connection present.\e[30;48;5;82m\e[0m"
fi
fi

export WINEVER=$($DefaultWinePath/wine --version | cut -b 6-)

# Functions Wine

function wineverread {
WINEPATH=$(grep $APPNAME-WINEVER $HOME/.config/$NAME.conf | cut -f2 -d"=")
if [[ -z $WINEPATH ]]; then
echo -e "\e[3;5;0;34mWine version has been not set for this application yet, using default wine version from the Mizutamari configuration.\e[30;48;5;82m\e[0m"
echo "$APPNAME-WINEVER=$DefaultWinePath" >> $HOME/.config/$NAME.conf
wineverread
fi
export WINEBIN="$WINEPATH/wine"
export WINE=$WINEPATH/wine
export WINEPATH
}

# The App lists & Application menu

function wineprefixselection {
export APPLIST=$(cd $LDIR && ls | grep -v README.md | grep -v Scripts )
export APPEXISTCHECK=$(cd "$GDIR" && ls && cd "$PDIR" && ls )
if [[ $TODO == *"Installation"* ]]; then
true
else
if [ -z "$(ls -A $GDIR && ls -A $PDIR )" ]; then
notify "Nothing has been installed"
exec $EXECNAME
fi
fi
if [ -z "$CLI" ]
then
if [[ $TODO == *"Installation"* ]]; then
export APPNAME=$(zenity --list --ok-label=Install --title="$NAME - $TODO" --window-icon=$ICO --text "What do you want to install?" --height=$ALH --width=$WTH --column="Application" $APPLIST)
else
export APPNAME=$(zenity --ok-label=$BUTTON --list --title="$NAME - $TODO" --window-icon=$ICO --text "What application do you want to $TEXT?" --height=$ALH --width=$WTH --column="Application" $APPEXISTCHECK)
fi
fi
if [[ -z "$APPNAME" ]]; then
notify "No application was selected"
aexit
fi

if [ ! -f $LDIR/$APPNAME ];
then
warn "Sorry but $APPNAME doesn't seem to exist. Maybe a typo?"
aexit
fi
Game=$(grep Game $LDIR/$APPNAME | cut -f2 -d"=")
Program=$(grep Program $LDIR/$APPNAME | cut -f2 -d"=")
if [[ $Game == *"Yes"* ]]; then
export APPDIR=$GDIR/$APPNAME
fi
if [[ $Program == *"Yes"* ]]; then
export APPDIR=$PDIR/$APPNAME
fi
export WINEPREFIX="$APPDIR"
}

# Mesa shader cache

export MESA_SHADER_CACHE_DIR=$CDIR
export MESA_DISK_CACHE_DATABASE=1

# Check current audio device

PCMDEVICE=$(grep default /dev/sndstat)
PCMNUM=$(echo $PCMDEVICE | cut -c 4- | cut -f1 -d":")
PCMCHAN=$(sysctl dev.pcm.$PCMNUM.play.vchanformat | cut -c 29-)
PCMRATE=$(sysctl sysctl dev.pcm.$PCMNUM.play.vchanrate | cut -c 27-)

# Small informations in the terminal

echo -e "\e[0;36m━━━━━━━━━━━━━━━━━━━━━━━◇◆◇━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "\e[0;36mWelcome to\e[30;48;5;82m\e[0m $NAME $VER Have Fun!"
echo -e "\e[3;5;0;36mCurrent Wine Version:\e[30;48;5;82m\e[0m $WINEVER"
echo -e "\e[3;5;0;36mOperating System:\e[30;48;5;82m\e[0m $OSN $(uname -r)"
echo -e "\e[3;5;0;36mWindow Manager:\e[30;48;5;82m\e[0m $WM"
echo -e "\e[3;5;0;36mGPU Info OpenGL:\e[30;48;5;82m\e[0m" && glxinfo | grep OpenGL
echo -e "\e[3;5;0;36mGPU Info Vulkan:\e[30;48;5;82m\e[0m" && vulkaninfo 2>/dev/null | grep -m 1 "GPU id"
echo -e "\e[3;5;0;36mAudio Info:\e[30;48;5;82m\e[0m"
echo Device: $PCMDEVICE
echo Rate: $PCMRATE Hz
echo Format: $PCMCHAN
if (( $PCMRATE > 96000 )); then
info "Warning: Your audio sample rate is too high, some applications may have issues!"
fi
echo -e "\e[0;36m━━━━━━━━━━━━━━━━━━━━━━━◇◆◇━━━━━━━━━━━━━━━━━━━━━━━"

# Greetings on certain holidays

if [[ $DATEDM == *"01-01"* ]]; then
DHDM="Happy New Year! 🎆🎉🥳"
fi

if [[ $DATEDM == *"14-02"* ]]; then
DHDM="Happy Valentine's Day! 🥰❤️"
fi

if [[ $DATEDM == *"31-10"* ]]; then
DHDM="Happy Halloween! 👻🎃"
fi

if [[ $DATEDM == *"25-12"* ]] || [[ $DATEDM == *"26-12"* ]]; then
DHDM="Merry Christmas! 🎄🎁"
fi

# Initial menu

if [ -z "$CLI" ]
then
export TODO=$(zenity --ok-label=Continue --cancel-label=Exit --list --radiolist --window-icon=$ICO  --height=320 --width 390 --title="$NAME" --text " Welcome $USER! $DHDM - What do you want to do?" --hide-header --column "$NAME" --column "Item" FALSE "Installation" FALSE "Launcher" FALSE "Uninstallation" FALSE "Winetricks" FALSE "Kill Wine in a selected prefix" FALSE "Open Mizutamari folder" FALSE "Other" FALSE "About")
Exit=$?
fi

if [[ -z "$TODO" ]]; then
exit
fi

if [ -z "$CLI" ]
then
export WINEDEBUG=-all
export DXVK_LOG_LEVEL=none
export VKD3D_DEBUG=none
else
export WINEDEBUG=warn+trace+fixme+err
fi

if [[ $TODO == *"Installation"* ]]; then
# Checking the internet connection.
nc -z 1.1.1.1 80  >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
echo -e "\e[3;5;0;32mInternet connection OK.\e[30;48;5;82m\e[0m"
else
IQ=$(question "Hm.. it looks like you have no internet connection, do you want to continue?")
if [[ $IQ == *"0"* ]]; then
echo -e "\e[3;5;0;31m$USER is going to the applist even $NAME have no internet connection apparently?\e[30;48;5;82m\e[0m"
fi
if [[ $IQ == *"1"* ]]; then 
notify "Action was cancelled."
exec $EXECNAME
fi
fi

wineprefixselection
# Check if an application was selected.
if [[ -z "$APPNAME" ]]; then
notify "Nothing was selected for installation"
unset INST
exec $EXECNAME
else
# Checks if $APPNAME was already installed, needs to be improved.
CIAI1=$(cd "$GDIR" && ls | grep "$APPNAME")
if [ -z "$CIAI1" ]
then
echo -e "\e[3;5;0;32mApplication was not installed as a Game before.\e[30;48;5;82m\e[0m"
else
QT=$(question "Hm.. it looks like $APPNAME is already installed, do you want to continue?")
if [[ $QT == *"0"* ]]; then
notify "Installation continues"
fi
if [[ $QT == *"1"* ]]; then 
notify "Installation has been cancelled"
exec $EXECNAME
fi
fi
CIAI2=$(cd "$PDIR" && ls | grep "$APPNAME")
if [ -z "$CIAI2" ]
then
echo -e "\e[3;5;0;32mApplication was not installed as a Program before.\e[30;48;5;82m\e[0m"
else
QT=$(question "Hm.. it looks like $INST is already installed, do you want to continue?")
if [[ $QT == *"0"* ]]; then
notify "Continuing installation"
fi
if [[ $QT == *"1"* ]]; then 
notify "Installation has been cancelled"
exec $EXECNAME
fi
fi
QT=$(question "Do you really want to install $APPNAME?")
if [[ $QT == *"0"* ]]; then
notify "Starting Installation"
fi
if [[ $QT == *"1"* ]]; then 
notify "Installation has been cancelled"
exec $EXECNAME
fi
wineverread
if [ -z "$CLI" ]
then
bash /usr/local/share/$NAME/Tools/Framework | zenity --progress --title="$NAME" --text="Installation of $APPNAME" --percentage=0
else
bash /usr/local/share/$NAME/Tools/Framework
fi
unset APPNAME
fi
fi

if [[ $TODO == *"Launcher"* ]]; then
BUTTON=Launch
TEXT=launch
wineprefixselection
wineverread
bash /usr/local/share/$NAME/Tools/Framework
notify "$APPNAME got closed."
unset APPNAME
fi

if [[ $TODO == *"Uninstallation"* ]]; then
BUTTON=Uninstall
TEXT=uninstall
wineprefixselection
QT=$(question "Do you really want to uninstall $APPNAME?")
if [[ $QT == *"0"* ]]; then
notify "Starting Uninstallation"
fi
if [[ $QT == *"1"* ]]; then 
notify "Uninstallation has been cancelled"
exec $EXECNAME
fi
bash /usr/local/share/$NAME/Tools/Framework
notify "Task completed!"
unset APPNAME
fi

if [[ $TODO == *"Winetricks"* ]]; then
BUTTON=Continue
TEXT=select
wineprefixselection
winetricks
fi

if [[ $TODO == *"Kill Wine in a selected prefix"* ]]; then
BUTTON=Kill
TEXT=kill
wineprefixselection
wineverread
$WINEPATH/wineserver -k
notify "Wine for $APPNAME have been killed"
exit
fi

if [[ $TODO == *"Open Mizutamari folder"* ]]; then
xdg-open $ProjectPath
fi

if [[ $TODO == *"Other"* ]]; then
OTR=$(zenity --list --ok-label=Continue --radiolist --window-icon=$ICO --height=250 --width 250 --title="$NAME - $TODO" --text "What do you want to do?" --hide-header --column "$NAME" --column "Item" FALSE "Edit configuration file" FALSE "Update library" FALSE "Use 4GB Patch" FALSE "Support"  FALSE "Delete & reset everything")

if [[ $OTR == *"Edit configuration file"* ]]; then
content=$(zenity --text-info --width=1000  --height=500 --filename=$HOME/.config/$NAME.conf --editable --window-icon=$ICO --title "$NAME - $TODO" --text "Close this window if you are done editing.")
[ $? -eq 0 ] && echo "$content" > $HOME/.config/$NAME.conf
fi

if [[ $OTR == *"Update library"* ]]; then
UL=$(question "Updating the library will delete unsubmitted changes, do you want to continue?")
if [[ $UL == *"0"* ]]; then
rm -r -d $LDIR
library-loader
fi
if [[ $UL == *"1"* ]]; then 
notify "Library update has been cancelled"
fi
fi

if [[ $OTR == *"Use 4GB Patch"* ]]; then
BUTTON=Continue
TEXT=select
wineprefixselection
wineverread
cd $APPDIR
EXE=$(zenity --file-selection --title="$NAME - $OTR" --window-icon=$ICO)
$WINEBIN $DDIR/4gb_patch.exe $EXE
fi

if [[ $OTR == *"Support"* ]]; then
xdg-open https://codeberg.org/Alexander88207/Mizutamari/issues/new
fi

if [[ $OTR == *"Delete & reset everything"* ]]; then
DRQ=$(zenity --question --window-icon=$ICO --width 300 --title "$NAME - $OTR" --text "Do you really want to delete and reset everything?"; echo $?)
if [[ $DRQ == *"0"* ]]; then
rm -r -d $ProjectPath
rm $HOME/.config/$NAME.conf
if [ -f "$HOME/.local/share/applications/$NAME.desktop" ]; then
rm "$HOME/.local/share/applications/$NAME.desktop"
else 
echo -e "\e[40;38;5;32mNo shortcut was created\e[30;48;5;82m\e[0m"
fi
zenity --info --window-icon=$ICO --width=200 --title="$NAME - $OTR" --text="All files of $NAME have been deleted, we hope you had a good time."
exit
fi
fi
fi

if [[ $TODO == *"About"* ]]; then
info "An Wine front-end for FreeBSD brought to you by:

Alexander Vereeken and the following contributors:

- Brandon Ayers
- Coolguy71
- Ducky
- KiwiDch
- MagZu
- Matt @ librequest.org
- Rurichan
- Rvtsv
- Shiorid

Version: $VER

Licensed under the BSD 2-Clause.
Copyright (c) $COPYRIGHT
All rights reserved."
fi

if [ -z "$CLI" ]
then
echo -e "\e[0;33mGoing back to menu..."
exec $EXECNAME
fi
