#!/bin/sh
#. /etc_ro_fs/init.d/common
. /etc/autoconf.conf
. /etc/advancedservices.conf

if (test -f "$FLASHCONFIGDIR/smbauto.conf"); then
   . $FLASHCONFIGDIR/smbauto.conf
fi

config_file="$FLASHCONFIGDIR/smb.conf"
log_file="$FLASHCONFIGDIR/smb.log" 
mount_dir=$HDTOOLSDIR         

niveau () {
        set $(echo $1 | sed -e 's/\./ /g')
        mask=$(( ($1<<24) | ($2<<16) | ($3 << 8) | $4 ))
        i=0
        while [ $i -lt 32 ]; do
                if [ $(( ($mask >> $i) & 1)) -eq 1 ]; then
                        echo $((32-$i))
                        break
                fi
                i=$(($i+1))
        done
        [ $i -eq 32 ] && echo 0
}


create_directories()
{
  if [ -e $mount_dir ]; then
		if ! [  -d $mount_dir/$MEDIADIR ]; then                        
				echo  Creating dir $mount_dir/$MEDIADIR            
				mkdir -p -m 777 $mount_dir/$MEDIADIR           
		fi
		
		if ! [ -d  $mount_dir/$SHAREDDIR ]; then                 
				echo Creating dir $mount_dir/$SHAREDDIR
				mkdir -p -m 777 $mount_dir/$SHAREDDIR                     
		fi
  fi
}

add_shared_directory()
{	
	count=$1
	path=$2
	share_name=$3
	
	if   [ $count = 0  ]; then 
	       public_name=""                             
	else       
		public_name="on $SAMBA_DEVICE_NAME$count disk"
	fi
	echo "[$share_name]" >> $config_file
#  	echo "   comment = USB shared disk $public_name " >> $config_file	
   	echo "   comment = $share_name $public_name" >> $config_file	
   	echo "   path = $mount_dir/$path/$share_name" >> $config_file
   	echo "   public = yes" >> $config_file
	echo "   writable = yes" >> $config_file
	echo "   printable = no" >> $config_file
	echo "   fstype = FAT" >> $config_file
	echo "   max connections = 3" >> $config_file
}


# this is not used anymore and is not up to date at all
update_config_file()
{
  	if [ ! -d $FLASHCONFIGDIR ]; then
  		echo Creating config dir $FLASHCONFIGDIR
    		mkdir -p $FLASHCONFIGDIR
  	fi

	echo "# smb conf file" > $config_file
	echo "[global]"	>> $config_file
	echo "   workgroup = $SAMBA_WORKGROUP" >> $config_file
	echo "   server string = Thomson Gateway Samba Server" >> $config_file
   	echo "   load printers = no" >> $config_file
	echo "   log file = $log_file" >> $config_file
	echo "   max log size = 1" >> $config_file
	echo "   security = share" >> $config_file
	echo "   encrypt passwords = yes" >> $config_file
	echo "   socket options = TCP_NODELAY" >> $config_file
	echo "   dns proxy = no" >> $config_file
	echo "   local master = yes" >> $config_file
	echo "   preferred master = yes" >> $config_file
	echo "   wins support = yes" >> $config_file
	echo "   max xmit = 65535" >> $config_file
	echo "   dfree command = /usr/sbin/dfree" >> $config_file
	echo "   deadtime = 1" >> $config_file
	echo "   keepalive = 60" >> $config_file

	level=$(niveau $NETMASK_0)
	echo "level: $level"
	echo "   interfaces=$IP_0/$level 127.0.0.1/31" >> $config_file

	if [ -d $FLASHCONFIGDIR ]; then
		echo "samba: configuration ..."
		echo "   smb passwd file = $FLASHCONFIGDIR/smbpasswd" >> $config_file	
	else				
		echo "samba: no configuration"
	fi

	
#Note that this means that other user can get into the homedirectory if they know the path
	echo "#[homes]" >> $config_file
	echo "#   comment = Home Directory" >> $config_file
	echo "#   browsable = yes" >> $config_file    
	echo "#   read only = no" >> $config_file        
	echo "#   map archive = yes" >> $config_file 

	echo "#[Disk]" >> $config_file
	echo "#   comment = Complete Disk" >> $config_file   
	echo "#   path = /dl/disk/" >> $config_file 
	echo "#   browsable = yes" >> $config_file    
        echo "#   writable = yes" >> $config_file        
	echo "#   public = yes" >> $config_file 
	echo "#   fstype = FAT" >> $config_file 
	
	create_directories
	
	add_shared_directory 0 / $MEDIADIR                 
	add_shared_directory 0 / $SHAREDDIR                      
	
#	list_dir=$(ls $mount_dir) 
#	shared_list_dir=$@
#	if [ "$#" -eq 0 ]; then
# 		shared_list_dir="$MEDIADIR $SHAREDDIR"
#	fi
#
#	count=1     
#	for directory in $list_dir; do
#	    for sdirectory in $shared_list_dir; do
#			if [ -d $mount_dir/$directory/$shared_list_dir ]; then
#				echo "samba: shared directory $mount_dir/$directory/$shared_list_dir defined"
#				add_shared_directory $count $directory $shared_list_dir
#			else				
#				echo "samba: not defined shared directory $mount_dir/$directory/$shared_list_dir"
#			fi
#		done
#		count=$(($count+1))
#	done    
	
}   



case $1 in
	start)
		[ "$ENABLE_SAMBA" != 1 ] && exit;
		$0 stop
		TELLER=0
		rm -f /var/run/cifscl
		echo "samba: initializing ..."
#		export FTP_CL_LOG_FILE_PATH=/var/tmp/smblog
                /usr/bin/cifscl -D -s $mount_dir/$SHAREDDIR -m  $mount_dir/$MEDIADIR
		while [ ! -f /var/run/cifscl ]
        do
           TELLER=`expr ${TELLER} + 1`
           echo "samba: wait for cifscl to initialize (${TELLER})"
           sleep 1;  
		   if [ "$TELLER" -eq 20 ]; then
           		echo " cifscl time out"
            	break
           fi
        done		
		echo "samba: starting ..."
		create_directories
		/usr/sbin/smbd -D 
		/usr/sbin/nmbd -D
		[ -f "/etc/init.d/btclient" ] && /etc/init.d/btclient start
		;;
	stop)
		[ -f "/etc/init.d/btclient" ] && /etc/init.d/btclient stop
		echo "samba: stopping ..."
		killall smbd
		killall nmbd
		killall cifscl
		;;
	update)
		echo "samba: updating ..."
		create_directories
		killall -HUP smbd
		killall -HUP nmbd
		;;
		#this will only restart the samba server if it was running before !!!!
	restart)
		killall smbd
		if [ $? = 0 ]; then
		    #server was running, we start it again
		    create_directories
		    /usr/sbin/smbd -D
		fi
		;;
	prepare_fs)
		create_directories
		;;
	*)
		echo "samba: do nothing"
esac
