#! /bin/bash

#---------Choose Instalation Language----------------------------------------
dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --menu "Use [UP] [DOWN],[Enter] \
for choose" 15 50 3 PT "Portuguese" EN "English" 2>/tmp/menuitem.$$

menuitem=`cat /tmp/menuitem.$$`
rm -f /tmp/menuitem.$$

case $menuitem in
PT)
    #---------Wellcom Board--------------------------------------------------
    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "Bem vindo(a) a instalacao do pacote \
Open1xSupplicant, Xsupplicant. \
Este pacote permite ao utilizador efectuar autenticacao atraves do \
protocolo 802.1x utilizando uma interface grafica. \
Pressione qualquer tecla..." 10 50

    #---------Choose Yes or No to Install--------------------------------------
    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --yesno "\n  Deseja Instalar o pacote Open1xSupplicant?" 7 50
    selection=$?
    if [ $selection == 0 ]
    then
        #---------Choose program Language----------------------------------------
        dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --menu "Usar [CIMA] [BAIXO],[Enter] \
para Escolher" 15 50 3 OpX "Open1xSupplicant" Xsp "Xsupplicant" dois "Ambos" 2>/tmp/menuitem.$$

        menuitem=`cat /tmp/menuitem.$$`
        rm -f /tmp/menuitem.$$
       
       echo "#LOG FILE OF OPEN1XSUPPLICANT INSTALATION#" > ./install.log
       if [ $menuitem == 'dois' ] || [ $menuitem == 'Xsp' ]
       then
                       
                #----------Install Xsupplicant------------------------------------

                echo "#START INSTALATION OF XSUPPLICANT#" >> ./install.log

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n  Entar na directoria xsupplicant" 7 40
                cd ./xsupplicant-1.0.1/

                sleep 1        
                 
                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n   A Instalar o Xsupplicant" 7 40

                echo "#START ./CONFIGURE#" >> ../install.log 
    
                stat=0

                if ./configure >> ../install.log 2>&1; then   
                    echo "#START MAKE#" >> ../install.log                    
                
                    if make >> ../install.log 2>&1; then
                        echo "#START MAKE INSTALL#" >> ../install.log                               
                
                        if make install >> ../install.log 2>&1; then
                            echo "#START MAKE CLEAN#" >> ../install.log
                        
                            if make clean >> ../install.log 2>&1; then
                                stat=1
                            fi
                        fi
                     fi
                fi
                
                if [ $stat == 1 ]
                then 
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "\n Terminou Correctamente a Instalacao do Xsupplicant" 7 45
                else
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "\n ERRO na Instalacao do Xsupplicant. Verifique o ficheiro \
de log para identificar o problema." 8 45
                fi
       
                cd ..

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n Saiu da directoria do Xsupplicant" 7 40

                echo "#END OF INSTALATION OF XSUPPLICANT#" >> ./install.log
       
       fi         
       if [ $menuitem == 'dois' ] || [ $menuitem == 'OpX' ]
       then

                #----------Install Open1xSupplicant------------------------------------

                echo "#START INSTALATION OF OPEN1XSUPPLICANT#" >> ./install.log                

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n  Entar na directoria do Open1xSupplicant" 7 45
                cd ./open1xsupplicant-0.1/

                sleep 1        

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n   A instalar o Open1xSupplicant" 7 45

                echo "#START ./CONFIGURE#" >> ../install.log 
                    
                stat=0

                if ./configure >> ../install.log 2>&1; then   
                    echo "#START MAKE#" >> ../install.log                    
                
                    if make >> ../install.log 2>&1; then
                        echo "#START MAKE INSTALL#" >> ../install.log                               
                
                        if make install >> ../install.log 2>&1; then
                            echo "#START MAKE CLEAN#" >> ../install.log
                        
                            if make clean >> ../install.log 2>&1; then
                                stat=1
                            fi
                        fi
                     fi
                fi

                if [ $stat == 1 ]
                then 
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "\n Terminou Correctamente a Instalacao do Open1xSupplicant" 7 45
                else
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "\n ERRO na Instalacao do Open1xSupplicant. Verifique o ficheiro \
de log para identificar o problema." 8 45
                fi           
 
                cd ..

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --infobox "\n Saiu da directoria do Open1xSupplicant" 7 45

                echo "#END OF INSTALATION OF OPENXSUPPLICANT#" >> ./install.log
        fi
        if [ $menuitem == 'dois' ] || [ $menuitem == 'OpX' ]
        then

                #---------Choose program Language----------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --menu "Usar [CIMA] [BAIXO],[Enter] \
