#!/bin/sh

source /etc/board_id
source /etc/ip_config


if [ -f /etc/ip_machine ]; then                                                 
        source /etc/ip_machine                                                  
fi 


echo "-----------------------------------------------------"
echo "--------------VARIABLES D'ENVIRONNEMENT--------------"
echo "-----------------------------------------------------"

echo "         NUMERO SERIE : $SERIAL_NUMBER"
echo "               CLIENT : $CLIENT"
echo "ADRESSE IP DU CAP1830 : $ADRESSE_IP"
echo "MASQUE DE SOUS RESEAU : $MASQUE"
echo "            BROADCAST : $BROADCAST"
echo "       CHEMIN SERVEUR : $SERVER_PATH"
echo " "
echo "      NOM UTILISATEUR : $USERNAME"
echo "             PASSWORD : $PASSWORD"

echo "-----------------------------------------------------"
echo "--------------CONFIGURE SPEC CLIENT--------------"
echo "-----------------------------------------------------"

if [ $CLIENT = SPX ]; then

rm -rf /var/www/*
cp -r /var/wwwspx/* /var/www/
chmod 777 -R /var/www
#pour eviter de copier ce dossier a chaque fois

sed "s/SPX/SPX1/" /etc/ip_config > /etc/ip_config_tmp
cp /etc/ip_config_tmp /etc/ip_config
echo "FOLDER SPX COPIED"

cp /etc/samba/spx/smb.conf.template /etc/samba/
echo "samba conf spx COPIED"
 
cp /etc/JAIL/spx/info /etc/
echo "file info COPIED"

cp /etc/JAIL/spx/1830 /usr/bin
echo "exe 1830 COPIED"


fi

if [ $CLIENT = TECNO ]; then

rm -rf /var/www/*
cp -r /var/wwwspx/* /var/www/
chmod 777 -R /var/www
#pour eviter de copier ce dossier a chaque fois

sed "s/TECNO/TECNO1/" /etc/ip_config > /etc/ip_config_tmp
cp /etc/ip_config_tmp /etc/ip_config
echo "FOLDER TECNO COPIED"

cp /etc/samba/spx/smb.conf.template /etc/samba/
echo "samba conf TECNO COPIED"
 
cp /etc/JAIL/tecnotest/info /etc/
echo "file info COPIED tecnotest"

cp /etc/JAIL/tecnotest/1830 /usr/bin
echo "exe 1830 COPIED tecnotest"


fi


if [ $CLIENT = CAPELEC ]; then

rm -rf /var/www/*
cp -r /var/wwwcapelec/* /var/www/
chmod 777 -R /var/www

#pour eviter de copier ce dossier a chaque fois
sed "s/CAPELEC/CAPELEC1/" /etc/ip_config > /etc/ip_config_tmp
cp /etc/ip_config_tmp /etc/ip_config
echo "FOLDER CAPELEC COPIED"

cp /etc/samba/capelec/smb.conf.template /etc/samba/
echo "samba conf capelec COPIED"

cp /etc/JAIL/capelec/info /etc/info
echo "file info COPIED"

cp /etc/JAIL/capelec/1830 /usr/bin
echo "exe 1830 COPIED"

fi


if [ $CLIENT = AVL ]; then

rm -rf /var/www/*
cp -r /var/wwwoem/* /var/www/
chmod 777 -R /var/www

#pour eviter de copier ce dossier a chaque fois
sed "s/AVL/AVL1/" /etc/ip_config > /etc/ip_config_tmp
cp /etc/ip_config_tmp /etc/ip_config
echo "FOLDER AVL COPIED"

cp /etc/samba/avl/smb.conf.template /etc/samba/
echo "samba conf AVL COPIED"

cp /etc/JAIL/capelec/info /etc/info
echo "file info COPIED"

cp /etc/JAIL/capelec/1830 /usr/bin
echo "exe 1830 COPIED"

fi


if [ $CLIENT = FOG ]; then

rm -rf /var/www/*
cp -r /var/wwwoem/* /var/www/
chmod 777 -R /var/www

#pour eviter de copier ce dossier a chaque fois
sed "s/FOG/FOG1/" /etc/ip_config > /etc/ip_config_tmp
cp /etc/ip_config_tmp /etc/ip_config
echo "FOLDER FOG COPIED"

cp /etc/samba/fog/smb.conf.template /etc/samba/
echo "samba conf FOG COPIED"

cp /etc/JAIL/capelec/info /etc/info
echo "file info COPIED"

cp /etc/JAIL/capelec/1830 /usr/bin
echo "exe 1830 COPIED"

fi




echo "-----------------------------------------------------"
echo "------------CONFIGURATION ADRESSE MAC----------------"
echo "-----------------------------------------------------"


ifconfig eth0 hw ether $MAC_ADR
# <> doit etre la meme addresse mac !
#ifconfig br0 hw ether $MAC_ADR

echo "-----------------------------------------------------"
echo "-----------------CREATION DU BRIDGE------------------"
echo "-----------------------------------------------------"

ifconfig eth0 0.0.0.0 promisc up
ifconfig wlan0 0.0.0.0

#brctl addbr br0



#echo "-----------------------------------------------------"
#echo "--------------AJOUT DE ETHO AU BRIDGE----------------"
#echo "-----------------------------------------------------"

#brctl addif br0 eth0 || brctl addif br0 eth0 || echo "Could not create bridge !"

#echo "-----------------------------------------------------"
#echo "---------------LANCEMENT DE HOSTAPD------------------"
#echo "-----------------------------------------------------"
#/etc/init.d/hostapd                                         
                             
#hostapd s'ajoute au bridge br0 dans hostapd.conf
                                                            
echo "-----------------------------------------------------"
echo "----------------ALUMAGE DE ETH0----------------------"
echo "-----------------------------------------------------"
ip link set dev eth0 up

echo "-----------------------------------------------------"
echo "------------CONFIGURATION DE L'ADRESSE IP------------"
echo "-----------------------------------------------------"
if [ $IP_MODE = DHCP ]; then
   echo "lancement mode dhcp"
udhcpc -i eth0 -nt 10 ||  `echo "lancement mode IP FIXE" ;ifconfig eth0 $ADRESSE_IP netmask $MASQUE broadcast $BROADCAST up;/etc/samba/configure $ADRESSE_IP`

echo "1">  /proc/sys/net/ipv4/ip_forward


#route add default gw $IPBOITIER 

#on copy l'adresse du serveur et l'adreese IP du boitier dans profile
#sed "s/#WINSIPADDR#/$WINSIPADDR/;s/#IPBOITIER#/$IPBOITIER/" /etc/ip_machine.template > /etc/ip_machine


else
#on copy l'adresse du serveur et l'adreese IP du boitier dans profile
#sed "s/#WINSIPADDR#/$WINSIPADDR/;s/#IPBOITIER#/$IPBOITIER/" /etc/ip_machine.template > /etc/ip_machine

#source /etc/ip_machine

  echo "lancement mode IP FIXE"
  ifconfig eth0 $ADRESSE_IP netmask $MASQUE broadcast $BROADCAST up
  /etc/samba/configure $ADRESSE_IP
#route add default gw $ADRESSE_IP
fi



#test de validité IPV4 de l'adresse entrée (expression régulière)


isIPv4(){
printf $1 | grep -Eq '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-4]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
echo $?
}

Result=$(isIPv4 $SERVER_IP)
#echo "longueur chaine SERVER_IP ${#SERVER_IP}"

IP_ADRESS=0


#result 0 means ip correct
if [ $Result = 1 ]; then

IP_ADRESS=1

echo "-------------------------------------------------------"
echo "--------recherche ip machine si nom reseau rentre------"  
echo "-------------------------------------------------------"


extract_winsip()
{
WINSIPADDR=`nmblookup $1 | awk '$2 ~ /<00>/{print $1}' | head -n 1`

echo "adresse ip serveur $1:$WINSIPADDR"
}

WINSIPADDR=""                                                              
COUNT=0                                                                                                                                                    
while [ -z $WINSIPADDR ]                                                                                                                                   
do                                                                                                                                                         
        echo "Adresse server $SERVER_IP..."                           
        extract_winsip $SERVER_IP                                               
        if [ -z $WINSIPADDR ]; then                                             
                sleep 3                                                         
                COUNT=$(($COUNT + 1))                                                                                                                              
                echo "Time: $COUNT"                                                                                                                                
                if [ $COUNT = 5 ]; then                                                                                                                            
     echo "ERREUR: Serveur introuvable!"                                                                                                        
                                                                                                       
         WINSIPADDR=$IP_MACHINE                                                                                                             
                                                                
         echo "Serveur par defaut: $WINSIPADDR"      

break  
                fi                                                              
        fi                                                                      
done                

IPBOITIER=`ifconfig eth0 | awk '$0 ~/Bcast/{print $2}'`

echo "adresse ip boitier $IPBOITIER"

fi



sed "s/#WINSIPADDR#/$WINSIPADDR/;s/#IPBOITIER#/$IPBOITIER/;s/#ISIPADR#/$IP_ADRESS/" /etc/ip_machine.template > /etc/ip_machine

#dans le script configure qui se trouve dans /etc/samba, va le smb.conf par rapport à la configuration
#on configure la passerelle pour le vpn CAPELEC
route add default gw $GATEWAY


#KILLCAP
