#!/bin/bash
##This script only selects the installer language and, if its a online install, asks user to use stable or devel installer
idiom=`dialog --stdout --nocancel --backtitle "JKArch Instalation" --title "Language selection" --menu "Select your language / Selecciona tu idioma" 0 70 5 1 "Spanish (es)" 2 "English (en) (In progress)"`

if [[ $idiom -eq 1 ]];then
	sclang="es"
	loadkeys es
	#LANG="es_ES.UTF-8" ; export LANG
elif [[ $idiom -eq 2 ]];then
	sclang="en"
	loadkeys en
	#LANG="en_GB.UTF-8" ; export LANG
fi

if [[ -f translations.sh ]];then #If its a offline install (ISO)
	exec ./archinstall.sh $sclang 1 #Offline
else #Online install, it has to check if it uses stable or devel release of the installer
	branch=`dialog --stdout --nocancel --backtitle "JKArch Instalation" --title "Installer selection" --menu "El instalador tiene dos versiones, una estable la cual recomendamos, y otra experimental que usamos para probar cambios antes de mandarlos a todos y puede tener más fallos / JKArch has a stable installer that we recommend to use, and a experimental one that is not stable and use for tests and may fail. / " 0 70 5 1 "Estable/Stable (Por defecto, recomendado)" 2 "Experimental (Not recommended)"`

	#Download and exec

	if [[ $branch -eq 1 ]];then #Stable
		wget -nv https://gitlab.com/JKANetwork/JKArch/raw/master/archinstall/archinstall.sh -O archinstall.sh
	else #Devel
		wget -nv https://gitlab.com/JKANetwork/JKArch/raw/master/archinstall/archinstall-dev.sh -O archinstall.sh
	fi
	wget -nv https://gitlab.com/JKANetwork/JKArch/raw/master/archinstall/translations.sh -O translations.sh

	if [[ $(cat  archinstall.sh | grep "^<\!DOCTYPE html>") == "<\!DOCTYPE html>" || $(cat  translations.sh | grep "^<\!DOCTYPE html>") == "<\!DOCTYPE html>" ]];then
		clear
		dialog --title "Descarga incorrecta / Corrupted download" --msgbox "La descarga del instalador ha fallado \nse reiniciará la instalación \n\n Download failed. Restarting install.." 15 60
		exec ./selectlang.sh
	fi
	chmod +x archinstall.sh
	chmod +x translations.sh
	exec ./archinstall.sh $sclang 0 #Online

fi
