<?php
//
// Created on: 11 Sep 2001
// Usage: sw_download.php?[language="eng|gb"]
//                        [&ip=172.18.128.100]
//                        [&bssid=0]
// 
// Function: Transfer BSS software from OMC Server
//           to BSC harddisk based on bssdist.fls file
//
//
//
//
// Remarks:
//     - Use explode() as a faster split()
//
?>
<?php
include("nocache.inc");
include("header.inc");
include("ip2name.inc");
include("read_bssdist.inc");
include("sw_download.inc");
include("bssversion.inc");
//$version="iw06_00.023";		// For testing only

//$debug=1;

if (!isset($iwbox_type))
	$iwbox_type = "bschd";
if (!isset($language))
	$language="eng";
$parameter="${language}namearray";

$trans = Array();

function assign_cpu_type($input)
{
	global $cpu_type, $trans;
	$cpu_type = $input;
	if (!strcmp($input,"iwp"))
		$trans = Array("KERNEL" => "bsp040", "PROC" => "68k");
	else
		$trans = Array("KERNEL" => "bspppc", "PROC" => "ppc");
}

/******************************************
* Get the IP address if it is not known
* 
*******************************************/
if (!isset($ip))
{
	echo "<HTML>\n";
?>
<SCRIPT language="javascript" type="text/javascript">
<!--
	function submit_check(myForm)
	{
		if (myForm.ip.value == '')
		{
			alert("Please select a valid IP address");
			return (false);
		}
		if (myForm.systype.value == '')
		{
			alert("Please select a system type");
			return (false);
		}
		window.status="<?=${$parameter}["confirm_sw_dnload"]?>";
		answer = confirm("<?=${$parameter}["confirm_sw_dnload"]?>");
		window.status="";
		if (answer != true)
		{
			return (false);
		}
		return (true);
	}
//-->
</SCRIPT>

<?php
	echo "<FORM onSubmit='return submit_check(this);'>\n";
	echo "<TABLE>\n";
	echo "<TR>\n";
	echo "<TD>\n";
	echo "<font color=red size=+1>{${$parameter}["select_ip"]}</font> ";
	echo "</TD>\n";
	echo "<TD>\n";
	echo "<SELECT name=ip size=1 style='width: 120'>\n";
	if (isset($bssid))
	{
		$tmpIp=bssid2ip($bssid);
		echo "<OPTION value=$tmpIp>$tmpIp</OPTION>\n";
	}
	else
	{
		echo "<OPTION value=''></OPTION>\n";
		echo "<OPTION value='172.18.128.100'>172.18.128.100</OPTION>\n";
		echo "<OPTION value='172.18.128.101'>172.18.128.101</OPTION>\n";
		echo "<OPTION value='172.18.128.102'>172.18.128.102</OPTION>\n";
		echo "<OPTION value='172.18.128.103'>172.18.128.103</OPTION>\n";
		echo "<OPTION value='172.18.128.104'>172.18.128.104</OPTION>\n";
		echo "<OPTION value='172.18.128.105'>172.18.128.105</OPTION>\n";
		echo "<OPTION value='172.18.128.106'>172.18.128.106</OPTION>\n";
		echo "<OPTION value='172.18.128.107'>172.18.128.107</OPTION>\n";
		echo "<OPTION value='172.18.128.108'>172.18.128.108</OPTION>\n";
		echo "<OPTION value='172.18.128.109'>172.18.128.109</OPTION>\n";
		echo "<OPTION value='172.18.128.110'>172.18.128.110</OPTION>\n";
		echo "<OPTION value='172.18.128.111'>172.18.128.111</OPTION>\n";
	}
	echo "</SELECT>\n";
	echo "</TD>\n";
	echo "</TR>\n";


	echo "<TR>\n";
	echo "<TD>\n";
	echo "<font color=red size=+1>{${$parameter}["system_type"]}</font> ";
	echo "</TD>\n";
	echo "<TD>\n";
	echo "<SELECT name=systype size=1 style='width: 80'>\n";
	echo "<OPTION value=''></OPTION>\n";
	echo "<OPTION value='bschd'>BSC</OPTION>\n";
	echo "<OPTION value='bsxhd'>BS Plus</OPTION>\n";
	echo "</SELECT>\n";
	echo "</TD>\n";

	echo "</TABLE>\n";
	echo "<BR>";
	echo "{${$parameter}["swversion"]}: <font color=blue>$version</font>";
	echo "<HR>\n";
	echo "<INPUT type=hidden name=language value=$language>\n";
	echo "<INPUT type=submit value={${$parameter}["submit"]}>\n";
	if (isset($bssid))
		echo "<INPUT type=button value='{${$parameter}[back]}' " .
		"onClick='javascript:parent.location.href=" .
		"\"modifyBssParam.php?language=$language&bssid=$bssid\"'>";
	echo "</FORM>\n";
	echo "</HTML>\n";
	return;
}



