#!/usr/bin/env bash

itzultool_version=0.4
itzultool_filename=itzultool-"$itzultool_version"-linux-x64
steamfolder="Shadow Tactics/Shadow Tactics_Data"
gogfolder="Shadow Tactics Blades of the Shogun/game/Shadow Tactics_Data"
repourl="https://tfe.eus/download/Blog/jokoak_euskaraz/shadow_tactics/emips"
emipprefix=shadowtactics
tempfolder=.shadow-tactics-instalazioa
gamename="Shadow Tactics - Blades of the Shogun"
email=tfe@tfe.eus

ascii=$(cat <<'END'
                                                                    
 .d88888b  dP     dP   .d888888  888888ba   .88888.  dP   dP   dP 
 88.    "' 88     88  d8'    88  88    `8b d8'   `8b 88   88   88 
 `Y88888b. 88aaaaa88a 88aaaaa88a 88     88 88     88 88  .8P  .8P 
       `8b 88     88  88     88  88     88 88     88 88  d8'  d8' 
 d8'   .8P 88     88  88     88  88    .8P Y8.   .8P 88.d8P8.d8P  
  Y88888P  dP     dP  88     88  8888888P   `8888P'  8888' Y88'   
                                                                  
 d888888P  .d888888   a88888b. d888888P dP  a88888b. .d88888b  
    88    d8'    88  d8'   `88    88    88 d8'   `88 88.    "' 
    88    88aaaaa88a 88           88    88 88        `Y88888b. 
    88    88     88  88           88    88 88              `8b 
    88    88     88  Y8.   .88    88    88 Y8.   .88 d8'   .8P 
    dP    88     88   Y88888P'    dP    dP  Y88888P'  Y88888P  
 -----------------------------------------------------------------                                                              
  B  L  A  D  E  S       O  F      T  H  E       S  H  O  G  U  N
                                                                          
END
)

