#!/bin/bash
# 
# This script "AS IT" has created by iri : http://www.irizone.net, under GNU/GPL licence.
# This script installs Scol
errorwget() {
	echo "$WGET_NO"
	echo "$INSTALL_ABORT"
	exit 1
}
# ### INIT :
DEFAULT_INSTALL_PATH=$HOME/scol/
# langage fr/en
if echo $LANG | grep -q "fr"; then
	WELCOME="Bonjour $USER :)"
	INSTALL_WANT="Voulez-vous installer Scol ? [y/n]"
	INSTALL_ABORT="Installation annulee"
	PATH_MSG1="Par defaut, Scol sera installe dans [$DEFAULT_INSTALL_PATH]"
	PATH_MSG2="Acceptez-vous ce parametre par defaut (recommande !!) ? [y/n]"
	PATH_CHANGE="Entrez le dossier d'installation (avec le / a la fin) :"
	INSTALL_MSG1="Installation de Scol ..."
	INSTALL_FINISHED="Scol 4.5 beta2 a bien ete installe. Enjoy !"
	README_MSG1="Voulez-vous lire le fichier README ? [y/n]"
	README_MSG2="Si oui, vous appuyerez sur les fleches haut et bas pour naviguer dans le fichier, sur 'q' pour revenir a l'installation"
	SCOL_LAUNCH="Voulez-vous lancez Scol ? [y/n]"
	LASTMSG="Merci d'utiliser Scol ! Tous les commentaires et questions sur http://www.scolring.org"
	WGET_NO="wget n'est pas installé. Voyez http://www.gnu.org/software/wget/wget.html"
else
	WELCOME="Hi $USER :)"
	INSTALL_WANT="Do you want install Scol ? [y/n]"
	INSTALL_ABORT="abort"
	PATH_MSG1="By default, Scol will be installed in [$DEFAULT_INSTALL_PATH]"
	PATH_MSG2="OK for this (recommanded !!) ? [y/n]"
	PATH_CHANGE="Enter your path now (with a / at the end) :"
	INSTALL_MSG1="Installation ..."
	INSTALL_FINISHED="Install 'Scol 4.5 beta2' is finished. Enjoy !"
	README_MSG1="Do you want read the file README ? [y/n]"
	README_MSG2="Use  the keys up and down to scroll the readme and 'q' to quit"
	SCOL_LAUNCH="Do you want launch Scol ? [y/n]"
	LASTMSG="Thanks to use this technology openSource ! All comments and questions on http://www.scolring.org"
	WGET_NO="wget is not installed. See http://www.gnu.org/software/wget/wget.html"
fi
# ### INSTALL :
echo "$WELCOME"
# Confirmation installation Scol
echo "$INSTALL_WANT"
read -n1 answer
# reponse non (autre que oui)
if [ "$answer" != 'y' -a "$answer" != 'Y' ]; then
	echo " "
	echo "$INSTALL_ABORT"
    	exit 1
fi
# Chemin d'installation
echo " "
echo "$PATH_MSG1"
echo "$PATH_MSG2"
read -n1 answer
# autre reponse que oui
if [ "$answer" != 'y' -a "$answer" != 'Y' ]; then
	echo " "
	echo "$PATH_CHANGE"
	read INSTALL_PATH
	# réponse vide
	if [ -z "$INSTALL_PATH" ]; then
		INSTALL_PATH=$DEFAULT_INSTALL_PATH
	fi
# reponse oui
else
	# réponse oui (Y/y)
    	INSTALL_PATH=$DEFAULT_INSTALL_PATH

fi
echo "--->$INSTALL_PATH<----"
mkdir --parents --verbose $INSTALL_PATH
echo " "
# # Sauvegarde d'une ancienne installation Scol éventuelle
cd $INSTALL_PATH
# cd ..
# mv scol scol.old
# Création du dossier d'install s'il n'existe pas
#if [ -d $INSTALL_PATH ]; then
#	mkdir $INSTALL_PATH
#fi
# Téléchargement de Scol
wget http://www.irizone.net/scs/scol_linux_v45_beta2_install.tgz || errorwget
echo " "
echo "$INSTALL_MSG1"
# Extraction de l'archive
tar xvfz scol_linux_v45_beta2_install.tgz
# Installation de Scol
cp -rf scol_linux_v45_beta2_install/scol/* $INSTALL_PATH
cp scol_linux_v45_beta2_install/README.txt $INSTALL_PATH
# Suppression de l'inutile
rm scol_linux_v45_beta2_install.tgz
rm -rf scol_linux_v45_beta2_install
echo " "
echo "$INSTALL_FINISHED"
echo " "
# Lecture du readme
echo "$README_MSG1"
echo "$README_MSG2"
read -n1 readdoc
if [ "$readdoc" = "y" -o "$readdoc" = "Y" ]; then
	echo " "
	cat $INSTALL_PATH"README.txt" | less
fi
# Lancer Scol ?
echo " "
echo "$SCOL_LAUNCH"
read -n1 scol
if [ "$scol" = "y" -o "$scol" = "Y" ]; then
	cd $INSTALL_PATH
	./scol &
fi
echo " "
echo "$LASTMSG"
echo " "
exit

