@echo off
REM *************************************************************
REM 03-May-04  E.Staller Option -t added second % in swproj
REM            v1.0      Update IP addr for MCH -> cleopatra
REM                      Update IP addr for SAL -> salj79xn
REM                      Update IP addr for ULM -> lhotse
REM                      Added SISL delss09
REM
REM *************************************************************
REM Does the environment variable catools exist ?
REM *************************************************************
if not defined catools (
 echo.
 echo ------------------------------------------------- 
 echo ERROR: Please set %%catools%% environment first !
 echo -------------------------------------------------
 goto error )

if "%1"=="-t" (
 echo.
 echo ---------------------------------------------------- 
 echo  These tools are available for current project %swproj%
 echo ---------------------------------------------------- 
 dir /b /l %catools%\project\%swproj%\*.exe
 dir /b /l %catools%\project\%swproj%\*.bat
 echo ---------------------------------------------------- 
 goto error )

if "%1"=="-p" (
 echo.
 echo ---------------------------------------------------- 
 echo  These projects are available          current: %swproj%  
 echo ---------------------------------------------------- 
 dir /b /l %catools%\project\
 echo ---------------------------------------------------- 
 goto error )

if "%1"=="-?" goto info
if "%1"=="-h" goto info
if "%1"=="/?" goto info
if "%1"=="/h" goto info
if "%1"=="-web" goto webinfo
goto no_info
:webinfo
 explorer https://wwwsw.mp.mchg.siemens.de/
 echo ---------------------------------------------------- 
 echo  Tool DB info is PKI protected, direct linking is 
 echo  not possible - please open the Tools Page - enter
 echo  your PKI information - proceed to Tool Distribution
 echo  Database !
 echo ---------------------------------------------------- 
 goto error
:info
 echo.
 echo ---------------------------------------------------- 
 echo  PROJect dependend tools usage info             v1.0	
 echo.
 echo   proj            restore last session settings
 echo   proj -? ^| -web  help, also -h /? /h,  -web = html  
 echo   proj -p         list available projects   
 echo   proj -t         list avaialble tools for this prj
 echo   proj -r         restore original path
 echo   proj -u usr     make samba connection as user ^<usr^>   
 echo   proj prj        use tools for prj e.g. x65, x75,...  
 echo   proj prj -u usr use tools for proj ^<prj^> as user ^<usr^> 
 echo.
 echo  wwwsw.mp.mchg.siemens.de -^> tools -^> tool database
 echo ---------------------------------------------------- 
 goto error
:no_info

if "%1"=="-r" (
 echo.
 echo ---------------------------------------------------- 
 echo  Restoring old path ^( remove toolpath ^)  
 echo ---------------------------------------------------- 
 echo OLD: path=%path%
 if defined TOOLDB_OLDPATH (
  set PATH=%TOOLDB_OLDPATH%
  echo ---------------------------------------------------
  echo NEW: path=%TOOLDB_OLDPATH%
  set TOOLDB_OLDPATH=
  ) else (
  echo ------- No old path to restore available ! --------
  echo NEW: path=%path%
  )
 echo ---------------------------------------------------- 
 goto error )

REM *************************************************************
REM check if the catools value is a known one (to inform user)
REM this check has only informative character !
REM *************************************************************
echo ------------------------------------------------ 
if "%catools%"=="\\139.23.140.72\catools\win32" ( 
 echo  You are using MCH-Tools from cleopatra
 goto site_identified )

if "%catools%"=="\\149.246.66.75\catools\win32" ( 
 echo  You are using KLF-Tools from klfs017x 
 goto site_identified )

if "%catools%"=="\\149.212.4.109\catools\win32" ( 
 echo  You are using AAL-Tools from aalasuux01 
 goto site_identified )

if "%catools%"=="\\140.231.214.206\catools\win32" ( 
 echo  You are using BEI-Tools from titan 
 goto site_identified )

if "%catools%"=="\\129.103.168.79\catools\win32" ( 
 echo  You are using SAL-Tools from salj079xn 
 goto site_identified )

if "%catools%"=="\\129.73.112.34\catools\win32" ( 
 echo  You are using SAN-Tools from sdcasun1 
 goto site_identified )

if "%catools%"=="\\141.73.109.200\catools\win32" ( 
 echo  You are using ULM-Tools from lhotse 
 goto site_identified )

if "%catools%"=="\\132.186.198.7\catools\win32" ( 
 echo  You are using SISL-Tools from delss09 
 goto site_identified )

