@rem The variable INSDIR can be changed to the folder path you want to install this application.
@rem But other variables can NOT be changed.
@echo off 
@echo 安装程序正在进行，请稍候……
set SOFT_NAME=HPVisionTerm
set COMP_NAME=Hewlett-Packard
set VERSION=1.4.02.00
set DISPVER=1.4.02.00


:: *************************************************************************
:: Here is the Install Directory that could be modified before installation.
:: *************************************************************************
set "INSDIR=%ProgramFiles%\%COMP_NAME%\%SOFT_NAME%"


:: Predefined variables.
set "SOFT_PUBLISHER=China Hewlett-Packard Co., Ltd."
set "URL=http://www.hp.com"

set "UNINS_STR=%INSDIR%\Uninstall.bat"

set "PROFILE_PATH=%AllUserSprofile%\Application Data\HpTelnet"

set "INS_REG_KEY=HKLM\Software\%COMP_NAME%\%SOFT_NAME%"
set "UNINS_REG_KEY=HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\%SOFT_NAME%"

:: Other options.
set ShowLog=0


@rem *************************************************************************************
@rem				Check register items
@rem *************************************************************************************

@reg query "%UNINS_REG_KEY%"
@if errorlevel 1 goto CheckConfigReg
@echo 系统中已经安装%SOFT_NAME%，请先卸载再安装。
@goto InstallEnd

:CheckConfigReg
@reg query "%INS_REG_KEY%" /v ProfilePath
@if errorlevel 1 goto ExitSetInsPath
@set choice=
@echo 发现系统中存在遗留的连接配置文件
@set /p "choice=是否直接使用该连接配置(Y/n)？"
@if not "%choice%"=="" set "choice=%choice:~0,1%"
@if /i "%choice%"=="n" goto ExitSetInsPath

@for /f "tokens=2*" %%i in ('reg query "%INS_REG_KEY%" /v ProfilePath') do set "PROFILE_PATH=%%j"

:ExitSetInsPath

@rem *************************************************************************************
@rem				Set install path
@rem *************************************************************************************

@set choice=
@echo 软件将安装在%INSDIR%下。
set /p "choice=是否更改(y/N)？"
@if not "%choice%"=="" set "choice=%choice:~0,1%"
@if /i not "%choice%"=="y" goto ExitSetInsPath

:InputInsPath
set newpath=
set /p "newpath=请输入新的安装路径:"
if not "%newpath%"=="" goto SetNewInsPath
@echo 路径不能为空，请重新输入。
goto InputInsPath

:SetNewInsPath
set "INSDIR=%newpath%"

:ExitSetInsPath

@rem *************************************************************************************
@rem				Set Session config path
@rem *************************************************************************************

set choice=
@echo 软件连接配置文件将安装在%PROFILE_PATH%下。
set /p "choice=是否更改(y/N)?"
if not "%choice%"=="" set "choice=%choice:~0,1%"
if /i not "%choice%"=="y" goto ExitSetConfPath

:InputConfPath
set newpath=
set /p "newpath=请输入新的连接配置安装路径:"
if not "%newpath%"=="" goto SetNewConfPath
@echo 路径不能为空，请重新输入。
goto InputConfPath

:SetNewConfPath
set "PROFILE_PATH=%newpath%"

:ExitSetConfPath

@rem *************************************************************************************
@rem				Copy files
@rem *************************************************************************************

@ECHO OFF
@ECHO\
@ECHO 正在复制文件……

@Set "curPath=%~dp0%"
@Set "curPath=%curPath:~0,-1%"

@rem Delete the install directory first.
@rd /s/q "%INSDIR%"

@rem Copy all the files and subdirectories in current location into install directory.
xcopy /E /Y /I "%curPath%" "%INSDIR%"

@rem Add additional attributes to the uninstall script file.
attrib +r "%INSDIR%\Uninstall.bat"

@rem Remove the useless files from the install path.
del /f/q "%INSDIR%\setup.bat"


@rem *************************************************************************************
@rem				Convert the old version session configuration files into the new format
@rem *************************************************************************************

@ECHO\
@ECHO 正在检查前期版本连接配置并转换……
@ECHO\
@rem Convert the old version session configuration files into the new format.

@Echo off

:: Set the log stream.
Set "LogFile=%INSDIR%\Conversion.log"

:: Log contents to screen.
::Set "LogFile=con"

:: Ignore log.
::Set "LogFile=nul"

:: Get the current path without the path delimiter.
Set "CUR_DIR=%~sdp0%"
Set "CUR_DIR=%CUR_DIR:~0,-1%"

Set "FILE_TASKNAME=%PROFILE_PATH%\taskname"
Set "FILE_TGLOBLE=%CUR_DIR%\tglobal.conf"
Set "FILE_CONVERT=%CUR_DIR%\SessionConvert.exe"

