@echo off
cd /d %~dp0 

rem 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷iss锟斤拷锟斤拷锟角凤拷锟斤拷锟斤拷
if [%1] == [] (
	echo 锟斤拷锟斤拷1未锟斤拷锟矫ｏ拷应锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷锟?
	goto ret
)

if [%2] == [] (
	echo 锟斤拷锟斤拷2未锟斤拷锟矫ｏ拷应锟斤拷锟斤拷iss锟斤拷锟斤拷锟侥硷拷
	goto ret
)

rem 锟斤拷锟斤拷
set SIGN_TOOL="..\signtool.exe"
set NETZ_EXE="..\..\netz-bin-40\netz.exe"
set MT_EXE="..\..\mt\mt.exe"
set INNO_SETUP_EXE="C:\Program Files (x86)\Inno Setup 5\ISCC.exe"

set OUTPUT_SETUP_TEMP_DIR=PackedFiles\
set OUTPUT_SETTUP_FILE_NAME=%1
set OUTPUT_SETTUP_FILE_EXT=exe
set BUILD_MAIN_EXE=ApowerRecover.exe
set PRO_MAIN_EXE=ApowerRecover.exe
set BUILD_DIR=..\..\src\Bin\Release

set PROGRAM_FILES_DIR=ProgramFiles
set UPDATE_FILES_DIR=UpdateFiles

rem 
set DevenvFile=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.com
set ProjectFile=..\..\src\PCDataRecovery.sln

rem 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
set ismain=0
if [%3] == [rebuild] (
	"%DevenvFile%" "%ProjectFile%" /rebuild "Release|x86"
	set ismain=1
)

if %ismain%==1 (
	rem 删锟斤拷原锟侥硷拷

	rd /s /q "%PROGRAM_FILES_DIR%"
	md "%PROGRAM_FILES_DIR%"

	rem 锟斤拷锟斤拷Release目录锟铰碉拷锟侥硷拷
	copy "%BUILD_DIR%\%BUILD_MAIN_EXE%.config" "%PROGRAM_FILES_DIR%\%BUILD_MAIN_EXE%.config" /Y
	copy "%BUILD_DIR%\%BUILD_MAIN_EXE%" "%PROGRAM_FILES_DIR%\%BUILD_MAIN_EXE%" /Y

	xcopy "%BUILD_DIR%\Lang\*.*" "%PROGRAM_FILES_DIR%\Lang\" /E /Q /Y
	copy "%BUILD_DIR%\ApowerDR.dll" "%PROGRAM_FILES_DIR%\ApowerDR.dll" /Y
	copy "%BUILD_DIR%\libcharset1.dll" "%PROGRAM_FILES_DIR%\libcharset1.dll" /Y
	copy "%BUILD_DIR%\libiconv2.dll" "%PROGRAM_FILES_DIR%\libiconv2.dll" /Y
	copy "%BUILD_DIR%\libntfs5.dll" "%PROGRAM_FILES_DIR%\libntfs5.dll" /Y
	
	rem 签锟斤拷
	%SIGN_TOOL% sign /t http://timestamp.digicert.com /n "Apowersoft Ltd" "%PROGRAM_FILES_DIR%\%PRO_MAIN_EXE%"
	%SIGN_TOOL% sign /t http://timestamp.digicert.com /n "Apowersoft Ltd" "%PROGRAM_FILES_DIR%\ApowerDR.dll"

	rem 准锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷
	echo 准锟斤拷锟斤拷锟斤拷锟侥硷拷
	rd /s /q "%UPDATE_FILES_DIR%"
	md "%UPDATE_FILES_DIR%"
	
	copy "%PROGRAM_FILES_DIR%\%PRO_MAIN_EXE%" "%UPDATE_FILES_DIR%\%PRO_MAIN_EXE%" /Y
	xcopy "%PROGRAM_FILES_DIR%\Lang\*.*" "%UPDATE_FILES_DIR%\Lang\" /E /Q /Y
	xcopy "%PROGRAM_FILES_DIR%\ApowerDR.dll" "%UPDATE_FILES_DIR%\" /E /Q /Y
)

rem 锟斤拷锟?
echo Packing pkg...
%INNO_SETUP_EXE%  /Qp /O"%OUTPUT_SETUP_TEMP_DIR%" /F"%OUTPUT_SETTUP_FILE_NAME%" "IssFiles\%2" 

rem 签锟斤拷
echo %OUTPUT_SETUP_TEMP_DIR%%OUTPUT_SETTUP_FILE_NAME%
set TEMP_SETUP_FILE="%OUTPUT_SETUP_TEMP_DIR%%OUTPUT_SETTUP_FILE_NAME%.%OUTPUT_SETTUP_FILE_EXT%"
%SIGN_TOOL% sign /t http://timestamp.digicert.com /n "Apowersoft Ltd" %TEMP_SETUP_FILE%

echo "--------锟斤拷锟斤拷锟缴ｏ拷锟斤拷锟街讹拷锟斤拷锟斤拷锟斤拷傻锟斤拷募锟?--------"

:ret
if [%3] == [endpause] pause
if [%4] == [endpause] pause
