# This script controls actions to be taken when the tethering is started or stopped.
# @Version=93
# ------------------------------------------------------------------------------------
# It uses the "edify" language, which is also used for the android OTA update scripts. 
# See:
# http://android.git.kernel.org/?p=platform/bootable/recovery.git;a=tree;f=edify;h=04720f8aaa9a5e0079b79f8be7f11b7f74414162;hb=HEAD
# ------------------------------------------------------------------------------------

#
# Actions for starting tethering
#
action() == "start" && (
  #
  # Set "status"-Property
  #
  setprop("tether.status","running");

  #
  # Make sure to kill old processes
  #
  kill_pidfile("/data/data/android.tether/var/fixpersist.pid");
  kill_pidfile("/data/data/android.tether/var/fixroute.pid");
  file_unlink("/data/data/android.tether/var/fixpersist.pid");
  file_unlink("/data/data/android.tether/var/fixroute.pid");

  #
  # Wifi mode, do some wifi things...
  #
  getcfg("tether.mode") == "wifi" && (
	#
	# Set "status"-Property
	#
    setprop("tether.mode","wifi");
  
    getcfg("device.type") == "nexusone" && (
      #
      # Nexus One.
      #
      !is_substring("softap", getcfg("wifi.driver")) && (
		  #
		  # Loading firmware
		  #
		  file_exists("/sdcard/android.tether/fw_bcm4329.bin") && (
	      	module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", "firmware_path=/sdcard/android.tether/fw_bcm4329.bin"), "Loading bcm4329.ko module<br>(fw_bcm4329.bin from /sdcard/android.tether)");
	      );      
	      !file_exists("/sdcard/android.tether/fw_bcm4329.bin") && (
	        file_exists("/etc/firmware/fw_bcm4329_ap.bin") && (
	        	module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", "firmware_path=/etc/firmware/fw_bcm4329_ap.bin"), "Loading bcm4329.ko module<br>(fw_bcm4329_ap.bin from /etc/firmware)");
	        );
	        !file_exists("/etc/firmware/fw_bcm4329_ap.bin") && (
	      		module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", ""), "Loading bcm4329.ko module");
	      	);
	      );
	      sleep("3");
	      #
	      # Bring up wifi interface
	      #
	      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
	      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
	      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
	      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	      getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	      );
	   );
	   is_substring("softap", getcfg("wifi.driver")) && (
		  #
		  # Loading firmware
		  #	   
		  file_exists("/sdcard/android.tether/fw_bcm4329.bin") && (
	      	module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", "firmware_path=/sdcard/android.tether/fw_bcm4329.bin"), "Loading bcm4329.ko module<br>(fw_bcm4329.bin from /sdcard/android.tether)");
	      );      
	      !file_exists("/sdcard/android.tether/fw_bcm4329.bin") && (
	        file_exists("/etc/firmware/fw_bcm4329_ap.bin") && (
	        	module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", "firmware_path=/etc/firmware/fw_bcm4329_ap.bin"), "Loading bcm4329.ko module<br>(fw_bcm4329_ap.bin from /etc/firmware)");
	        );
	        file_exists("/etc/firmware/fw_bcm4329_apsta.bin") && (
	      		module_loaded("bcm4329") || log(insmod("/system/lib/modules/bcm4329.ko", "firmware_path=/etc/firmware/fw_bcm4329_apsta.bin"), "Loading bcm4329.ko module<br>(fw_bcm4329_apsta.bin from /etc/firmware)");
	      	);
	      );	
	      sleep("3");
	      getcfg("wifi.driver") == "softap_htc1" && (
	        log(run_program("/data/data/android.tether/bin/ultra_bcm_config "+getcfg("wifi.interface")+" softap_htc "+ getcfg("wifi.essid")+" "+getcfg("wifi.encryption")+" "+getcfg("wifi.encryption.key")+" "+getcfg("wifi.channel")), "Starting access-point");	      
	      );
	      getcfg("wifi.driver") == "softap_htc2" && (
	        log(run_program("/data/data/android.tether/bin/ultra_bcm_config "+getcfg("wifi.interface")+" softap_gog "+ getcfg("wifi.essid")+" "+getcfg("wifi.encryption")+" "+getcfg("wifi.encryption.key")+" "+getcfg("wifi.channel")), "Starting access-point");	      
	      );	      
	      getcfg("wifi.driver") == "softap_gog" && (
	        log(run_program("/data/data/android.tether/bin/ultra_bcm_config eth0 "+getcfg("wifi.driver")+" "+ getcfg("wifi.essid")+" "+getcfg("wifi.encryption")+" "+getcfg("wifi.encryption.key")+" "+getcfg("wifi.channel")), "Starting access-point");	      
	        run_program("/data/data/android.tether/bin/ifconfig eth0 down");
	        run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " down");
	      );
	      #
	      # Bring up wifi interface
	      #
	      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
	                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
	   );
    );
    
    is_substring("galaxy", getcfg("device.type")) && (
	  file_exists("/sdcard/android.tether/rtecdc.bin") && (
 	      module_loaded("bcm4325") || log(insmod("/system/libmodules/bcm4325.ko",
	                                             	"firmware_path=/sdcard/android.tether/rtecdc.bin nvram_path=/etc/nvram.txt"),
	                                      			"Loading bcm4325.ko module<br>(rtecdc.bin from /sdcard/android.tether)");
	  );     
      !file_exists("/sdcard/android.tether/rtecdc.bin") && (
		  !file_exists("/etc/rtecdc_adhoc.bin") && (
		      module_loaded("bcm4325") || log(insmod("/system/libmodules/bcm4325.ko", 
		      										"firmware_path=/etc/rtecdc.bin nvram_path=/etc/nvram.txt"), 
		      										"Loading bcm4325.ko module<br>(rtecdc.bin from /system/etc)");
		  );  
		  file_exists("/etc/rtecdc_adhoc.bin") && (
		      module_loaded("bcm4325") || log(insmod("/system/libmodules/bcm4325.ko", 
		      										"firmware_path=/etc/rtecdc_adhoc.bin nvram_path=/etc/nvram.txt"), 
		      										"Loading bcm4325.ko module<br>(rtecdc_adhoc.bin from /system/etc)");
		  );
	  );  
      sleep("3");
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
      getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );
      run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");                  
    );
    
    getcfg("device.type") == "dream" && (
      #
      # HTC dream/magic/tattoo/eris/hero
      #      
	  module_loaded("wlan") || log(insmod("/system/lib/modules/wlan.ko", ""), "Loading wlan.ko module");
      file_exists("/proc/calibration") && (
	      file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
	 	      log(run_program("/system/bin/wlan_loader -f /sdcard/android.tether/Fw1251r1c.bin -e " +
		                  "/proc/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /sdcard/android.tether)");
	      );
	      !file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
		      file_exists("/system/etc/wifi/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e " +
			                  "/proc/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/wifi)");
		      );
		      file_exists("/system/etc/firmware/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/firmware/Fw1251r1c.bin -e " +
			                  "/proc/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/firmware)");
		      );      
	      );
      );
      file_exists("/data/calibration") && (
 	      file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
	 	      log(run_program("/system/bin/wlan_loader -f /sdcard/android.tether/Fw1251r1c.bin -e " +
		                  "/data/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /sdcard/android.tether)");
	      );
	      !file_exists("/sdcard/android.tether/Fw1251r1c.bin") && (
		      file_exists("/system/etc/wifi/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e " +
			                  "/data/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/wifi)");
		      );
		      file_exists("/system/etc/firmware/Fw1251r1c.bin") && (
			      log(run_program("/system/bin/wlan_loader -f /system/etc/firmware/Fw1251r1c.bin -e " +
			                  "/data/calibration -i /data/data/android.tether/conf/tiwlan.ini"), "Configuring WiFi interface<br>(Fw1251r1c.bin from /system/etc/firmware)");
		      );      
	      );     
      );
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
    );
	
    getcfg("device.type") == "moment" && (
      #
      # Samsung Moment
      #
      file_exists("/sdcard/android.tether/rtecdc.bin") && (
      	module_loaded("dhd") || log(insmod("/lib/modules/dhd.ko", "firmware_path=/sdcard/android.tether/rtecdc.bin nvram_path=/etc/nvram.txt"), "Loading dhd.ko module");
      );      
      !file_exists("/sdcard/android.tether/rtecdc.bin") && (
        !file_exists("/etc/rtecdc_adhoc.bin") && (
      		module_loaded("dhd") || log(insmod("/lib/modules/dhd.ko", "firmware_path=/etc/rtecdc.bin nvram_path=/etc/nvram.txt"), "Loading dhd.ko module");
      	);
        file_exists("/etc/rtecdc_adhoc.bin") && (
      		module_loaded("dhd") || log(insmod("/lib/modules/dhd.ko", "firmware_path=/etc/rtecdc_adhoc.bin nvram_path=/etc/nvram.txt"), "Loading dhd.ko module");
      	);
      );      
      sleep("3");
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	  getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );      
      run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
    );

    getcfg("device.type") == "legend" && (
      #
      # HTC Legend
      #
      module_loaded("sdio") || log(insmod("/system/lib/modules/sdio.ko",""), "Loading sdio.ko module");
      module_loaded("tiwlan_drv") || log(insmod("/system/lib/modules/tiwlan_drv.ko", ""), "Loading tiwlan_drv.ko module");
      
      file_exists("/system/bin/tiwlan_loader") && (
	      file_exists("/proc/calibration") && (
	      	run_program("/system/bin/tiwlan_loader -f /system/etc/wifi/Fw1273_CHIP.bin -e " +
	                  	"/proc/calibration -i /system/etc/wifi/tiwlan.ini");
	      );
	      file_exists("/data/calibration") && (
	      	run_program("/system/bin/tiwlan_loader -f /system/etc/wifi/Fw1273_CHIP.bin -e " +
	                  	"/data/calibration -i /system/etc/wifi/tiwlan.ini");
	      );     
      );
      file_exists("/system/bin/wlan_loader") && (
	      file_exists("/proc/calibration") && (
	      	run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1273_CHIP.bin -e " +
	                  	"/proc/calibration -i /system/etc/wifi/tiwlan.ini");
	      );
	      file_exists("/data/calibration") && (
	      	run_program("/system/bin/wlan_loader -f /system/etc/wifi/Fw1273_CHIP.bin -e " +
	                  	"/data/calibration -i /system/etc/wifi/tiwlan.ini");
	      );     
      );
      sleep("3");
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " +getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	  getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );      
      run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
    );

    getcfg("device.type") == "ally" && (
      #
      # LG Ally
      #
  	  module_loaded("wireless") || log(insmod("/system/lib/modules/wireless.ko", "firmware_path=/system/etc/wl/rtecdc.bin nvram_path=/system/etc/wl/nvram.txt config_path=/data/misc/wifi/config"), "Loading wireless.ko module");
      sleep("3");
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	  getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );
      run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
    );
    
    
    getcfg("device.type") == "droidx" && (
  	  #
  	  # DroidX
  	  #
   	  module_loaded("tiap_drv") || log(insmod("/system/lib/modules/tiap_drv.ko", ""), "Loading tiap_drv.ko module");
      run_program("/system/bin/wlan_loader -f /system/etc/wifi/fw_tiwlan_ap.bin -i /system/etc/wifi/tiwlan_ap.ini -e /pds/wifi/nvs_map.bin");
      sleep("3");      

      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      sleep("3");
	  #
	  # Start hostapd
      #
      log(run_program("/system/bin/Hostapd -P /data/data/android.tether/var/hostapd.pid -B /data/data/android.tether/conf/hostapd.conf"), "Starting hostapd");
    );

    getcfg("device.type") == "blade" && (
  	  #
  	  # ZTE Blade
  	  #
   	  module_loaded("ar6000") || log(insmod("/system/wifi/ar6000.ko", ""), "Loading ar6000.ko module");
      sleep("3");      

      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      sleep("3");
	  #
	  # Start hostapd
      #
      log(run_program("/system/bin/hostapd -P /data/data/android.tether/var/hostapd.pid -B /data/data/android.tether/conf/hostapd.conf"), "Starting hostapd");
    );
    
    getcfg("device.type") == "generic" && (
      #
      # Generic device
      # 
      log(load_wifi(), "Loading WiFi driver");
      
      #
      # Bring up wifi interface
      #
      log(run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " " + getcfg("ip.gateway") + " netmask " + getcfg("ip.netmask")) &&
                  run_program("/data/data/android.tether/bin/ifconfig " + getcfg("wifi.interface") + " up"),  "Activating WiFi interface");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " mode ad-hoc"), "Setting ad-hoc mode");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " essid " + getcfg("wifi.essid")), "Setting essid");
      log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " channel " + getcfg("wifi.channel")), "Setting channel");
	  getcfg("wifi.txpower") != "disabled" && (
	      	log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " txpower " + getcfg("wifi.txpower")), "Setting transmit power");     	
	  );      
      run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
    );
    
    #
    # WEP-Encryption
    #
    getcfg("wifi.encryption") == "wep" && (
      getcfg("wifi.setup") == "iwconfig" && (      	
        log(run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " key s:"+getcfg("wifi.encryption.key")+"") &&
          run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " key restricted"), "Activating encryption<br/>(iwconfig)");
        run_program("/data/data/android.tether/bin/iwconfig " + getcfg("wifi.interface") + " commit");
      );
      getcfg("wifi.setup") == "wpa_supplicant" && (      	
        sleep("2");
	log(run_program("cd /data/local/tmp; mkdir /data/local/tmp/wpa_supplicant; wpa_supplicant -B -D" +
	  getcfg("wifi.driver") +
	  " -i" + getcfg("wifi.interface") +
	  " -c/data/data/android.tether/conf/wpa_supplicant.conf"), "Activating encryption<br/>(wpa_supplicant)");
      );
    );

    #
    # Starting persist-fixer in background
    #    
    getcfg("tether.fix.persist") == "true" && (
      file_exists("/data/data/android.tether/bin/fixpersist.sh") && (
		run_program("/data/data/android.tether/bin/fixpersist.sh "+ getcfg("wifi.interface") +" "+ getcfg("wifi.essid") +" "+ getcfg("wifi.channel") +" & echo $! > /data/data/android.tether/var/fixpersist.pid");
      );
    );
  );

  getcfg("tether.mode") == "bt" && (
    #
    # Set "mode"-Property
    #
    setprop("tether.mode","bt");

    #
    # Bluetooth - start pand
    #
    run_program("/data/data/android.tether/bin/pand --listen --role NAP " +
                "--devup /data/data/android.tether/bin/blue-up.sh " +
                "--devdown /data/data/android.tether/bin/blue-down.sh " +
                "--pidfile /data/data/android.tether/var/pand.pid");
  );

  #
  # Starting route-fixer in background
  #
  getcfg("tether.fix.route") == "true" && (
    file_exists("/data/data/android.tether/bin/fixroute.sh") && (
  	  file_exists("/system/bin/ip") && (
  		getcfg("tether.mode") == "wifi" && (
  			run_program("/data/data/android.tether/bin/fixroute.sh "+ getcfg("wifi.interface") +" "+ getcfg("ip.network") +"/24 "+ getcfg("ip.gateway") +" & echo $! > /data/data/android.tether/var/fixroute.pid");
  		);
  		getcfg("tether.mode") == "bt" && (
  			run_program("/data/data/android.tether/bin/fixroute.sh bnep0 "+ getcfg("ip.network") +"/24 "+ getcfg("ip.gateway") +" & echo $! > /data/data/android.tether/var/fixroute.pid");
  		);
  	  );
    );
  );	

  #
  # Remove old rules
  #
  run_program("/data/data/android.tether/bin/iptables -N wireless-tether");
  run_program("/data/data/android.tether/bin/iptables -F wireless-tether");  
  run_program("/data/data/android.tether/bin/iptables -t nat -F PREROUTING");
  run_program("/data/data/android.tether/bin/iptables -t nat -F POSTROUTING");
  run_program("/data/data/android.tether/bin/iptables -t nat -F");

  #
  # Bring up NAT rules
  #
  log(
    run_program("/data/data/android.tether/bin/iptables -A wireless-tether -m state --state ESTABLISHED,RELATED -j ACCEPT") &&
    run_program("/data/data/android.tether/bin/iptables -A wireless-tether -s " + getcfg("ip.network") + "/24 -j ACCEPT") &&
    run_program("/data/data/android.tether/bin/iptables -A wireless-tether -p 47 -j ACCEPT") &&
    run_program("/data/data/android.tether/bin/iptables -A wireless-tether -j DROP") &&
    run_program("/data/data/android.tether/bin/iptables -A FORWARD -m state --state INVALID -j DROP") &&
    run_program("/data/data/android.tether/bin/iptables -A FORWARD -j wireless-tether") &&
    run_program("/data/data/android.tether/bin/iptables -t nat -I POSTROUTING -s " +
               getcfg("ip.network") + "/24 -j MASQUERADE"),
    "Enabling NAT rules");

  #
  # IP forwarding
  #
  log(file_write("/proc/sys/net/ipv4/ip_forward", "1"), "Enabling IP forwarding");

  #
  # dnsmasq for wifi tether (bluetooth has pand start it)
  #
  getcfg("tether.mode") == "wifi" &&
      run_program("/data/data/android.tether/bin/dnsmasq -i " + getcfg("wifi.interface") +" "+
                  "--resolv-file=/data/data/android.tether/conf/resolv.conf " +
                  "--conf-file=/data/data/android.tether/conf/dnsmasq.conf");

  #
  # Access control
  #
  file_exists("/data/data/android.tether/conf/whitelist_mac.conf") &&
    log(
      run_program("/data/data/android.tether/bin/iptables -t nat -I PREROUTING -s " + getcfg("ip.network") + "/24 -j DROP") &&
      whitelist_macs("/data/data/android.tether/conf/whitelist_mac.conf"),
    "Enabling access control."
    );
  log("Tethering now running");
);