@Set HasLog=0
@rem No need to convert if not session is there.
@If not Exist "%FILE_TASKNAME%" (echo 无法找到可用于转换的配置文件--"%FILE_TASKNAME%" && goto EndConversion)
@If not Exist "%FILE_TGLOBLE%" (echo 无法找到全局路径配置文件--"%FILE_TGLOBLE%" && goto EndConversion)
@If not Exist "%FILE_CONVERT%" (echo 无法找到转换工具--"%FILE_CONVERT%" && goto EndConversion)

@rem Clear the variables.
@Set Session_File=0
@Set Sessions_Folder=0
@Set var=0

@rem Read the file tglobal.
@for /f "eol=# tokens=1,2 delims==" %%a in (%FILE_TGLOBLE%) do @(

@rem The whitespace in the variables are reserved intentionally.
if /i "%%a"=="SESSION_FILE " Set "Session_File=%%b"
if /i "%%a"=="SESSION_FOLDER " Set "Sessions_Folder=%%b")

@rem Remove the whitespaces of the string at the left end.
for /f "tokens=*" %%i in ("%Session_File%") do Set "Session_File=%%i"
for /f "tokens=*" %%i in ("%Sessions_Folder%") do Set "Sessions_Folder=%%i"

@if NOT defined Session_File @(
@rem Use the default path.
@echo "Using default session list file."
Set "Session_File=%PROFILE_PATH%\Sessions.lst")


@if NOT defined Sessions_Folder @(
@rem Use the default path.
@echo "Using default sessions folder"
Set "Sessions_Folder=%PROFILE_PATH%\sessions")

@Set HasLog=1

@rem Start conversion.
%FILE_CONVERT% "%PROFILE_PATH%" "%Session_File%" "%Sessions_Folder%">"%LogFile%"

@ECHO 配置文件转换完毕
@ECHO\


:EndConversion

@rem *************************************************************************************
@rem				Add register items
@rem *************************************************************************************

@Echo off
@ECHO\
@ECHO 正在添加注册表项……

reg add "%INS_REG_KEY%" /f
reg add "%INS_REG_KEY%" /v Path /t REG_SZ /d "%INSDIR%" /f
reg add "%INS_REG_KEY%" /v ProfilePath /t REG_SZ /d "%PROFILE_PATH%" /f
reg add "%INS_REG_KEY%" /v Version /t REG_SZ /d "%VERSION%" /f

reg add "%UNINS_REG_KEY%" /f
reg add "%UNINS_REG_KEY%" /v DisplayName /t REG_SZ /d "%SOFT_NAME%" /f
reg add "%UNINS_REG_KEY%" /v DisplayVersion /t REG_SZ /d "%DISPVER%" /f
reg add "%UNINS_REG_KEY%" /v Publisher /t REG_SZ /d "%SOFT_PUBLISHER%" /f
reg add "%UNINS_REG_KEY%" /v URLInfoAbout /t REG_SZ /d "%URL%" /f
reg add "%UNINS_REG_KEY%" /v Version /t REG_SZ /d "%VERSION%" /f
reg add "%UNINS_REG_KEY%" /v HelpLink /t REG_SZ /d "%URL%" /f
reg add "%UNINS_REG_KEY%" /v UninstallString /t REG_SZ /d "%UNINS_STR%" /f
reg add "%UNINS_REG_KEY%" /v NoModify /t REG_DWORD /d 00000001 /f
reg add "%UNINS_REG_KEY%" /v NoRepair /t REG_DWORD /d 00000001 /f


@rem *************************************************************************************
@rem				delete old desktop shortcut
@rem *************************************************************************************

::@ECHO\
::@ECHO 正在删除前期版本生成的桌面快捷方式……
::@ECHO\

::@rem Delete the shortcut links on Desktop.
::Set "ShortcutRemover=%INSDIR%\RemoveShortcuts.vbs"
::@If Exist "%ShortcutRemover%" (cscript "%ShortcutRemover%")

:: Show the log file.
::@if /i "%LogFile%"=="con" goto ExitRoutine
::@if /i "%LogFile%"=="nul" goto ExitRoutine
::@if /i "%ShowLog%"=="0" goto ExitRoutine
::@if /i "%HasLog%"=="0" goto ExitRoutine
::Start /B notepad.exe "%LogFile%"

:::ExitRoutine

@ECHO\
@ECHO 安装完成
@ECHO\
@ECHO\


@rem Delete the installation files.
rd /s/q "%curPath%\conf"
rd /s/q "%curPath%\i386"
del /q "%curPath%\*.*"

:InstallEnd
ECHO ON

:: This script file has been be removed and nothing could be executed after this line.