para Escolher" 15 50 3 PT "Portugues" EN "Ingles" FR "Frances" 2>/tmp/menuitem.$$

                menuitem2=`cat /tmp/menuitem.$$`
                rm -f /tmp/menuitem.$$

                case $menuitem2 in
                PT)        
                    if [ -d $HOME/.open1xsupplicant/ ]
                    then
                        if [ -f $HOME/.open1xsupplicant/language.xml ]
                        then
                            echo "#LANGUAGE FILE ALREDY EXIST#" >> ./install.log
                        else
                            cp ./open1xsupplicant-0.1/Languages/language_PT.xml $HOME/.open1xsupplicant/
                            mv $HOME/.open1xsupplicant/language_PT.xml $HOME/.open1xsupplicant/language.xml
                            echo "#CREATION OF PT LANGUAGE FILE#" >> ./install.log
                        fi
                   else
                        mkdir $HOME/.open1xsupplicant/
                        cp ./open1xsupplicant-0.1/Languages/language_PT.xml $HOME/.open1xsupplicant/
                        mv $HOME/.open1xsupplicant/language_PT.xml $HOME/.open1xsupplicant/language.xml
                        echo "#CREATION OF PT LANGUAGE FILE#" >> ./install.log
                   fi           
                ;;
                FR)        
                   if [ -d $HOME/.open1xsupplicant/ ]
                   then
                       if [ -f $HOME/.open1xsupplicant/language.xml ]
                       then
                           echo "#LANGUAGE FILE ALREDY EXIST#" >> ./install.log
                       else
                           cp ./open1xsupplicant-0.1/Languages/language_FR.xml $HOME/.open1xsupplicant/
                           mv $HOME/.open1xsupplicant/language_FR.xml $HOME/.open1xsupplicant/language.xml
                           echo "#CREATION OF FR LANGUAGE FILE#" >> ./install.log
                       fi
                   else
                       mkdir $HOME/.open1xsupplicant/
                       cp ./open1xsupplicant-0.1/Languages/language_FR.xml $HOME/.open1xsupplicant/
                       mv $HOME/.open1xsupplicant/language_FR.xml $HOME/.open1xsupplicant/language.xml
                       echo "#CREATION OF FR LANGUAGE FILE#" >> ./install.log
                   fi           
                ;;
                esac
                #----Create Desktop icon to open1xsupplicant-------
                if [ -d $HOME/.open1xsupplicant/ ]
                then                        
                    cp ./open1xsupplicant.png $HOME/.open1xsupplicant/            
                    echo "#COPY ICON TO OPEN1XSUPPLICANT DIRECTORY#" >> ./install.log                 
                else
                    mkdir $HOME/.open1xsupplicant/
                    cp ./open1xsupplicant.png $HOME/.open1xsupplicant/                        
                    echo "#COPY ICON TO OPEN1XSUPPLICANT DIRECTORY#" >> ./install.log
                fi 
                if [ -d $HOME/Desktop/ ]
                then
                    desktop=Desktop              
                else
                    desktop=.gnome-desktop
                fi
                echo "[Desktop Entry]" > $HOME/$desktop/open1xsupplicant.desktop
                echo "Encoding=UTF-8" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Exec=open1xsupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                if [ $USER != "root"  ]
                then   
                    echo "X-KDE-SubstituteUID=true" >> $HOME/$desktop/open1xsupplicant.desktop
                    echo "X-KDE-RootOnly=true" >> $HOME/$desktop/open1xsupplicant.desktop                
                fi                
                echo "Icon=$HOME/.open1xsupplicant/open1xsupplicant.png" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Name=Open1xSupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Name[en_US]=Open1xSupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "StartupNotify=true" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Terminal=false" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Type=Application" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "#CREATE OPEN1XSUPPLICANT DESKTOP ICON IN -> $HOME/$desktop/#" >> ./install.log
                #----------------------------------------------------   
       fi
       
       if [ $menuitem == 'dois' ] || [ $menuitem == 'OpX' ] || [ $menuitem == 'Xsp' ]
       then
       
           #---------Wellcom Board--------------------------------------------------
           dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "Obrigado pela preferencia \
 e espero que esta lhe seja util. \
Para Mais Informacoes http://www.open1xsupplicant.pt.vu/" 10 50
       
           clear
       else
           #---------Wellcom Board--------------------------------------------------
           dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "Pode Instalar este pacote noutra \
altura, executando os mesmos passos. Obrigado \
Para mais Informacoes http://www.open1xsupplicant.pt.vu/" 10 50

           clear
       fi
             
    else
        #---------Wellcom Board--------------------------------------------------
        dialog --title "Open1xSupplicant-0.1" --backtitle "Instalacao do \
Open1xSupplicant-0.1" --msgbox "Pode Instalar este pacote noutra \
altura, executando os mesmos passos. Obrigado \
Para mais Informacoes http://www.open1xsupplicant.pt.vu/" 10 50

        clear
    fi 
