#! /bin/bash


usage () {
    echo $@
    echo "usage: install <program>"
    echo "programs are: Xsp         : Install only Xsupplicant"
    echo "              OpX         : Install only Open1xSupplicant"
    echo "              Both        : Install Both programs"
    exit $R_USAGE
}

######################################################################
# check arguments
#
PROGRAM=$1

case $PROGRAM in
    Xsp) ACTION=Xsupplicant ;;
    OpX) ACTION=Open1xSupplicant ;;
    Both) ACTION=BothPrograms ;;
    *) usage
esac



echo "#LOG FILE OF OPEN1XSUPPLICANT INSTALATION#" > ./install.log
if [ $ACTION == BothPrograms ] || [ $ACTION == Xsupplicant ]
then
    #----------Install Xsupplicant------------------------------------

    echo "#START INSTALATION OF XSUPPLICANT#" >> ./install.log
    echo "#START INSTALATION OF XSUPPLICANT#"
                
    cd ./xsupplicant-1.0.1/

    sleep 1        
                 
    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 
        echo "Terminou Correctamente a Instalacao do Open1xSupplicant."
    else
        echo "ERRO na Instalacao do Open1xSupplicant. Verifique o ficheiro de log para identificar o problema."
    fi 
       
    cd ..
               
    echo "#END OF INSTALATION OF XSUPPLICANT#" >> ./install.log
    echo "#END OF INSTALATION OF XSUPPLICANT#"
fi         
       
if [ $ACTION == BothPrograms ] || [ $ACTION == Open1xSupplicant ]
then

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

    echo "#START INSTALATION OF OPEN1XSUPPLICANT#" >> ./install.log  
    echo "#START INSTALATION OF OPEN1XSUPPLICANT#"              
                
    cd ./open1xsupplicant-0.1/

    sleep 1        

    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 
        echo "Terminou Correctamente a Instalacao do Open1xSupplicant."
    else
        echo "ERRO na Instalacao do Open1xSupplicant. Verifique o ficheiro de log para identificar o problema."
    fi           
 
    cd ..                

    echo "#END OF INSTALATION OF OPENXSUPPLICANT#" >> ./install.log
    echo "#END OF INSTALATION OF OPENXSUPPLICANT#"
    
    #----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
exit $R_SUCCESS