#
# Actions when stopping tether
#
action() == "stop" && (
  #
  # Set "status"-Property
  #
  setprop("tether.status","stopped");

  #
  # Disable forwarding and remove NAT rules.
  #
  log(file_write("/proc/sys/net/ipv4/ip_forward", "0"), "Disabling forwarding");
  
  log(
    run_program("/data/data/android.tether/bin/iptables -D FORWARD -j wireless-tether") &&
    run_program("/data/data/android.tether/bin/iptables -D FORWARD -m state --state INVALID -j DROP") &&
    run_program("/data/data/android.tether/bin/iptables -F wireless-tether") &&  
    run_program("/data/data/android.tether/bin/iptables -X wireless-tether") && 
    run_program("/data/data/android.tether/bin/iptables -t nat -F PREROUTING") &&
    run_program("/data/data/android.tether/bin/iptables -t nat -F POSTROUTING") &&
    run_program("/data/data/android.tether/bin/iptables -t nat -F"),
  "Disabling NAT rules");

  #
  # Bluetooth, kill pand and and dnsmasq processes
  #
  getcfg("tether.mode") == "bt" && (
    run_program("/data/data/android.tether/bin/pand -K");
    sleep("1");
    kill_process("pand");
    file_unlink("/data/data/android.tether/var/pand.pid");
    kill_process("dnsmasq");
    kill_pidfile("/data/data/android.tether/var/fixroute.pid");
    file_unlink("/data/data/android.tether/var/fixroute.pid");
  );
  
  #
  # Wifi mode, bring interface down, kill dnsmasq/wpa_supplicant, remove module.
  #
  getcfg("tether.mode") == "wifi" && (
    kill_process("wpa_supplicant");
    kill_process("dnsmasq");
    kill_pidfile("/data/data/android.tether/var/hostapd.pid");
	kill_pidfile("/data/data/android.tether/var/fixpersist.pid");
	kill_pidfile("/data/data/android.tether/var/fixroute.pid");
	file_unlink("/data/data/android.tether/var/fixpersist.pid");
	file_unlink("/data/data/android.tether/var/fixroute.pid");
    run_program("/data/data/android.tether/bin/ifconfig " +  getcfg("wifi.interface") + " down");
    
    getcfg("device.type") == "generic" && (
      unload_wifi();
    );

    module_loaded("bcm4329") && rmmod("bcm4329");
    module_loaded("bcm4325") && rmmod("bcm4325");
    module_loaded("wlan") && rmmod("wlan");
    module_loaded("tiwlan_drv") && rmmod("tiwlan_drv");
    module_loaded("tiap_drv") && rmmod("tiap_drv");
    module_loaded("sdio") && rmmod("sdio");
    module_loaded("dhd") && rmmod("dhd");
    module_loaded("wireless") && rmmod("wireless");
    module_loaded("ar6000") && rmmod("ar6000");
  );

  #
  # Remove old dnsmasq.leases and pid-file
  #
  file_exists("/data/data/android.tether/var/dnsmasq.leases") && (
  	file_unlink("/data/data/android.tether/var/dnsmasq.leases");
  );
  file_exists("/data/data/android.tether/var/dnsmasq.pid") && (
    file_unlink("/data/data/android.tether/var/dnsmasq.pid");
  );
  file_exists("/data/data/android.tether/var/setssid.pid") && (
    file_unlink("/data/data/android.tether/var/setssid.pid");
  );
  log("Tethering now stopped");
);

#
# Actions when restarting access control
#
action() == "restartsecwifi" && (
  #
  # Remove old rules
  #
  log (
    run_program("/data/data/android.tether/bin/iptables -t nat -F PREROUTING") &&
    run_program("/data/data/android.tether/bin/iptables -t nat -F POSTROUTING"),
  "Disabling NAT rules");

  #
  # Bring up NAT rules
  #
  log(
    run_program("/data/data/android.tether/bin/iptables -t nat -I POSTROUTING -s " +
               getcfg("ip.network") + "/24 -j MASQUERADE"),
    "Enabling NAT rules");

  #
  # Access control
  #
  file_exists("/data/data/android.tether/conf/whitelist_mac.conf") &&
    log(
      run_program("/data/data/android.tether/bin/iptables -t nat -I PREROUTING -s " + getcfg("ip.network") + "/24 -j DROP") &&
      whitelist_macs("/data/data/android.tether/conf/whitelist_mac.conf"),
    "Enabling access control."
    );
);