#!/bin/bash
clear
# define enf
FILEPATH=$(readlink -f "$0")
BINARYPATH="$(dirname "${FILEPATH}")" 
cd "${BINARYPATH}"
FILENAME="$(basename "${FILEPATH}")"
FILEDIR="$(basename "${BINARYPATH}")"
EJENVPATH="$(dirname "${FILEPATH}")/easyjoin_env"
EJPATH="$(dirname "${FILEPATH}")/easyjoin"
LOG=install_$(date +%s).log
VERSION_MAIN_PATH="https://easyjoin.net/_linux/update-wine.xml"

# define colors
MAIN='\033[0m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
FLASH='\033[5m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHTGRAY='\033[0;37m'
DARKGRAY='\033[1;30m'
LIGHTRED='\033[1;31m'
LIGHTGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHTBLUE='\033[1;34m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
WHITE='\033[1;37m'

# functions

get_download_link() {
	XML="`wget -qO- https://easyjoin.net/_linux/update-wine.xml`"
	BASEURI=$(sed -n 's|<BaseUri>\(.*\)</BaseUri>|\1|p'  <<< $XML)
	PAYLOAD=$(sed  -n 's|<Payload>\(.*\)</Payload>|\1|p'  <<< $XML)
	DOWNLOAD_LINK=$BASEURI$PAYLOAD
	DOWNLOAD_LINK="${DOWNLOAD_LINK// /}"
	
}

init_ej () {
	
	# easyjoin
	if [[ ! -d "$EJPATH" ]]; then
		get_download_link
		printf "[${BLUE}SETUP_INFO${MAIN}] Get EasyJoin!\n"
		echo "[INFO] Get EasyJoin" >> $LOG
		wget $DOWNLOAD_LINK -O ej.zip >> $LOG 2>&1
		echo "[INFO] Extract EasyJoin" >> $LOG
		printf "[${BLUE}SETUP_INFO${MAIN}] Extract EasyJoin\n"
		unzip ej.zip -d easyjoin >> $LOG 2>&1
		rm ej.zip
	fi
	
	if [[ ! -d "$EJENVPATH" ]]; then
		printf "[${BLUE}SETUP_INFO${MAIN}] Initialize environment\n"
		echo "[INFO] Initialize environment" >> $LOG
		WINEPREFIX=$EJENVPATH winetricks -q win10 >> $LOG 2>&1
		printf "[${BLUE}SETUP_INFO${MAIN}] Install dotnet48\n"
		printf "[${BLUE}SETUP_INFO${MAIN}] 5-15min runtime [30-60min on very slow systems]\n"
		printf "[${BLUE}SETUP_INFO${MAIN}] You can use 'tail -f $LOG' on another terminal to monitor\n"
		echo "[INFO] Install dotnet48" >> $LOG
		WINEPREFIX=$EJENVPATH winetricks -q dotnet48 >> $LOG 2>&1
		printf "[${BLUE}SETUP_INFO${MAIN}] Install d3dcompiler_47\n"
		echo "[INFO] Install d3dcompiler_47" >> $LOG
		WINEPREFIX=$EJENVPATH winetricks -q d3dcompiler_47 >> $LOG 2>&1
		printf "[${BLUE}SETUP_INFO${MAIN}] Disable HW Accelertion\n"
		echo "[INFO] Disable HW Acceleration" >> $LOG
		WINEPREFIX=$EJENVPATH wine reg add "HKCU\\SOFTWARE\\Microsoft\\Avalon.Graphics" /v DisableHWAcceleration /t REG_DWORD /d 1 /f >> $LOG 2>&1
		printf "[${BLUE}SETUP_INFO${MAIN}] Install fonts\n"
		echo "[INFO] Install corefonts" >> $LOG
		WINEPREFIX=$EJENVPATH winetricks -q allfonts >> $LOG 2>&1
	fi
}

# banner
printf "${BOLD}${CYAN} ░█████████                                  ░█████           ░██  ${MAIN}\n"
printf "${BOLD}${CYAN} ░██                                           ░██                         ${MAIN}\n"
printf "${BOLD}${CYAN} ░██         ░█████    ░██████  ░██   ░██      ░██   ░██████  ░██░███████ ${MAIN}\n"
printf "${BOLD}${CYAN} ░████████       ░██  ░██       ░██   ░██      ░██  ░██   ░██ ░██░██   ░██ ${MAIN}\n"
printf "${BOLD}${CYAN} ░██         ░██████   ░██████  ░██   ░██ ░██  ░██  ░██   ░██ ░██░██   ░██  ${MAIN}\n"
printf "${BOLD}${CYAN} ░██        ░██  ░██        ░██ ░██  ░███ ░██  ░██  ░██   ░██ ░██░██   ░██ ${MAIN}\n"
printf "${BOLD}${CYAN} ░█████████  ░████░██  ░██████   ░████░██  ░█████    ░██████  ░██░██   ░██ ${MAIN}\n"
printf "${BOLD}${CYAN}                                      ░██${MAIN}\n"
printf "${BOLD}${CYAN}                                 ░██████${MAIN}\n"
printf "[${BLUE}INFO${MAIN}] Welcome to EasyJoin!\n"

# check if run as root

if (( $EUID == 0 )); then
	printf "[${RED}ERROR${MAIN}] Do NOT run as root!\n"
    exit 2
fi

# check if wine and winetricks are available
WGET=0
WINE=0
WINE32=0
WINETRICKS=0
UNZIP=0 

if command -v wget >/dev/null 2>&1
then
    WGET=1
else
	printf "[${RED}ERROR${MAIN}] WGET missing.\n"
fi  
if command -v wine >/dev/null 2>&1
then
    WINE=1
else
	printf "[${RED}ERROR${MAIN}] WINE missing.\n"
fi  


dpkg -s wine32 &> /dev/null  

if [ ! $? -ne 0 ]
then
    WINE32=1
else
	printf "[${RED}ERROR${MAIN}] WINE32 missing.\n"
fi
	
if command -v winetricks >/dev/null 2>&1
then
    WINETRICKS=1
else
	printf "[${RED}ERROR${MAIN}] WINETRICKS missing.\n"
fi  
if command -v unzip >/dev/null 2>&1
then
    UNZIP=1
else
	printf "[${RED}ERROR${MAIN}] UNZIP missing.\n"
fi  


if [[ ! $WINE == 1 ]] || [[ ! $WINE32 == 1 ]] || [[ ! $WINETRICKS == 1 ]] || [[ ! $WGET == 1 ]] || [[ ! $UNZIP == 1 ]]; then         
	printf "[${RED}ERROR${MAIN}] Some requirements are missing. Please run this commands.\n"
	printf "[${RED}ERROR${MAIN}] 'sudo dpkg --add-architecture i386'.\n"
	printf "[${RED}ERROR${MAIN}] 'sudo apt update'.\n"
	printf "[${RED}ERROR${MAIN}] 'sudo apt install wine wine32:i386 winetricks wget unzip'.\n"
  printf "[${RED}ERROR${MAIN}] Once you have installed the missing packages, run ./easyjoin_wine.sh again.\n"
	exit 10	
fi  

# check if in correct directory
if [[ ! $FILEDIR == easyjoin_wine* ]]; then
	printf "[${YELLOW}WARN${MAIN}] This scipt needs to be run in a folder that begins with 'easyjoin_wine'.\n"
	printf "[${YELLOW}WARN${MAIN}] e.g. 'easyjoin_wine', 'easyjoin_wine_new', etc.\n"
	printf "[${BLUE}INFO${MAIN}] We will now try to create a new folder 'easyjoin_wine' and copy this script into it.\n"
	printf "\n"
	read -p "Press enter to continue"
	printf "\n"
	FOLDERNAME=easyjoin_wine

	if [ -d "$BINARYPATH/$FOLDERNAME" ]; then
		printf "[${YELLOW}WARN${MAIN}] A folder 'easyjoin' already exists. Adding a random number to it.\n"
		FOLDERNAME="${FOLDERNAME}_$RANDOM"
	fi	
	if [ -d "$BINARYPATH/$FOLDERNAME" ]; then
		printf "[${RED}ERROR${MAIN}] There is also a folder '${FOLDERNAME}'. We give up. Just retry :D\n"
		exit 80;
		
	fi	
	mkdir $FOLDERNAME
	mv $FILENAME $FOLDERNAME/easyjoin_wine.sh
	
	printf "[${BLUE}INFO${MAIN}] You can find this script now in:\n"
	printf "[${BLUE}INFO${MAIN}] ${YELLOW}${BOLD}${FOLDERNAME}/easyjoin_wine.sh${MAIN}\n"
	printf "[${BLUE}INFO${MAIN}] We restart now.\n"
	printf "\n"
	read -p "Press enter to continue"
	clear
	bash $FOLDERNAME/easyjoin_wine.sh
	exit 0
   
fi

# check if already in use
if [[ ! -d "$EJENVPATH" ]] || [[ ! -d "$EJENVPATH" ]] ; then
	printf "[${YELLOW}WARN${MAIN}] It seems that either the environment or easyjoin itself are missing.\n"
	printf "[${YELLOW}WARN${MAIN}] We will run now the setup.\n"
	printf "[${BLUE}INFO${MAIN}] This can take a while!\n"
	printf "\n"
	read -p "Press enter to continue"
	printf "\n"

	init_ej
	
	printf "[${BLUE}INFO${MAIN}] EasyJoin is now ready. Just run ${YELLOW}${BOLD}${FILEDIR}/easyjoin_wine.sh${MAIN} to start it.\n"
  printf "[${BLUE}INFO${MAIN}] EasyJoin is now opening...\n"
 
  WINEPREFIX=$EJENVPATH nohup wine "easyjoin/EasyJoin.exe" >> /dev/null  2>&1 &
  exit 0
   
fi

WINEPREFIX=$EJENVPATH nohup wine "easyjoin/EasyJoin.exe" >> /dev/null  2>&1 &
exit 0


 
                                     