echo  You are using Tools from %catools% !
:site_identified


REM *************************************************************
REM Check if Samba connection works
REM *************************************************************

if exist %catools%\siemens (
 echo ------------------------------------------------ 
 goto ok )
echo ------------------------------------------------ 

if "%1"=="-u" (
 echo Checking Samba Connection for %2%...
 net use /USER:%2 %catools%
 if not exist %catools%\siemens (
  echo ------------------------------------------------ 
  echo ERROR: Samba connection FAILED !
  echo ------------------------------------------------ 
  goto error )
 %catools%\siemens\toolsdb\latest\script\proj %3 %4 %5 %6 %7
 goto error 
 )

if "%2"=="-u" (
 echo Checking Samba Connection for %3%...
 net use /USER:%3 %catools%
 if not exist %catools%\siemens (
  echo ------------------------------------------------ 
  echo ERROR: Samba connection FAILED !
  echo ------------------------------------------------ 
  goto error )
 %catools%\siemens\toolsdb\latest\script\proj %4 %5 %6 %7
 goto error 
 )

echo Checking Samba Connection...
net use %catools%
if not exist %catools%\siemens (
 echo ------------------------------------------------ 
 echo ERROR: Samba connection FAILED !
 echo ------------------------------------------------ 
 goto error )
echo ------------------------------------------------ 
:ok

REM *************************************************************
REM Get last project from (1) cmdline (2) registry (3) environment
REM *************************************************************

if not defined TOOLDB_OLDPATH set TOOLDB_OLDPATH=%PATH%
set PATH=%TOOLDB_OLDPATH%

if not "%1"=="" (
 if exist %catools%\project\%1 (
  set swproj=%1
  echo  Using Project : %1 ^(Commandline^)
  echo  Tools in Path %%catools%%\project\%1
  set PATH=%catools%\project\%1;%TOOLDB_OLDPATH%
  %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v Lastuse /t REG_SZ /d "%DATE% %TIME%" /f > NUL
  %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v Lastchange /t REG_SZ /d "%DATE% %TIME%" /f > NUL
  %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v swproj /t REG_SZ /d "%1" /f > NUL
 ) else (
  echo  No Tool Directory available for %1 ^!
 )
) else (
 if not DEFINED temp set temp=c:\ 
 %catools%\siemens\toolsdb\latest\bin\proj_reg add    HKEY_CURRENT_USER\Software\Siemens\ToolDB /v Lastuse /t REG_SZ /d "%DATE% %TIME%" /f > NUL
 %catools%\siemens\toolsdb\latest\bin\proj_reg export HKEY_CURRENT_USER\Software\Siemens\ToolDB %Temp%.\ToolDB.tmp > NUL
 FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\ToolDB.tmp ^| FIND "swproj"') DO (
  if exist %catools%\project\%%~B (
   set swproj=%%~B
   set DummyTempReplacement=9
   echo  Using last Project : %%~B ^(Registry^) 
   echo  Tools in Path %%catools%%\project\%%~B
   set PATH=%catools%\project\%%~B;%TOOLDB_OLDPATH%
   ) 
  )
 if EXIST %Temp%.\ToolDB.tmp del /f /q %Temp%.\ToolDB.tmp 
 if not defined DummyTempReplacement (
  if defined swproj (
   if exist %catools%\project\%swproj% (
    echo  Using last Project : %swproj% ^(Environment^) 
    echo  Tools in Path %%catools%%\project\%swproj%
    set PATH=%catools%\project\%swproj%;%TOOLDB_OLDPATH%
    %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v Lastuse /t REG_SZ /d "%DATE% %TIME%" /f > NUL
    %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v Lastchange /t REG_SZ /d "%DATE% %TIME%" /f > NUL
    %catools%\siemens\toolsdb\latest\bin\proj_reg add HKEY_CURRENT_USER\Software\Siemens\ToolDB /v swproj /t REG_SZ /d "%swproj%" /f > NUL
   ) else (
    echo  No Project found as Param, Registry ^& Env !
    echo  No valid Toolpath available !
   )
  ) else (
    echo  No Project found as Param, Registry ^& Env !
    echo  No valid Toolpath available !
  )
 )  
 set DummyTempReplacement=
)
echo ------------------------------------------------ 

REM *************************************************************
REM 
REM *************************************************************

:error
REM echo ------------------------------------------------ 

echo.

REM *************************************************************
REM end of file proj.bat
REM *************************************************************