endascii=$(cat <<'END'
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWWMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMWWMMMWNWXOkxoolloxOKKNWMMMMMMMMMWMMMMMMMMMMMM
MMMMMMMMMMMNMMMMWWWXx;........'''....,lxkWMMMWMWMMMMMMMMMMMM
MMMMMMMMMMMMMMMWKc........'',OWMWl........:OKNWWWMWKNMMMMMMM
MMMMMMMMMMMMMNOo..........cKXMMW0............:oxNO0WMMMMMMMM
MMMMMMMMMMMMNX,..........kMMMMMK...............ldKWWMMMMMMMM
MMMMMMMMMMMMO:..........;MMMMMMMx...............;ckMMMMMMMMM
MMMMMMMMMMMX'...........xMMMMMMMd................NkMMMMMMMMM
MMMMMMMMMMM;............XMNNMMMMO................KMMMMMMMMMM
MMMMMMMMMMX.............;NN0MMMMN,...............cMMMMMMMMMM
MMMMMMMMMMk.............'0XWMMMMWO,...............MMMMMMMMMM
MMMMMMMMMMk............dl0XWMWMMWod...............MMMMMMMMMM
MMMMMMMMMMK............,.KXNNKWMW:...............cMMMMMMMMMM
MMMMMMMMMMM;...........;'XNMWWNNNdc;.............dMMMMMMMMMM
MMMMMMMMMMMK............'xKMMMMMWo.'ccc;........lMMMMMMMMMMM
MMMMMMMMMMMMO............,;MMMWWNl.....',......:XMMMMMMMMMMM
MMMMMMMMMMMMMx............kMMWWNN;............dWMMMMMMMMMMMM
MMMMMMMMMMMXMWcl.........'WMNWNlK'..........:N0XMMMMMMMMMMMM
MMMMMMMMMMMMMMKXkx,......;MWoMo.,........'lXMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMWocWW:kl,...WX:W........;ddWMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMkWM0WMMX:0WKlW.'.;o0NWMxNMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMM0WMMMMMMkMNkXMdoK0WMMMMKMMMMMMMMMMMMMMMMMMMM                                                             
END
)

# FUNTZIOAK

initial_config() {
	# Aukeratu denda
	while true; do
		read -p "Zein dendatatik instalatu duzu jokoa?
		1: Steam
		2: GOG
		(idatzi 1 edo 2 eta sakatu SARTU tekla)
		" storeop
		case $storeop in
		    1 ) store='steam'; break;;
		    2 ) store='gog'; break;;
		    * ) echo "
		    OKERREKO AUKERA. Idatzi 1 (Steam) edo 2 (GOG) eta sakatu SARTU tekla.
		    ";;		
		esac
	done
}

get_steam_path() {
	echo "STEAM dendako jokoaren instalazioa bilatzen..."	
	steamconfigpath=~/.steam/steam/config/libraryfolders.vdf
	if [[ ! -f "$steamconfigpath" ]]; then
		steamconfigpath=~/.var/app/com.valvesoftware.Steam/.local/share/Steam/config/libraryfolders.vdf
		if [[ ! -f "$steamconfigpath" ]]; then
		    steamconfigpath=""
		    echo "EZIN IZAN DA STEAMEKO KONFIGURAZIO FITXATEGIA AURKITU."
		fi
	fi

	if [[ ! -z "$steamconfigpath" ]]; then
		while read -r line; do
		    if [[ $line == \"path\"* ]]; then
		        base=$(echo $line | cut -d '"' -f 4)
		        optpath="$base"/steamapps/common/$steamfolder
		        if [[ -d "$optpath" ]]; then
		            paths+=("$optpath")
		            #echo "Konfigurazioan $optpath aurkitu da..."
		        fi
		    fi
		done < "$steamconfigpath"

		if [[ ${#paths[@]} > 0 ]]; then
		    if [[ ${#paths[@]} == 1 ]]; then
		        path=${paths[0]}
		    else
		        # Galdetu erabiltzaileari
		        echo "Jokoarentzako karpeta posible bat baino gehiago aurkitu dira. Zein da jokoaren benetako karpeta?"
		        select selpath in "${paths[@]}"; do
		            if [[ -z "$selpath" ]]; then
		                printf 'Okerreko aukera.\n' "$selpath" >&2
		            else
		                path="$selpath"
		                break
		            fi
		        done
		    fi
		fi
	fi
}

get_gog_path() {
	echo "GOG dendako jokoaren instalazioa bilatzen..."
	paths[0]="`eval echo ~/GOG Games/$gogfolder`"
	
	if [[ ${#paths[@]} > 0 ]]; then
	    if [[ ${#paths[@]} == 1 ]]; then
	    	if [[ -d "${paths[0]}" ]]; then
		        path=${paths[0]}
	        fi
	    else
	        # Galdetu erabiltzaileari
	        echo "Jokoarentzako karpeta posible bat baino gehiago aurkitu dira. Zein da jokoaren benetako karpeta?"
	        select selpath in "${paths[@]}"; do
	            if [[ -z "$selpath" ]]; then
	                printf 'Okerreko aukera.\n' "$selpath" >&2
	            else
	                path="$selpath"
	                break
	            fi
	        done
	    fi
	fi
}

set_path_manually() {
	read -p "Ez da jokoaren karpeta aurkitu. Idatzi eskuz non dagoen.
    (adb. /home/erabiltzailea/.steam/steam/steamapps/common/$gamefolder)
    Kokalekua: " path
    while [[ ! -d "$path" ]]; do
        read -p "Sartutako kokalekua ez da existitzen. Saiatu berriz.
        (adb. /home/erabiltzailea/.steam/steam/steamapps/common/$gamefolder)
        Kokalekua: " path
    done
}

get_game_path() {
	path=""
	paths=()

	case $store in

		steam)
		get_steam_path
		;;
		
		gog)
		get_gog_path
		;;
		
	esac

	if [[ -z "$path" ]]; then
		set_path_manually
	fi

	echo "Path: $path"
}

create_temp_folder() {
	mkdir $tempfolder
	cd $tempfolder
}

handle_error() {
	if [[ $ok == 0 ]]; then
		echo "Huts egin du."
		echo "Instalazioko fitxategiak ezabatzen..."
		cd ..
		rm -R $tempfolder/

		# Errorea
		echo ""
		echo "✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗"
		echo "✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗ ✗"
		echo ""
		echo "Arazoren bat gertatu da '$gamename' jokoaren euskaratzea instalatzean. Saiatu berriro edo idatzi $email helbidera lagun zaitzadan."
		
		exit 1
	fi
}

download_itzultool() {
	echo ""
	echo "ItzulTool deskargatzen..."

	# Deskargatu ItzulTool
	wget https://github.com/ibaios/itzultool/releases/download/v"$itzultool_version"/"$itzultool_filename" || ok=0

	handle_error

	chmod +x ./"$itzultool_filename" || ok=0

	handle_error

	echo "Deskargatuta."
}

download_emip() {
	echo ""
	echo "EMIP itzulpen-fitxategia deskargatzen..."

	# Deskargatu EMIP fitxategia
	wget "${repourl}/${emipprefix}_${store}_linux.emip" || ok=0

	handle_error

	echo "Deskargatuta."
}

apply_emip() {
	echo ""
	echo "Itzulpena aplikatzen. Honek luze jo dezake..."

	# Aplikatu itzulpena
	./"$itzultool_filename" applyemip *.emip "$path" || ok=0

	handle_error
}

final_message() {
	echo ""
    echo "Instalazioko fitxategiak ezabatzen..."
    cd ..
    rm -R $tempfolder/
    echo "Eginda."

    # Instalatuta!
    echo ""
    echo "$endascii"
    echo ""
    echo "✔  Instalazioa behar bezala burutu da."
    echo "   Orain, '$gamename' euskaraz izango duzu."
}


# HASIERA
echo "$ascii"

echo "$gamename euskaraz - Instalatzen..."

initial_config

ok=1

get_game_path

create_temp_folder

download_itzultool

download_emip

apply_emip

final_message

exit 0

