/******************************************************************/
/*                 Started from OS2SERV.EXE                       */
/******************************************************************/
/* Run SERVICE.EXE or FSERVICE.EXE based on information in the    */
/* IBMDAPLN.CTL file in the \OS2\INSTALL directory.               */
/*                                                                */
/* Control file will have one record with the following:          */
/*                                                                */
/* x:\csf x:\fixpak log debug pstat svdrives nnnnnnnn.exe parms   */
/*                                                                */
/* x:\csf=CSF files on the CD                                     */
/* x:\fixpak=fixpak source to apply on the CD                     */
/* log=path to log file                                           */
/* debug=1 if active, 0 if not                                    */
/* svdrives=number of drives being serviced                       */
/* nnnnnnnn=SERVICE or FSERVICE                                   */
/* parms are /R: and /S: for FSERVICE.EXE                         */
/*                                                                */
/* Delete the control file and exit immediately. This             */
/* tells OS2SERV.EXE to finish up.                                */
/*                                                                */
/* v1.33 - implement use of csfdriveapply variable                */
/*       - use IBMDAPAT (attrib) to reset/set R/O flag            */
/*       - of log file.                                           */
/*       - use date('S') for Y2K 4 digit year                     */
/* v2.01 - remove pstat_cmd as parm, always use ibmdapps          */
/* v2.04 - remove date from log entries, add []. Add timestamp to */
/*       - passed parameters, 1=on.                               */
/* v2.05 - Change name of env var from csfdebugtime to            */
/*       - csftimestamp.                                          */
/******************************************************************/
'@Echo off'
Parse Source . . me .                                        /* Who we are */

/***********************************/
/* Register all REXXUTIL functions */
/***********************************/
rc=RxFuncAdd('SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs')
Call SysLoadFuncs

/*******************/
/* Find boot drive */
/*******************/
env='OS2ENVIRONMENT'                                   /* Environment name */
boot=Filespec('DRIVE',Value('COMSPEC',,env))            /* Find Boot drive */
tempfile=SysTempFileName(boot||'\OS2\INSTALL\OS2SERV.???')    /* Work file */

/***************************************************/
/* This is the control file created by OS2SERV.EXE */
/* before it called us. Make sure it's there. Exit */
/* if it's not.                                    */
/***************************************************/
control=boot||'\OS2\INSTALL\IBMDAPLN.CTL'
If stream(control,'C','Query Exists')='' then
  Do
    log=boot||'\OS2\INSTALL\SERVICE.LOG'        /* Normal service log file */
    Call Logger control' not found.'
    Signal Done
  End
Call stream control,'C','Open Read'
Parse value linein(control) with,
    csfpath,                                           /* Path to CSF code */
   fullpath,                                     /* Path to root of fixpak */
        log,                                   /* Fully qualified log file */
  timestamp,                                    /* Timestamp CB, 1=checked */
      debug,                                           /* Debug mode, 1=on */
   svdrives,                                   /* Single drive letter or . */
service_cmd,                                /* service.exe or fservice.exe */
      parms                              /* other parms v1.33, v2.01, 2.04 */

Call stream control,'C','Close'
service=service_cmd='SERVICE.EXE'
If debug then
  Call Logger 'CONTROL DATA: 'csfpath fullpath log debug,
    svdrives service_cmd parms                   /* Remove pstat_cmd v2.01 */

/********************************/
/* Change to the CSF directory. */
/********************************/
Call Directory(csfpath)

/****************************************************/
/* Set the environment vars for SERVICE or FSERVICE */
/* and then start. If servicing a single drive,     */
/* pass that drive to speed up service apply.       */
/****************************************************/
If timestamp=1 then             /* Want service/fservice to timestamp log? */
  x=value('CSFTIMESTAMP','1','OS2ENVIRONMENT')
svdrives=translate(svdrives,' ','#')                              /* v1.33 */
If words(svdrives)=1 Then                 /* v1.33 Servicing single drive? */
  x=value('CSFDRIVEAPPLY',svdrives,'OS2ENVIRONMENT')/* v1.33 Yes, set variable */
If service Then                                    /* Will run SERVICE.EXE */
  Do
    x=Value('CSFCDROMDIR',fullpath,'OS2ENVIRONMENT')
    x=Value('CSFUTILPATH',csfpath,'OS2ENVIRONMENT')
    If debug then
      Call Logset
    Call Logger 'START 'service_cmd
    'START 'service_cmd
  End
Else                                              /* Will run FSERVICE.EXE */
  Do
    x=Value('REMOTE_INSTALL_STATE','0','OS2ENVIRONMENT')
    If debug then
      Call Logset
    Call Logger 'START /F /FS /C 'service_cmd parms
    'START /F /FS /C 'service_cmd parms
  End

/************************************************/
/* Now monitor the program using PSTAT. When it */
/* ends we continue processing. Check every 10  */
/* seconds till the started program goes away.  */
/************************************************/
Do forever
  If stream(tempfile,'C','Query exist')<>'' then
    x=SysFileDelete(tempfile)
  Call SysSleep(10)
  'IBMDAPPS /C > 'tempfile        /* Use IBMDAPPS, pstat_cmd removed v2.01 */
  found=0
  Call Stream tempfile,'C','Open Read'
  Do while lines(tempfile)
    data=linein(tempfile)
    If pos(service_cmd,data)>0 then
      Do
        found=1
        Leave
      End
  End
  Call Stream tempfile,'C','Close'
  If \found Then                                             /* Not there? */
    Leave                                                 /* Yes, all done */
End
If stream(tempfile,'C','Query exist')<>'' then
  x=SysFileDelete(tempfile)

/************************************************************/
/* Clear all possible environment setrting we may have done */
/************************************************************/
x=value('CSFTIMESTAMP','','OS2ENVIRONMENT')                       /* v2.04 */
x=value('CSFDRIVEAPPLY','','OS2ENVIRONMENT')                      /* v1.33 */
x=Value('CSFCDROMDIR','','OS2ENVIRONMENT')
x=Value('CSFUTILPATH','','OS2ENVIRONMENT')
x=Value('REMOTE_INSTALL_STATE','','OS2ENVIRONMENT')

x=SysFileDelete(me)                                      /* Delete ourself */
x=SysFileDelete(control)                        /* Delete the control file */

/*******************************/
/* Only exit from this program */
/*******************************/
Done:
'EXIT'                                           /* Close the OS/2 session */
Exit

/****************************/
/* Log all environment vars */
/****************************/
Logset:
'@SET >' tempfile
Call stream tempfile,'C','Open Read'
Do while lines(tempfile)
  Call Logger linein(tempfile)
End
Call stream tempfile,'C','Close'
x=SysFileDelete(tempfile)
Return

/*********************************************************/
/* Write passed data to log file, preceded by date/time. */
/* v2.04 remove date from log entries, add [].           */
/*********************************************************/
Logger:
Parse arg logdata
l='['||time()||']'
'@IBMDAPAT -R' log
Call Stream log,'C','Open Write'
Call Lineout log,l
Call Stream log,'C','Close'
'@IBMDAPAT +R' log
Return
