#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=50

#RUN_D=/var/run/samba
#NMBD_PID_F=$RUN_D/nmbd.pid
#SMBD_PID_F=$RUN_D/smbd.pid


OPKG_PATH=$(grep "dest usb" /etc/opkg.conf | awk '{print $3}')

config_cb() {
        local cfg="$CONFIG_SECTION"
        local cfgtype
        config_get cfgtype "$cfg" TYPE

        case "$cfgtype" in
                timezone)
                        config_get posixtz      $cfg posixtz
                        config_get zoneinfo     $cfg zoneinfo
			echo "$posixtz" > /etc/TZ
                ;;
        esac
}


start() {	  
	if [ -e /bin/smbd ]; then
		smbd -D
	fi
	if [ -e /bin/nmbd ]; then
		nmbd -D
	fi
}
stop() {
	killall smbd
	killall nmbd
}


install() {
	remove    
	. /sbin/vigorcode.sh
	if [ "$OPKG_PATH" != "" ]; then
	  [ -e $OPKG_PATH/bin/smbd ] && echo "smbd" || {
	  	opkg -V2 --nodeps -dest usb install "$PKG_SERVER"samba3_3.0.24-2_arm.ipk
    }
	  
	  [ -d /etc/default ] || mkdir /etc/default
	  [ -d /etc/samba ] || mkdir /etc/samba
	  
	  
		[ -e /bin/nmbd ] || {
			ln -s $OPKG_PATH/bin/nmbd /bin/nmbd
	  }
		[ -e /bin/smbd ] || { 
			ln -s $OPKG_PATH/bin/smbd /bin/smbd
	  }
		[ -e /etc/config/samba ] || { 
			ln -s $OPKG_PATH/etc/config/samba /etc/config/samba
    }
   # [ -e /etc/init.d/samba ] || {
	#		ln -s $OPKG_PATH/etc/init.d/samba /etc/init.d/samba
  #  }
	  [ -e /etc/samba/smb.conf ] || { 
		  ln -s $OPKG_PATH/etc/samba/smb.conf /etc/samba/smb.conf
	  }
	  [ -e /etc/samba/smbpasswd ] || { 
		  ln -s $OPKG_PATH/etc/samba/smbpasswd /etc/samba/smbpasswd
	  }
	  [ -e /usr/bin/smbpasswd ] || { 
		  ln -s $OPKG_PATH/usr/bin/smbpasswd /usr/bin/smbpasswd
	  }
	fi
}  
remove_ln (){
	rm -rf /bin/nmbd  
 	rm -rf /bin/smbd    
 	rm -rf /etc/config/samba 
	#rm -rf /etc/init.d/samba
 	rm -rf /etc/samba/smb.conf
 	rm -rf /etc/samba/smbpasswd
	rm -rf /usr/bin/smbpasswd
}  
remove() {
	/etc/init.d/samba stop
	remove_ln
	rm -rf $OPKG_PATH/bin/nmbd  
 	rm -rf $OPKG_PATH/bin/smbd    
 	rm -rf $OPKG_PATH/etc/config/samba 
	rm -rf $OPKG_PATH/etc/init.d/samba
 	rm -rf $OPKG_PATH/etc/samba/smb.conf
 	rm -rf $OPKG_PATH/etc/samba/smbpasswd
	rm -rf $OPKG_PATH/usr/bin/smbpasswd
}

boot() {
    [ -d /etc/default ] || mkdir /etc/default
	  [ -d /etc/samba ] || mkdir /etc/samba
	  
	  
		[ -e /bin/nmbd ] || {
			ln -s $OPKG_PATH/bin/nmbd /bin/nmbd
	  }
		[ -e /bin/smbd ] || { 
			ln -s $OPKG_PATH/bin/smbd /bin/smbd
	  }
		[ -e /etc/config/samba ] || { 
			ln -s $OPKG_PATH/etc/config/samba /etc/config/samba
    }
  # [ -e /etc/init.d/samba ] || {
	#		ln -s $OPKG_PATH/etc/init.d/samba /etc/init.d/samba
  #  }
	  [ -e /etc/samba/smb.conf ] || { 
		  ln -s $OPKG_PATH/etc/samba/smb.conf /etc/samba/smb.conf
	  }
	  [ -e /etc/samba/smbpasswd ] || { 
		  ln -s $OPKG_PATH/etc/samba/smbpasswd /etc/samba/smbpasswd
	  }
	  [ -e /usr/bin/smbpasswd ] || { 
		  ln -s $OPKG_PATH/usr/bin/smbpasswd /usr/bin/smbpasswd
	  }
	 start
}   