/******************************************
* Mount the BSC /hd0 to /tmp/$$ directory
*******************************************/
$tmpdir = sprintf("%.3f",time()/1000);
$iwbox = ip2name($ip);
$path_prefix = "/tmp";
$cpu_type="iwp";

$url_temp=$_SERVER["REQUEST_URI"];
echo "This page URL:$url_temp<br>";

echo "$path_prefix/$iwbox<br>";
echo "version=$version<br>";
exit();

/***************************************************************************************
* PHP state machine
*
* Rename original build and create directory
* Format: it is a 2D array
*	Array(
*         Array('function','description','next state if != 0','next state if == 0'
*       )
****************************************************************************************/
$cmdlist = Array(
	's00' => Array ('system("/bin/ping -q -c 1 $ip >> /dev/null", $retval);return($retval);',
		"{${$parameter}["ping"]} $iwbox ($ip) ...",
		"kickout","s01"),

	's01' => Array ('return(is_dir("$path_prefix/$iwbox"));',
		"{${$parameter}["checkexist"]} $path_prefix/$iwbox",
		"s03","s02"),

	's02' => Array ('return(mkdir("$path_prefix/$iwbox",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox",
		"s03","halt"),

	's03' => Array ('system("/bin/umount $path_prefix/$iwbox", $retval);return($retval);',
		"",
		//"{${$parameter}["umounting"]} $path_prefix/$iwbox",
		"s04","s03"),

	's04' => Array ('system("/bin/mount $path_prefix/$iwbox", $retval);return($retval);',
		"{${$parameter}["mounting"]} $path_prefix/$iwbox",
		"halt","s10"),

	's10' => Array ('return(is_dir(sprintf("$path_prefix/$iwbox/%s",strtoupper($version))));',
		sprintf("{${$parameter}["checkexist"]} $path_prefix/$iwbox/%s",strtoupper($version)),
		"s11","s20"),

	's11' => Array ('return(rename(sprintf("$path_prefix/$iwbox/%s",strtoupper($version)),"$path_prefix/$iwbox/$tmpdir"));',
		sprintf("{${$parameter}["rename"]} $path_prefix/$iwbox/%s --> $path_prefix/$iwbox/$tmpdir",strtoupper($version)),
		"s20","halt"),

	's20' => Array ('return(is_file("$path_prefix/$iwbox/BSCSTART"));',
		sprintf("{${$parameter}["checkexist"]} $path_prefix/$iwbox/BSCSTART"),
		"s21","s30"),

	's21' => Array ('return(rename("$path_prefix/$iwbox/BSCSTART","$path_prefix/$iwbox/BSCSTART.OLD"));',
		"{${$parameter}["rename"]} $path_prefix/$iwbox/BSCSTART --> $path_prefix/$iwbox/BSCSTART.OLD",
		"s30","halt"),

	's30' => Array ('return(is_file("$path_prefix/$iwbox/VXWORKS"));',
		sprintf("{${$parameter}["checkexist"]} $path_prefix/$iwbox/VXWORKS"),
		"s31","s40"),

	's31' => Array ('return(rename("$path_prefix/$iwbox/VXWORKS","$path_prefix/$iwbox/VXWORKS"));',
		"{${$parameter}["rename"]} $path_prefix/$iwbox/VXWORKS --> $path_prefix/$iwbox/VXWORKS",
		"s40","halt"),

	's40' => Array ('return(mkdir("$path_prefix/$iwbox/$version",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version",
		"s41","halt"),

	's41' => Array ('return(mkdir("$path_prefix/$iwbox/$version/compile",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/compile",
		"s50","halt"),

	's50' => Array ('return(mkdir("$path_prefix/$iwbox/$version/compile/68k",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/compile/68k",
		"s60","halt"),

	's60' => Array ('return(mkdir("$path_prefix/$iwbox/$version/compile/cfg",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/compile/cfg",
		"s70","halt"),

	's70' => Array ('return(mkdir("$path_prefix/$iwbox/$version/btskern",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/btskern",
		"s80","halt"),

	's80' => Array ('return(mkdir("$path_prefix/$iwbox/$version/trxxil",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/trxxil",
		"s90","halt"),

	's90' => Array ('return(mkdir("$path_prefix/$iwbox/$version/trxdsp",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/trxdsp",
		"s100","halt"),

	's100' => Array ('return(mkdir("$path_prefix/$iwbox/$version/mtdspb",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/mtdspb",
		"s101","halt"),

	's101' => Array ('return($version[3]>5);',
		"{${$parameter}["check_version"]}",
		"s110","s180"),

	's110' => Array ('system("/bin/bin/strings /tmp/$iwbox/VXWORKS|grep -i \"interWAVE IWP\"", $retval);return($retval);',
		"{${$parameter}["check_cpu_board"]}",
		"s111","s112"),

	// CPU board is ICP rather than IWP
	's111' => Array ('assign_cpu_type("iwp");return(0);',
		"{${$parameter}["processor"]}: IWP",
		"halt","s120"),

	's112' => Array ('assign_cpu_type("icp");return(0);',
		"{${$parameter}["processor"]}: ICP",
		"halt","s120"),

	's120' => Array ('return(mkdir("$path_prefix/$iwbox/$version/isr",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/isr",
		"s130","halt"),

	's130' => Array ('return(mkdir("$path_prefix/$iwbox/$version/iwlib",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/iwlib",
		"s140","halt"),

	's140' => Array ('return(mkdir("$path_prefix/$iwbox/$version/iwlib/platform",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/iwlib/platform",
		"s150","halt"),

	's150' => Array ('return(mkdir("$path_prefix/$iwbox/$version/iwlib/platform/bspppc",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/iwlib/platform/bspppc",
		"s160","halt"),

	's160' => Array ('return(mkdir("$path_prefix/$iwbox/$version/iwlib/platform/bsp040",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/iwlib/platform/bsp040",
		"s170","halt"),

	's170' => Array ('return(mkdir("$path_prefix/$iwbox/$version/compile/ppc",0700));',
		"{${$parameter}["create_dir"]} $path_prefix/$iwbox/$version/compile/ppc",
		"s180","halt"),

	's180' => Array ('return(1);',
		"*** {${$parameter}["remind"]} ***",
		"s200","halt")
);


