#!/bin/bash
####################################################
## Script par Paul R. pour ASRI Edu
## Modif par C.G.
## version 20130329
####################################################

## Test pour n'activer qu'une seule instance de ce script - Script suggéré par Zorgue
#pid=`pidof -x -o $$ ${0##*/}`
pid=`pidof -o $$ ${0##*/}`
if [ -n "$pid" ]
then
  echo "Le script est déjà actif, vous ne pouvez donc pas l'activer une seconde fois."
  yaf-splash -timeout 4 -bg tomato -margin 4 -outline 0 -placement top -font 9x18 -text "Le script est actif, vous ne pouvez donc pas l'activer une seconde fois." &
  exit
else
echo "C'est bon, aucun doublon de ce script n'est actuellement actif. "
fi


## Si pupmode=5, affichage d'un message d'avertissement (une fois seulement)
echo "Repérage du pupmode actuel"
PUPMODE=`grep 'PUPMODE=' /etc/rc.d/PUPSTATE | sed -r "s/PUPMODE=//"`
if [ "$PUPMODE" = "5" ]; 
		then
	   /usr/share/asri_box/ia_docs/msg_pupmode5
fi

echo "Préparation des variables scripts box_ia."
## Script Contenant toutes les déclarations de variables des scripts box_ia
CONFIG_VARS_FILE=asriboxrc/config_vars

echo "Lecture des variables scripts box_ia."
# Lecture des variables des scripts box_ia
. $CONFIG_VARS_FILE

if [ -z $DISPLAY ]
then
	DIALOG=dialog
else
	DIALOG=Xdialog
fi

##
# POC around i18n/Localization in a bash script
#(1)
#export TEXTDOMAIN=asriboxia_divers
echo "export fichier langue asriboxia_choixtheme"
export TEXTDOMAIN=asriboxia_choixtheme
I18NLIB=i18n-lib.sh

#(2)
# source in I18N library - shown above
if [[ -f $I18NLIB ]]
then
        . $I18NLIB
else
        echo "ERROR - $I18NLIB NOT FOUND"
        exit 1
fi

#(3)
## ALLOW USER TO SET LANG PREFERENCE
## assume lang and country code follows
if [[ "$1" = "-lang" ]]
then
        export LC_ALL="$2_$3.UTF-8"
fi


echo "Test connexion internet Dépôt paquets proposés aux utilisateurs"
#ping google.fr -c 3
ping $PING_DEPOTPAQUETS -c 3 # 20130329 Test dépôt paquets
RESULT=$?

	if [ $RESULT != "0" ]; then
	echo "Fermeture du message de lancement"
	killall yaf-splash &
	Xdialog --center --title "Installeur d'applications ${DISTRO_NAME} ${DISTRO_VERSION}" --msgbox "
	L'installation des applications nécessite une connexion Internet fonctionelle,
	ainsi qu'un accès au serveur qui héberge les paquets destinés à ${DISTRO_NAME} ${DISTRO_VERSION}.
	
	Mais problème ! Actuellement, soit votre connexion internet n'est pas active,
	soit le serveur qui héberge les paquets n'est pas accessible.
	
	Remarque : l'installeur d'applications populaires ne sera utilisable
	que lorsque le serveur qui héberge les paquets sera accessible.
	" 0 0 $@
	exit
	fi


echo "GUI choix theme"
$DIALOG --title "${DISTRO_NAME} ${DISTRO_VERSION}" --ok-label "$(gettext -s "theme_divers")" --cancel-label "$(gettext -s "theme_kids")" --yesno "
$(gettext -s "explications")

$(gettext -s "theme_divers") = $(gettext -s "theme_divers_description")

$(gettext -s "theme_kids") = $(gettext -s "theme_kids_description")
"  15 70 5 
INST=$?
	
	if [ "$INST" = "255" ]; then
	exit
	fi
	
	if [ "$INST" = "1" ]; then
		export TEXTDOMAIN=asriboxia_kids
		export CONFIG_INTERFACE_FILE=$CONFIG_PATH/config_interface_kids.txt
		export OUTBOX=out_boxiauserkids.xml
	fi

	if [ "$INST" = "0" ]; then
		export TEXTDOMAIN=asriboxia_divers
		export CONFIG_INTERFACE_FILE=$CONFIG_PATH/config_interface_divers.txt
		export OUTBOX=out_boxiauserdivers.xml
	fi



echo "Debut du message d'information"
#gtkdialog-splash -text "$(gettext -s "lancement_installeur")" &
yaf-splash -bg lavender -margin 4 -outline 0 -placement bottom -font 9x18 -text "$(gettext -s "lancement_installeur")" &

echo "Debut du script GUI utilisateur"
TDB="<window title=\"${DISTRO_NAME} ${DISTRO_VERSION} $(gettext -s "titre_barre_fenetre") v`cat $DOCSAPP`\" icon-name=\"gtk-preferences\">
    <vbox>
    <text use-markup=\"true\"><label>\"$(gettext -s "titre_fenetre")\"</label></text>
    <notebook labels=\"$(gettext -s "onglets")\"> 
    <vbox spacing=\"5\">  
        <hbox>
            <vbox>
                <text use-markup=\"true\"><label>_
                </label></text>
                <text use-markup=\"true\"><label>