;;
EN)     
    #---------Wellcom Board--------------------------------------------------
    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "Welcom to Open1xSupplicant,Xsupplicant \
package instalation. \
This package allow user to athenticate through 802.1x protocol \
using a graphic interface. \
Press any key..." 10 50

    #---------Choose Yes or No to Install--------------------------------------
    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --yesno "\n  Wish to inatsall Open1xSupplicant package?" 7 50
    selection=$?
    if [ $selection == 0 ]
    then
        #---------Choose program Language----------------------------------------
        dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --menu "Use [UP] [DOWN],[Enter] \
to shose" 15 50 3 OpX "Open1xSupplicant" Xsp "Xsupplicant" both "Both" 2>/tmp/menuitem.$$

        menuitem=`cat /tmp/menuitem.$$`
        rm -f /tmp/menuitem.$$
       
       echo "#LOG FILE OF OPEN1XSUPPLICANT INSTALATION#" > ./install.log
       if [ $menuitem == 'both' ] || [ $menuitem == 'Xsp' ]
       then
                       
                #----------Install Xsupplicant------------------------------------

                echo "#START INSTALATION OF XSUPPLICANT#" >> ./install.log

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --infobox "\n  Enter on Xsupplicant directory" 7 40
                cd ./xsupplicant-1.0.1/

                sleep 1        
                 
                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --infobox "\n   Installing Xsupplicant" 7 40

                echo "#START ./CONFIGURE#" >> ../install.log 
    
                stat=0

                if ./configure >> ../install.log 2>&1; then   
                    echo "#START MAKE#" >> ../install.log                    
                
                    if make >> ../install.log 2>&1; then
                        echo "#START MAKE INSTALL#" >> ../install.log                               
                
                        if make install >> ../install.log 2>&1; then
                            echo "#START MAKE CLEAN#" >> ../install.log
                        
                            if make clean >> ../install.log 2>&1; then
                                stat=1
                            fi
                        fi
                     fi
                fi
                
                if [ $stat == 1 ]
                then 
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Isntalation of \
Open1xSupplicant-0.1" --msgbox "\n Instalation of Xsupplicant successfully finished" 7 45
                else
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "\n ERROR on Xsupplicant instalation. Verify the log \
file install.log to identify the problem." 8 45
                fi
       
                cd ..

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Isntalation of \
Open1xSupplicant-0.1" --infobox "\n Leaving Xsupplicant directory" 7 40

                echo "#END OF INSTALATION OF XSUPPLICANT#" >> ./install.log
       
       fi         
       if [ $menuitem == 'both' ] || [ $menuitem == 'OpX' ]
       then

                #----------Install Open1xSupplicant------------------------------------

                echo "#START INSTALATION OF OPEN1XSUPPLICANT#" >> ./install.log                

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --infobox "\n  Enter on Open1xSupplicant directory" 7 45
                cd ./open1xsupplicant-0.1/

                sleep 1        

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --infobox "\n   Installing Open1xSupplicant" 7 45

                echo "#START ./CONFIGURE#" >> ../install.log 
                    
                stat=0

                if ./configure >> ../install.log 2>&1; then   
                    echo "#START MAKE#" >> ../install.log                    
                
                    if make >> ../install.log 2>&1; then
                        echo "#START MAKE INSTALL#" >> ../install.log                               
                
                        if make install >> ../install.log 2>&1; then
                            echo "#START MAKE CLEAN#" >> ../install.log
                        
                            if make clean >> ../install.log 2>&1; then
                                stat=1
                            fi
                        fi
                     fi
                fi

                if [ $stat == 1 ]
                then 
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "\n Instalation of Open1xSupplicant successfuly finished" 7 45
                else
                    #---------Info Message-------------------------------------------------
                    dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "\n ERROR on Open1xSupplicant instalation. Verify the log \
file install.log to identify the problem." 8 45
                fi           
 
                cd ..

                #---------Info Message-------------------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --infobox "\n Leaving Open1xSupplicant directory" 7 45

                echo "#END OF INSTALATION OF OPENXSUPPLICANT#" >> ./install.log
        fi
        if [ $menuitem == 'both' ] || [ $menuitem == 'OpX' ]
        then

                #---------Choose program Language----------------------------------------
                dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --menu "Use [UP] [DOWN],[Enter] \
to shose" 15 50 3 PT "Portuguese" EN "English" FR "French" 2>/tmp/menuitem.$$

                menuitem2=`cat /tmp/menuitem.$$`
                rm -f /tmp/menuitem.$$

                case $menuitem2 in
                PT)        
                    if [ -d $HOME/.open1xsupplicant/ ]
                    then
                        if [ -f $HOME/.open1xsupplicant/language.xml ]
                        then
                            echo "#LANGUAGE FILE ALREDY EXIST#" >> ./install.log
                        else
                            cp ./open1xsupplicant-0.1/Languages/language_PT.xml $HOME/.open1xsupplicant/
                            mv $HOME/.open1xsupplicant/language_PT.xml $HOME/.open1xsupplicant/language.xml
                            echo "#CREATION OF PT LANGUAGE FILE#" >> ./install.log
                        fi
                   else
                        mkdir $HOME/.open1xsupplicant/
                        cp ./open1xsupplicant-0.1/Languages/language_PT.xml $HOME/.open1xsupplicant/
                        mv $HOME/.open1xsupplicant/language_PT.xml $HOME/.open1xsupplicant/language.xml
                        echo "#CREATION OF PT LANGUAGE FILE#" >> ./install.log
                   fi           
                ;;
                FR)        
                   if [ -d $HOME/.open1xsupplicant/ ]
                   then
                       if [ -f $HOME/.open1xsupplicant/language.xml ]
                       then
                           echo "#LANGUAGE FILE ALREDY EXIST#" >> ./install.log
                       else
                           cp ./open1xsupplicant-0.1/Languages/language_FR.xml $HOME/.open1xsupplicant/
                           mv $HOME/.open1xsupplicant/language_FR.xml $HOME/.open1xsupplicant/language.xml
                           echo "#CREATION OF FR LANGUAGE FILE#" >> ./install.log
                       fi
                   else
                       mkdir $HOME/.open1xsupplicant/
                       cp ./open1xsupplicant-0.1/Languages/language_FR.xml $HOME/.open1xsupplicant/
                       mv $HOME/.open1xsupplicant/language_FR.xml $HOME/.open1xsupplicant/language.xml
                       echo "#CREATION OF FR LANGUAGE FILE#" >> ./install.log
                   fi           
                ;;
                esac
                #----Create Desktop icon to open1xsupplicant-------
                if [ -d $HOME/.open1xsupplicant/ ]
                then                        
                    cp ./open1xsupplicant.png $HOME/.open1xsupplicant/            
                    echo "#COPY ICON TO OPEN1XSUPPLICANT DIRECTORY#" >> ./install.log                 
                else
                    mkdir $HOME/.open1xsupplicant/
                    cp ./open1xsupplicant.png $HOME/.open1xsupplicant/                        
                    echo "#COPY ICON TO OPEN1XSUPPLICANT DIRECTORY#" >> ./install.log
                fi 
                if [ -d $HOME/Desktop/ ]
                then
                    desktop=Desktop              
                else
                    desktop=.gnome-desktop
                fi
                echo "[Desktop Entry]" > $HOME/$desktop/open1xsupplicant.desktop
                echo "Encoding=UTF-8" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Exec=open1xsupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                if [ $USER != "root"  ]
                then   
                    echo "X-KDE-SubstituteUID=true" >> $HOME/$desktop/open1xsupplicant.desktop
                    echo "X-KDE-RootOnly=true" >> $HOME/$desktop/open1xsupplicant.desktop                
                fi                
                echo "Icon=$HOME/.open1xsupplicant/open1xsupplicant.png" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Name=Open1xSupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Name[en_US]=Open1xSupplicant" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "StartupNotify=true" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Terminal=false" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "Type=Application" >> $HOME/$desktop/open1xsupplicant.desktop
                echo "#CREATE OPEN1XSUPPLICANT DESKTOP ICON IN -> $HOME/$desktop/#" >> ./install.log
                #----------------------------------------------------   
       fi
       
       if [ $menuitem == 'both' ] || [ $menuitem == 'OpX' ] || [ $menuitem == 'Xsp' ]
       then
       
           #---------Wellcom Board--------------------------------------------------
           dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "Thanks for the preference, \
 we hope this are useful. \
Form more informations http://www.open1xsupplicant.pt.vu/" 10 50
       
           clear
       else
           #---------Wellcom Board--------------------------------------------------
           dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "You can install this package in another \
time, executing the same steps. Thanks \
For more informations http://www.open1xsupplicant.pt.vu/" 10 50

           clear
       fi
             
    else
        #---------Wellcom Board--------------------------------------------------
        dialog --title "Open1xSupplicant-0.1" --backtitle "Instalation of \
Open1xSupplicant-0.1" --msgbox "You can install tihs package in another \
time, executing the same steps. Thanks \
For more informations http://www.open1xsupplicant.pt.vu/" 10 50

        clear
    fi 
;;
esac

exit $R_SUCCESS