/*********************************************
 * Get the array of BSS software filenames
 *********************************************/
$index = 200;
$output = get_sw_filenames($version,"/home/build", $iwbox_type);
if ($debug)
{
	echo "<HR>";
	foreach ($output as $files) echo "{${$parameter}["copy"]} $files[0] --> ./$files[1] <BR>";
	echo "<HR>";
	return;
}
foreach ($output as $files)
{
	$tmp_array = Array(
		"return(copy(\"" . strtr($files[0],$trans) . "\",\"/tmp/$iwbox/" . strtr($files[1],$trans) . "\"));",
		"{${$parameter}["copy"]} $files[0] --> ./$files[1]",
		sprintf("s%d",$index + 1),"halt"
	);
	$cmdlist = array_merge($cmdlist,Array("s$index" => $tmp_array));
	$index++;
}

$cmdlist = array_merge($cmdlist, Array(
	"s$index" => Array ('return(1);',
		"*** {${$parameter}["cleanup"]} ***",
		"cleanup","halt"),

	"cleanup" => Array('return(is_dir("$path_prefix/$iwbox"));',
		"{${$parameter}["checkexist"]} $path_prefix/$iwbox",
		"umount","getout"),

	"umount" => Array('system("/bin/umount $path_prefix/$iwbox", $retval);return($retval);',
		"{${$parameter}["umounting"]} $path_prefix/$iwbox",
		"rmdir","rmdir"),

	"rmdir" => Array('return(rmdir("$path_prefix/$iwbox"));',
		"{${$parameter}["rmdir"]} $path_prefix/$iwbox",
		"getout","getout")
	)
);


if (isset($debug))
{
	foreach ($cmdlist as $key => $value)
	{
		echo "$key,$value[0],$value[1],$value[2],$value[3]<BR>";
	}
}


/*********************************************
 * Execute the state machine here
 *********************************************/
echo "<font color=red size=+1>{${$parameter}["heading"]}<BR></font>";
echo "<PRE>";
for ($state="s00" ; ; )
{
	if (isset($debug))
		echo strtr("{$cmdlist[$state][0]}\n$state: ",$trans);

	echo strtr(${cmdlist}[$state][1],$trans);
	flush();
	$command = strtr(${cmdlist}[$state][0],$trans);
	if (@eval($command))
		$state = $cmdlist[$state][2];
	else
		$state = $cmdlist[$state][3];


	if ($state == "getout")
	{
		echo " ... {${$parameter}["ok"]}\n";
		break;
	}
	else if ($state == "kickout")
	{
		echo " ... {${$parameter}["not_ok"]}\n";
		echo "</PRE><font color=red size=+1>... {${$parameter}["failed"]}<BR></font>";
		echo "<PRE>";
		break;
	}
	else if ($state == "halt")
	{
		echo " ... {${$parameter}["not_ok"]}\n";
		echo "</PRE><font color=red size=+1>... {${$parameter}["failed"]}<BR></font>";
		echo "<PRE>";
		$state = "cleanup";
	}
	else
	{
		if (${cmdlist}[$state][1] != "")
			echo " ... {${$parameter}["ok"]}\n";
	}
}

echo "</PRE><font color=blue>... {${$parameter}["done"]}<BR></font>";

?>