$(gettext -s "bienvenue1")</label></text>
                <text use-markup=\"true\"><label>
$(gettext -s "bienvenue2")</label></text>
                <text use-markup=\"true\"><label>_
                </label></text>
                <pixmap><input file>$IMAGE_DIR/asri_logo02.png</input></pixmap>
                <text width_request=\"100\" height_request=\"5\"><label>_
                </label></text>        
"

# save and change IFS
OLDIFS=$IFS

while IFS=\; read ID TITLE TOOLTIPX ICON PET_FILE INFO_URL
do  
	#echo -e "\nId : $ID\nTitre :\t $TITLE\nDescription :\t $TOOLTIP"
	export TOOLTIP=$(echo $TOOLTIPX | sed 's/<br\/>/\n/g')
	if [[ "$ID" =~ ^BT\.* ]]
	then
		#echo "Ajout du Bouton : $TITLE"
		export BT="
                <hbox>
                    <text>
                        <label>$TITLE</label>
                    </text>
                    <button tooltip-text=\"$TOOLTIP\">
                        <input file>$IMAGE_DIR/$ICON</input>"
		# Plusieurs commandes action
		until [ "$PET_FILE" == "$TOKEN" ]; do
			TOKEN=${PET_FILE%%,*}
			PET_FILE=${PET_FILE#*,}
			if [[ "$TOKEN" =~ \.*[.]pet\.* ]]   
			then
				# C'est un fichier ".pet"
				URL=$SERVER_URL01
				PET=$TOKEN
				if [[ "$TOKEN" =~ \$\.* ]]
				then
					# Le champ commence par une variable
					VAR=${TOKEN%%/*}
					PET=${TOKEN#*/}
					if [[ "$VAR" =~ \$SERVER_URL02 ]]
					then
						URL=$SERVER_URL02
					else
						if [[ "$VAR" =~ \$SERVER_URL03 ]]
						then
							URL=$SERVER_URL03
						else
							if [[ "$VAR" =~ \$SERVER_URL04 ]]
							then
								URL=$SERVER_URL04
							fi
						fi
					fi
				fi
				BT=$BT"
						<action>petget $URL/$PET && exec $PET &</action>"
			else
				if [[ "$TOKEN" =~ ^.+ ]]
				then
					VAR=${TOKEN%%/*}
					VAL=${TOKEN#*/}
					if [[ "$VAR" =~ \$DOCS_DIR ]]
					then
						BT=$BT"
						<action>$DOCS_DIR/$VAL</action>"
					else
						# Le champ est rempli
						BT=$BT"
						<action>$TOKEN</action>"
					fi
				else
						#20110707 Le champ est vide
						BT=$BT"
						<action>yaf-splash -timeout 2 -bg lavender -margin 4 -outline 0 -placement top -font 9x18 -text "'"Bouton inactif."'" &</action>"
				fi
			fi
		done
		BT=$BT" 
					</button>"
        if [[ "$INFO_URL" =~ \.*[A-Za-z]+\.* ]]   
        then
			BT=$BT"
						<button tooltip-text=\"$(gettext -s "msg_infos_application")\">
							<input file>$IMGINFOS</input>"
			if [[ "$INFO_URL" =~ ^http\.* ]] 
				then
					BT=$BT"
							<action>defaulthtmlviewer '$INFO_URL' &</action>"
				else
					if [[ "$INFO_URL" =~ ^.+ ]]
					then
						BT=$BT"
							<action>$INFO_URL</action>"
					fi
				fi
			BT=$BT" 
						</button>"
		fi
		TDB=$TDB$BT"
				</hbox>"
	else
		if [[ "$ID" =~ ^SEP\.* ]]
		then
			TDB=$TDB"
            </vbox>
            <text width_request=\"5\"><label>_
            </label></text>
            <vbox>"
		else
			if [[ "$ID" =~ ^TAB\.* ]]
			then
				#echo "Ajout de l'onglet : $TITLE"
				TDB=$TDB"
            </vbox>
            <text width_request=\"5\"><label>_
            </label></text>
        </hbox>
     </vbox>
     <vbox spacing=\"5\">
        <hbox>
            <vbox>"
			fi
		fi
	fi
done < $CONFIG_INTERFACE_FILE

# restore it
IFS=$OLDIFS


TDB=$TDB"			
            </vbox>   	
		</hbox>
    </vbox>
    </notebook>
    <hbox>  
        <button tooltip-text=\"$(gettext -s "bt_theme_switcher_tooltip")\">
            <label>$(gettext -s "bt_theme_switcher_label")</label>
            <input file>$IMAGE_DIR/switchtheme01.png</input>
            <action>$SCRIPT_ROOT_PATH/box_ia_user &</action>
            <action>exit:success</action>
        </button>
        <button tooltip-text=\"$(gettext -s "bt_quit_tooltip")\">
            <input file stock=\"gtk-cancel\"></input>
            <label>$(gettext -s "bt_quit_label")</label>
        </button>
    </hbox>
</vbox>   
</window>"
	
export TDB
killall yaf-splash &
echo "$TDB" > $OUTBOX
gtkdialog3 --program=TDB

unset TDB

echo "Fin du script GUI utilisateur"
