@echo off
set OLD_LOC1="c:\Documents and Settings\All Users\Application Data\Hewlett-Packard\HPSUM.ini"
set OLD_LOC2=c:\ProgramData\Hewlett-Packard\HPSUM.ini
set SKIP=0
set SCRIPT=0
set SCRIPT_NAME=custom_port.bat
set USER=username
set PASSWORD=passwd

echo HP Smart Update Manager 5.x to 6.x target port script
echo                       -
echo This script will attempt to find targets saved in the HP SUM 5.x INI file
echo and create similar nodes in the HP SUM 6.x database.
echo                       -
echo After porting is done, targets will show up as new nodes in HP SUM 6.x
echo and will need credentials updated and a baseline assigned.
echo                       -

IF "x%1"=="x/h" (
  echo syntax: %0 [/y] [/h] [/script] [/user {username} /password {password}
  echo /y = continue without pausing for prompts
  echo /h = print this help
  echo /script = create script instead
  echo          A file is created with commands to be run instead of running them
  echo          immediately.  The file can then be edited manually before being run.
  echo /user {username} /password {password}
  echo          Set default username and password when adding ported targets.
  echo          Default without this option is user=username and password=passwd.
  goto :EOF
)
echo For additional options, run: %0 /h	
echo                       -

:LOOP_ARGS
IF "%1"=="" GOTO DONE_ARGS
  IF "%1"=="/y"  (
    set SKIP=1
	shift
	GOTO LOOP_ARGS
  )
  IF "%1"=="/script"  (
    set SCRIPT=1
	echo Output will go to %SCRIPT_NAME%
	shift
	GOTO LOOP_ARGS
  )
  IF "%1"=="/user" (
     IF "x%2"=="x" ( 
	   echo Missing <username> after /user
	   exit -1
	 ) else (
       echo Using %2 as default username
       set USER=%2
	   shift
	 )
	 shift
	 GOTO LOOP_ARGS
  )
  IF "%1"=="/password" (
    IF "x%2"=="x" (
	  echo Missing <password> after /password
	  exit -1
	) else (
	  echo Using %2 as default password
	  set PASSWORD=%2
	  shift
	)
	shift
	GOTO LOOP_ARGS
  )
  echo Unknown argument: %1
  echo For syntax use: %0 /h
  exit -1
:DONE_ARGS


IF %SKIP%==1 goto oktoport
set /p ok="Are you sure you want to port targets? [y or n] : "

if "%ok%"=="y" goto oktoport
echo Exiting without porting any targets.
goto end

:oktoport
IF NOT EXIST hpsum.bat (
  echo Could not find HP SUM 6.x in current directory [ hpsum.bat ]
  exit -1
)

set TMPFILE=.oldtargets
IF EXIST %TMPFILE% erase %TMPFILE%

IF EXIST %OLD_LOC1% (
  echo Copying targets from HP SUM 5.x INI file at %OLD_LOC1%
  type %OLD_LOC1% | findstr address > %TMPFILE%
  goto :foundfile
)

IF EXIST %OLD_LOC2% (
  echo Copying targets from HP SUM 5.x INI file at %OLD_LOC2%
  type %OLD_LOC2% | findstr address > %TMPFILE%
  goto :foundfile
)

echo Could not find HP SUM 5.x INI file in:
echo    %OLD_LOC1%
echo    %OLD_LOC2%
echo Unable to port targets.
exit -1

:foundfile
IF %SCRIPT%==1 echo @echo off > %SCRIPT_NAME%
FOR /F "delims='=' tokens=2" %%A IN (.oldtargets) DO (
   IF "%%A" NEQ "localhost" (
      echo Porting 5.x target [ %%A ] to 6.x
	  IF %SCRIPT%==1 (
	    echo call hpsum add /nodes %%A user=%USER% password=%PASSWORD% >> %SCRIPT_NAME%
	  ) ELSE (
        call hpsum add /nodes %%A user=%USER% password=%PASSWORD%
	  )
    )
)

IF %SCRIPT%==1 (
  echo                       -
  echo %SCRIPT_NAME% now contains the commands that are needed to add the targets
  echo to HP SUM 6.0.  You can edit this file before running it in order to
  echo manually remove/add targets and change credentials if you want.
  echo Then execute:
  echo    %SCRIPT_NAME%
  echo to run the script.
 )

IF EXIST %TMPFILE% erase %TMPFILE%

:end
IF %SKIP%==0 (
  pause
)
