<?php
	// iniファイルから V2Version.h を作成する 2009/06/11
	// XXX x.xx.xxxの形に変更 2014/01/07
	// 一定時間内の再呼び出しは無視する 2015/04/01
	// フォルダー名からバージョン番号を作成する 2015/11/10
	$inifilename = "0build.ini";
	$headfilename = "0Version.head";
	$outfilename = "version.h";
	$checkmin = 3;												// 無視時間（分）

	mb_language("Japanese");
	mb_internal_encoding("SJIS-win");
	date_default_timezone_set("Asia/Tokyo");

	echo ">>> make " . $inifilename . " Start.\n";

	// デバッグ用
	$debug_print = 0;

	$cntdir = getcwd();																	// カレントディレクトリーの取得
	//echo $cntdir . "\n";
	$cntpos = mb_strrpos($cntdir,"\\");
	$cntdir = mb_substr($cntdir,$cntpos + 1,mb_strlen($cntdir) - $cntpos);	// +1 = \ の分
	//echo $cntdir . "\n";
	$cntnum = mb_substr($cntdir,0,6);										// 6桁分取り出し
	//echo $cntnum . "\n";
	$mjno = mb_substr($cntnum,0,1);
	$mnno = mb_substr($cntnum,1,2);
	$rvno = mb_substr($cntnum,3,3);

	// セクションを意識してiniをパースします。
	$iniarr = @parse_ini_file($inifilename , TRUE);
	if($iniarr === FALSE){
		echo "*** " . $inifilename . " Open Error!\n";
		exit(-1);
	} else {
	}
	$Product = $iniarr["Str"]["Product"];
	//$mjno = $iniarr["Numeric"]["major"];
	//$mnno = $iniarr["Numeric"]["minor"];
	//$rvno = $iniarr["Numeric"]["rev"];
	$bdno = $iniarr["Numeric"]["build"];

	//printf("%04d\n",$bdno);
	//exit;

	if(@file_exists($outfilename) == TRUE){
		// ファイルの変更日時取得
		$editday = @filemtime($outfilename);
		// 今から5分前
		$afterday = mktime(date("H"),date("i") - $checkmin,date("s"),date("m"),date("d"),date("Y"));
		if(date("YmdHis",$afterday) < date("YmdHis",$editday)){
			echo "<<< not make " . date("Y/m/d H:i:s",$editday) . " End.\n";
			exit(-1);
		}
	}

	// 元ファイルの削除
	if( $fpr = @fopen($outfilename,"r") ){
		fclose($fpr);
		if( @unlink($outfilename) ){
		} else {
			echo "*** " . $outfilename . " Delete Error!\n";
			exit(-1);
		}
	} else {
	}

	// ヘッダーファイルのコピー
	if( @copy($headfilename , $outfilename) ){
	} else {
		echo "*** " . $headfilename . " to " . $outfilename . " Copy Error!\n";
		exit(-1);
	}

	// ファイルのオープン（追記）
	$fpw = @fopen($outfilename , "a");
	if( $fpw === FALSE ){
		echo "*** " . $outfilename . " Append Open Error!\n";
		exit(-1);
	}

	// 書き込み
	$str = "\r\n" . "#ifndef __VERSION_H__" . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define __VERSION_H__" . "\r\n\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define MAJOR\t\t" . $mjno . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define MINOR\t\t" . $mnno . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define REV\t\t\t" . $rvno . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define BUILD\t\t" . $bdno . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "#define PRODUCT_STR\t\"" . $Product . "\"\r\n\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define MAJOR_STR\t\"" . sprintf("%01d",$mjno) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define MINOR_STR\t\"" . sprintf("%02d",$mnno) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define REV_STR\t\t\"" . sprintf("%03d",$rvno) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define BUILD_STR\t\"" . sprintf("%03d",$bdno) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$datenow = time();

	$str = "#define DATE\t\t" . date("Ymd",$datenow) . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define TIME\t\t" . date("Gis",$datenow) . "\r\n";	// Hにすると先頭にゼロが付き、8進数扱いになる
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "#define DATE_STR\t\"" . date("Y/m/d",$datenow) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define TIME_STR\t\"" . date("H:i:s",$datenow) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "#define VERSION\t\t\t\t\t\"" . sprintf("%01d.%02d.%03d.%03d",$mjno,$mnno,$rvno,$bdno) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }
	$str = "#define VERSION_STRING\t\"" . sprintf("%s %01d.%02d.%03d.%03d ",substr($Product . "   " , 0 , 3),$mjno,$mnno,$rvno,$bdno) . date("Y/m/d",$datenow) . " " . date("H:i:s",$datenow) . "\"\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	$str = "#endif	/*__VERSION_H__*/" . "\r\n";
	if( fwrite($fpw , $str) === FALSE ){ echo "*** File Write Error!\n";exit(-1); }

	echo "<<< make " . date("Y/m/d H:i:s",$datenow) . " End.\n";

	exit(0);
?>
