#!/bin/sh

#source /home/configuration
source /etc/ip_config
source /etc/ip_machine	

#Log
#
log()
{
	echo "[sh] $1"
	echo -e "`date '+%H:%M:%S'` [sh] $1\r" >>  /home/CAPELEC/DATA/log/`date '+%y-%m-%d'`.log
}

#switchoff_network
#
switchoff_network()
{
	ifconfig eth0 0.0.0.0 up > /dev/null 2>&1	
	ifconfig wlan0 0.0.0.0 up > /dev/null 2>&1	
	ifconfig eth0 0.0.0.0 down > /dev/null 2>&1	
	ifconfig wlan0 0.0.0.0 down > /dev/null 2>&1	
}



restart_lan()
{
#on démonte le réseau

#umount -l /mnt/GIEGLAN

#on remonte le reseau
     if [ $IS_IP_ADR = 1 ]; then
     echo "montage reseau mode NOM CHEMIN"
     mount -t cifs //$IP_MACHINE/$PATH_GIEGLAN /mnt/GIEGLAN -o username=$USERNAME,password=$PASSWORD,nounix,noserverino
     else	
     echo "montage reseau mode ip CHEMIN"
     mount -t cifs $PATH_MNT /mnt/GIEGLAN -o username=$USERNAME,password=$PASSWORD,nounix,noserverino
     fi


TEST=$?
if [ $TEST = 0 ];then
#echo "PROD TEST ==0!!!!!!!!!"
		log "Mount ok"
		touch /home/CAPELEC/DATA/mount.state
		#Verification des mises à jour
		log "Check update..."
		if [ `ls /mnt/GIEGLAN/*.tar 2> /dev/null | wc -w` != 0 ]; then
			log "copy *.tar"
			cp /mnt/GIEGLAN/*.tar /home/CAPELEC/DATA/update/
			rm /mnt/GIEGLAN/*.tar
			log "copy ok"
			log ""
		else
			log "No update"	
		fi
fi


}


#extract_winsip
#Entree:
#		$1 = nom du serveur
#Sortie:
#		ADDR_SERVER = adresse ip du serveur	
extract_winsip()
{
	#echo "Server name: $1"
	ADDR_SERVER=`nmblookup $1 | awk '$2 ~ /<00>/{print $1}'`	
}

#config_ip_fixe
#Entree:
#		SUPPORT = WIFI ou LAN
config_ip_fixe()
{
	log "$1 on $ADDR_IP"
	ifconfig $1 $ADDR_IP netmask $MASQUE up > /dev/null 2>&1		
}


#Connexion_network 
#Entree:
#		SUPPORT = WIFI ou LAN
#Sortie:
#		"ETH" = eth0 ou wlan0
connexion_network()
{	
	if [ $SUPPORT = WIFI ]; then	
		ETH="wlan0"
		connexion_wifi	
		touch /home/state/wifi.state
	fi

	if [ $SUPPORT = LAN ]; then	ETH="eth0"; fi	
}

#mon_ip
#Entree: eth0 ou wlan0
#
mon_ip()
{	
	DHCP_IP=`ifconfig $1 | grep "inet addr" | cut -d" " -f12 | cut -d: -f2` 
}

#Config_ip
#Entree:
#		"IP_MODE" = DHCP ou FIXE
#		"ETH" = eth0 ou wlan0
config_ip()
{
	if [ $IP_MODE = DHCP ]; then
		log "Start dhcp mode..."
		ifconfig $ETH up > /dev/null 2>&1
		udhcpc -i $ETH -nt 30 > /dev/null 2>&1	
		if [ $? = 0 ]; then
			log "dhcp ok."	
			mon_ip $ETH
			log "IP: $DHCP_IP"
			log $DHCP_IP > /home/state/dhcp.state
		else
			log "ERROR: dhcp failed"
			#echo "Start IP fixe mode..."
			#config_ip_fixe $ETH
		fi
	else
		log "Start IP fixe mode..."
		config_ip_fixe $ETH
	fi
	#ifconfig > /dev/null 2>&1
	route add default gw $GATEWAY #Passerelle par defaut pour internet
}
#Connexion_wifi 
connexion_wifi()
{
	# Connection de la clef usb wifi
	ifconfig wlan0 up > /dev/null 2>&1
	if [ $? != 0 ]; then
		log "ERROR: Wifi key not found"
	else
		log "Connection..."		
		log "Configuring Wifi device: $SSID"
		if [ $CRYP = "NONE" ]; then
			log "Using no encryption..."
			iwconfig wlan0  essid "$SSID" key off > /dev/null 2>&1
			if [ $? != 0 ]; then			
				log "ERROR: Wifi failed"
			fi
		fi
		if [ $CRYP = "WEP" ]; then
			log "Using WEP encryption..."
			iwconfig wlan0  essid "$SSID" key restricted $KEY > /dev/null 2>&1
			if [ $? != 0 ]; then
				log "ERROR: Wifi failed"
			fi
		fi	
		if [ $CRYP = "WPA" ]; then
			log "Using WAP encryption..."
			echo "ctrl_interface=/var/run/wpa_supplicant" > /etc/wpa.conf
			echo "network={" >> /etc/wpa.conf
			echo -e "ssid=\042$SSID\042" >> /etc/wpa.conf
			echo "key_mgmt=WPA-PSK" >> /etc/wpa.conf
			echo -e "psk=\042$KEY\042" >> /etc/wpa.conf
			echo "}" >> /etc/wpa.conf
			killall wpa_supplicant
			wpa_supplicant -Dwext -iwlan0 -c/etc/wpa.conf -B
		fi					
	fi
}


#Test si wifi connecté au SSID
testWifi()
{
	RES=`iwconfig wlan0 | grep $SSID`
	if [ -n "$RES" ]; then
		RET=0	
	else
		RET=1
	fi
	return $RET
}

#Test si wifi est n'est dans l'etat Not-Associated
# RET=1 Not associated
# RET=0	Ok
testNotAssoWifi()
{
	ifconfig wlan0  > /dev/null 2>&1
	if [ $? != 0 ]; then
		RET=1
	else
		RES=`iwconfig wlan0 | grep "Not-Associated"`
		if [ -n "$RES" ]; then
			RET=1	
		else
			RET=0
		fi
	fi
	return $RET
}

#montage_reseau
#entrée:
#		IP_MODE = "DHCP" ou "FIXE"
#(si DHCP)	SERVER_NAME = ...
#(si FIXE)	SERVER_IP = ...
montage_reseau()
{
	#Mode DHCP
	if [ $IP_MODE = "DHCP" ]; then
		#Lecture de l'adresse IP du serveur
		log "Find IP server..."
		extract_winsip $SERVER_NAME
		log "Server IP: $1:$ADDR_SERVER"
		#Montage	
		log "Mount //$ADDR_SERVER/$SERVER_PATH"
		mount -t cifs //$ADDR_SERVER/$SERVER_PATH /mnt/LAN -o username=$USERNAME,password=$PASSWORD,nounix,noserverino > /dev/null 2>&1
		ERREUR=$?
	#Mode IP FIXE
	else	
		log "Mount //$SERVER_IP/$SERVER_PATH"
		mount -t cifs //$SERVER_IP/$SERVER_PATH /mnt/LAN -o username=$USERNAME,password=$PASSWORD,nounix,noserverino > /dev/null 2>&1
		ERREUR=$?		
	fi	

	if [ $ERREUR = 0 ]; then
		log "Mount ok"
		touch /home/state/mount.state
		#Verification des mises à jour
		log "Check update..."
		if [ `ls /mnt/LAN/*.tar 2> /dev/null | wc -w` != 0 ]; then
			log "copy *.tar"
			cp /mnt/LAN/*.tar /home/tmp/
			rm /mnt/LAN/*.tar
			log "copy ok"
			log ""
		else
			log "No update"	
		fi
	else 
		log "ERROR: Mount failed"
	fi
}


