/* REXX */
/*
  tdsinst.cmd
  
  Copyright (C) 1998--2001 Walter Schmidt, Erlangen 
  
  emtex/TDS installation program
  
  to be run
  from a local directory, which must contain all required files
  
*/



/*
  Initialization:
*/  
INTRO = "Installation script for emTeX/TDS v0.56 as of 2001-09-30"
VERSION = 0.56
REQEMXREV = 60
REQEMXVER = "0.9d"

bootdrv = querybootdrv()
wrkdir = strip(directory(), "T", "\")

ret = RxFuncAdd("SysLoadFuncs", "REXXUTIL", "SysLoadFuncs")
call SysLoadFuncs

SIGNAL on error

/* Load text strings for SysFileDel and SysRmDir return codes.         */
CALL LoadDELRCText   /* provides text strings for SysFileDel return codes */
CALL LoadRDRCText    /* provides text strings for SysRmDir return codes   */

say 
say INTRO
say
say "On which drive do you want emTeX/TDS to be installed or updated?"
say "(hit <return> only to select the boot drive)"
answer = linein()
texdrive = translate(substr(answer,1,1)) || ":"
if texdrive = ":" then texdrive = bootdrv

ver = SysIni(texdrive"\texmf\emtex\data\EMTEXTDS.INI", "EMTEXTDS", "VERSION")
if ver = "ERROR:" then
   ver = 0
else do
   say
   say "emTeX/TDS version " || ver || " detected."
   say
end

/* full install or update only? 

   ver =  0    :  first install, no cleanup required
   ver =  0.1  :  force full install, despite higher version #
   ver <  0.53 :  full install
   ver >= 0.53 :  update only
*/

/* skip this step if full install is required, anyway */
if ver < 0.53 then signal checkfiles

say "By default, your emTeX/TDS system will be updated only."
say "However, you may also force a full reinstall."
say
say "Do you request a full reinstall? (y/n)"
answer = translate(substr(linein(),1,1))
if answer = "Y" then ver = 0.1


/* Determine the required files */
checkfiles:

if ver >= 0.55 then
  reqfile.0 = 1
else
if ver >= 0.53 then 
  reqfile.0 = 2
else
  reqfile.0 = 11

/* search for the files in the current directory  */
reqfile.1 =  "texmf.zip"
reqfile.2 =  "config.zip"
reqfile.3 =  "bibtex4b.zip"
reqfile.4 =  "dvid16h1.zip"
reqfile.5 =  "mf4b.zip"
reqfile.6 =  "mfjob12d.zip"
reqfile.7 =  "mfware.zip"
reqfile.8 =  "tex4b.zip"
reqfile.9 =  "texware.zip"
reqfile.10=  "makeindx.zip"
reqfile.11 =  "dvid16h2.zip"
if CheckRequiredFiles("V") then do
   do i = 1 to reqfile.0
      reqfile.i = wrkdir|| "\" || reqfile.i
   end
end
else signal fileerr

/* Last exit */
say
say "emTeX/TDS on drive " || texdrive || " will be installed/updated."
say "Continue? (y/n)"
answer = translate(substr(linein(),1,1))
if answer <> "Y" then signal cancel



/* 
   Step 0
   ------
   go to the target drive and create the texmf root directory
*/

call setlocal
call directory(texdrive||"\")
call sysmkdir("texmf")
call directory("\texmf")


/* 
   Step 1
   ------
   Check for unzip.exe
*/
if SysSearchPath("PATH", "unzip.exe") = "" then signal unziperr


/*
  Step 2
  ------
  Removing obsolete files created by previous versions
*/

/* skip this step, if this is the first install */
if ver = 0  then signal euro

say "Removing possibly obsolete files ..."

call SysFileDelete("emtex\bmfbases\makemf.cmd")
call SysFileDelete("doc\generic\german\gerdoc98.dvi")
call SysFileDelete("emtex\doc\bibtex.doc")
call SysFileDelete("emtex\doc\makeindx.doc")
call SysFileDelete("emtex\doc\makeindx.cpy")
call SysFileDelete("doc\emtexTDS\install.ger")
call SysFileDelete("doc\emtexTDS\install.eng")
call SysFileDelete("doc\latex\eurosans\readme")
call SysFileDelete("doc\latex\graphics\grfguide.ps")

call SysFileDelete("tex\latex\local\local.ins")

call SysFileDelete("tex\latex\amsmath\amstext.sty.org")

call SysFileDelete("doc\generic\german\gerdoc.tex")
call SysFileDelete("fonts\source\public\logo\manfnt.mf")
call SysFileDelete("fonts\tfm\public\logo\manfnt.tfm")
call SysFileDelete("source\generic\babel\lcydefs.tex")
call SysFileDelete("dvips\micropress\cmfonts.map")
/* files have been moved to other directory */

call SysFileDelete("emtex\bin\bibtex.exe")
call SysFileDelete("emtex\bin\makeindex.exe")
call SysFileDelete("emtex\bin\dvihplj.exe")
/* will be replaced later */


call ddt("tex\latex\base")

/*
call ddt("tex\latex\tools")
call ddt("tex\latex\graphics")
call ddt("tex\latex\cyrillic")
*/

call ddt("source\latex\babel")
call ddt("tex\latex\babel")
call ddt("doc\latex\babel")

call ddt("dvips\base")

call ddt("fonts\type1\ams")
call ddt("fonts\type1\public\cm")
call ddt("fonts\type1\public\logo")
call ddt("fonts\type1\public\misc")
call ddt("fonts\type1\public\rsfs")
call ddt("fonts\type1\public\wasy")

call ddt("fonts\source\public\logo")

call ddt("tex\latex\psnfss\pag")
call ddt("tex\latex\psnfss\pbk")
call ddt("tex\latex\psnfss\pcr")
call ddt("tex\latex\psnfss\phv")
call ddt("tex\latex\psnfss\pnc")
call ddt("tex\latex\psnfss\ppl")
call ddt("tex\latex\psnfss\psy")
call ddt("tex\latex\psnfss\ptm")
call ddt("tex\latex\psnfss\pzc")
call ddt("tex\latex\psnfss\pzd")

call ddt("source\latex\psnfss")
call ddt("doc\latex\psnfss")
call ddt("source\latex\mathpple")
call ddt("tex\latex\mathpple")
call ddt("doc\latex\mathpple")
call ddt("fonts\tfm\public\logo")
call ddt("dvips\bluesky")
call ddt("tex\latex\amslatex")
call ddt("doc\latex\amslatex")

call SysFileDelete("doc\emtextds\gsinstal.eng")
call SysFileDelete("doc\emtextds\gsinstal.ger")
call SysFileDelete("metafont\base\plain.mf.sav")

/*
call ddt("tex\latex\koma.script")
call ddt("doc\latex\koma-script")
call ddt("source\latex\koma-script")
*/

call SysFileDelete("tex\latex\amsmath\amsdtx.cls")

/* obsolete files, which are possibly not overwritten later */

say " done."


/* Step 2a
   -------
   Rename Adobe Euro Fonts, if present
*/
euro:

/* skip this step, if v=0 or v>=0.53 found */

if (ver  = 0) | (ver >= 0.53) then signal emtex

old.1 =  "\texmf\fonts\type1\adobe\eurofont\_1______.pfb"
old.2 =  "\texmf\fonts\type1\adobe\eurofont\_1B_____.pfb"  
old.3 =  "\texmf\fonts\type1\adobe\eurofont\_1I_____.pfb"  
old.4 =  "\texmf\fonts\type1\adobe\eurofont\_1BI____.pfb"  
old.5 =  "\texmf\fonts\type1\adobe\eurofont\_2______.pfb"  
old.6 =  "\texmf\fonts\type1\adobe\eurofont\_2B_____.pfb"  
old.7 =  "\texmf\fonts\type1\adobe\eurofont\_2I_____.pfb"  
old.8 =  "\texmf\fonts\type1\adobe\eurofont\_2BI____.pfb"  
old.9 =  "\texmf\fonts\type1\adobe\eurofont\_3______.pfb"  
old.10 = "\texmf\fonts\type1\adobe\eurofont\_3B_____.pfb"  
old.11 = "\texmf\fonts\type1\adobe\eurofont\_3I_____.pfb"  
old.12 = "\texmf\fonts\type1\adobe\eurofont\_3BI____.pfb"  

new.1 = "zpeurs.pfb" 
new.2 = "zpeubs.pfb"    
new.3 = "zpeuris.pfb"   
new.4 = "zpeubis.pfb"   
new.5 = "zpeurt.pfb"    
new.6 = "zpeubt.pfb"    
new.7 = "zpeurit.pfb"   
new.8 = "zpeubit.pfb"   
new.9 = "zpeur.pfb"     
new.10 = "zpeub.pfb"     
new.11 = "zpeuri.pfb"    
new.12 = "zpeubi.pfb"    

do i = 1 to 12
   if stream(old.i, "C", "QUERY EXISTS") <> "" & ,
      stream("\texmf\fonts\type1\adobe\eurofont\"||new.i, "C", "QUERY EXISTS") = "" then do
        "ren " old.i new.i
   end
end


/* 
  Step 3
  ------
  Install the required parts of the original emTeX distribution.
*/
emtex:

/* skip this step, if ver>=0.53 found */
if ver >= 0.53 then signal tds

/* Overwrite existing files */
ret=value("UNZIP", "-qqo", "OS2ENVIRONMENT")

say "Installing emTeX ..."

call SysMkDir("\texmf\doc")
call SysMkDir("\texmf\doc\bibtex")

/* reqfile.3 => bibtex4b. */
"unzip -j "||reqfile.3 || " emtex/doc/bibtex.doc -d \texmf\doc\bibtex"
"unzip -j "||reqfile.3 || " emtex/texinput/bibtex/* -d \texmf\doc\bibtex" 
"unzip "||reqfile.3 || " emtex/bin/bibtex32.exe"
"ren emtex\bin\bibtex32.exe bibtex.exe"

/* reqfile.4 => dvid16h1 */
"unzip "||reqfile.4|| " emtex/doc/adjust.dvi"
"unzip "||reqfile.4|| " emtex/book/german/dvidrv.inf"
"unzip "||reqfile.4|| " emtex/book/english/dvidrv.inf"
"unzip "||reqfile.4|| " emtex/data/dvidrv.err"
/* "unzip "||reqfile.4|| " emtex/bin/dvihplj.exe" */
"unzip "||reqfile.4|| " emtex/bin/dvipm.exe"
"unzip "||reqfile.4|| " emtex/bin/dvipm.ico"
"unzip "||reqfile.4|| " emtex/dll/dvipmres.dll"
"unzip "||reqfile.4|| " emtex/dll/emdll1x.dll"
"unzip "||reqfile.4|| " emtex/dll/emdll1x2.dll"
"unzip "||reqfile.4|| " emtex/dll/emdll2x.dll"
"unzip "||reqfile.4|| " emtex/help/dvipmeng.hlp"
"unzip "||reqfile.4|| " emtex/help/dvipmger.hlp"
"unzip "||reqfile.4|| " emtex/data/2a5_a4.cnf"

/* reqfile.5 => mf4b */
"unzip "||reqfile.5||" emtex/book/german/metafont.inf"
"unzip "||reqfile.5||" emtex/book/english/metafont.inf"
"unzip "||reqfile.5||" emtex/bin/mf386.exe"
"unzip "||reqfile.5||" emtex/bin/mfpm386.ovl"
"unzip "||reqfile.5||" emtex/bin/gftopk.exe"
"unzip "||reqfile.5||" emtex/bmfbases/mf.poo"
"unzip "||reqfile.5||" emtex/help/mfpmeng.hlp"
"unzip "||reqfile.5||" emtex/help/mfpmger.hlp"

/* reqfile.6 => mfjob12d */
"unzip "||reqfile.6||" emtex/book/german/mfjob.inf"
"unzip "||reqfile.6||" emtex/book/english/mfjob.inf"
"unzip "||reqfile.6||" emtex/bin/mfjob.exe"
"unzip "||reqfile.6||" emtex/bin/mfjob.ovl"

/* reqfile.7 => mfware */
"unzip "||reqfile.7||" emtex/bin/gftodvi.exe"
"unzip "||reqfile.7||" emtex/bin/mft.exe"
"unzip "||reqfile.7||" emtex/doc/german/mfware.doc"
"unzip "||reqfile.7||" emtex/doc/english/mfware.doc"

/* reqfile.8 => tex4b */
"unzip "||reqfile.8||" emtex/bin/htex386.exe"
"unzip "||reqfile.8||" emtex/htexfmts/tex.poo"
"unzip "||reqfile.8||" emtex/book/german/tex.inf"
"unzip "||reqfile.8||" emtex/book/english/tex.inf"

/* reqfile.9 => texware */
"unzip "||reqfile.9||" emtex/doc/german/texware.doc"
"unzip "||reqfile.9||" emtex/doc/english/texware.doc"
"unzip "||reqfile.9||" emtex/bin/pltotf.exe"
"unzip "||reqfile.9||" emtex/bin/tftopl.exe"
"unzip "||reqfile.9||" emtex/bin/vftovp.exe"
/* "unzip "||reqfile.9||" emtex/bin/vptovf.exe" */

/* reqfile.10 => makeindx */
"unzip "||reqfile.10||" emtex/bin/mkidx32.exe"
"ren emtex\bin\mkidx32.exe makeindex.exe"

/* reqfile.11 => dvid16h2 */
"unzip "||reqfile.11||" emtex/bin/dvihplj3.exe"
"ren emtex\bin\dvihplj3.exe dvihplj.exe"

call SysMkDir("\texmf\bibtex")
call SysMkDir("\texmf\bibtex\bib")
call SysMkDir("\texmf\bibtex\bst")

"unzip -j "||reqfile.3||" emtex\bibtex\bib\* -d \texmf\bibtex\bib"
"unzip -j "||reqfile.3||" emtex\bibtex\bst\* -d \texmf\bibtex\bst"

say " done."


/*
  Step 4
  ------
  Installing 
  - the non-emTeX parts of the TDS tree and
    customized cmd scripts for emtex,
  - the configuration files,
*/
tds:

say "Installing the texmf tree ..."
/* Overwrite existing files */
ret=value("UNZIP", "-qqo", "OS2ENVIRONMENT")
/* reqfile.1 => texmf */
"unzip "||reqfile.1

if reqfile.0 >= 2 then do
   /* here we have to ask before owerwriting configuration files: */
   ret=value("UNZIP", "-qq", "OS2ENVIRONMENT")
   /* reqfile.2 => config */
   "unzip "||reqfile.2
end

say " done."

/* The configuration file docstrip.cfg is special,    */
/* because its contents must reflect the drive letter */
/* where TeX is installed.                            */
/* This file is generated unless it's already present.*/
filename = texdrive || "\texmf\tex\latex\local\docstrip.cfg"
if stream(filename, "C", "QUERY EXISTS") = "" then do
   say "Creating the file docstrip.cfg ..."
   call lineout filename, "%% file docstrip.cfg, generated by tdsinst.cmd"
   call lineout filename, "%% do not edit this file!"
   call lineout filename, "\BaseDirectory{"||texdrive||"/texmf}"
   call lineout filename, "\UseTDS"
   call stream filename, "C", "CLOSE"
   say " done."
end


/*
  Step 5
  ------
  Creating a program object for dvipm
*/
/* skip this step, unless full install is required */
if ver > 0.1 then signal writever

say
say "Do you want an object for dvipm to be created on the desktop? (y/n)"
answer = translate(substr(linein(),1,1))
if  answer = "Y" then do
  say "Creating an object for the dvi viewer ..."
  res = make_po('dvipm', 'PM', texdrive||'\texmf\emtex\bin\dvipm.exe', '@vp.cnf %*', '')
  if res = 1  then say " done."
  else say " failed."
end


/*
  Step 5a
  --------
  Writing the version number
*/
writever:
call SysIni "\texmf\emtex\data\EMTEXTDS.INI", "EMTEXTDS", "VERSION", VERSION
say
say "Version number" VERSION "written."
say
say "All files of the emTeX/TDS system have been successfully installed."
"pause"


/*
  Step 6
  ------
  Checking emx
*/
say
say "I am going to check the EMX library now."
say
emxpaths = 0
currentrev = emxrevision()
say "emx rev. =" currentrev
if (currentrev = 0) then do
   call beep 440,250
   say
   say "I cannot find the emx runtime environment."
   say "Install the latest version of emx before using TeX!"
   "pause"
end
if (currentrev > 0) & (currentrev < REQEMXREV)then do
   call beep 440,250
   say
   say "The emx runtime environment found is not up to date."
   say "Install the latest version of emx before using TeX!"
   "pause"
end
else say "Ok."


/*
  Step 7
  ------
  Updating config.sys
*/

/* skip this step if ver > 0.5 found */
if ver > 0.5 then signal done 

configfile = bootdrv||"\config.sys"
if stream(configfile,"C","QUERY EXISTS") = "" then signal nobootdrv

call readconfig configfile

if checkvar("TMP") = "" then do
   tmpname = bootdrv||"\tmp"
   call SysMkDir tmpname
   call addvar "TMP", tmpname
   say
   say "Temporary files will be created in the directory" tmpname "."
   say 
end /* do */

call addvar "EMTEXDIR", texdrive||"\TEXMF\EMTEX"
call addvar "TEXMACROS", texdrive||"\TEXMF\TEX"
call addvar "TEXTFM", texdrive||"\TEXMF\FONTS\TFM!!;."
call addvar "BFONTS", texdrive||"\TEXMF\FONTS\PK"
call addvar "TYPE1FONTS", texdrive||"\TEXMF\FONTS\TYPE1"
call addvar "TEXCONFIG", texdrive||"\TEXMF\DVIPS!!"
call addvar "VFONTS", texdrive||"\TEXMF\FONTS\VF!!"
call addvar "EMTEXSWCHAR", "-"
call addvar "MFOPT", "-mp50000"
call addvar "MFJOBOPT", "-3 -i"
call addvar "MFINPUT", texdrive||"\TEXMF\FONTS\SOURCE!!;"||texdrive"\TEXMF\METAFONT!"
call addvar "INDEXSTYLE", texdrive||"\TEXMF\MAKEINDEX"
call addvar "BSTINPUT", texdrive||"\TEXMF\BIBTEX\BST"
call addvar "BIBINPUT", texdrive||"\TEXMF\BIBTEX\BIB"
if pathedit("PATH", texdrive||"\texmf\emtex\bin;") = 0 then signal updfailed
if pathedit( "HELP", texdrive||"\texmf\emtex\help;") = 0 then signal updfailed
if libpathedit(texdrive||"\texmf\emtex\dll;") = 0 then signal updfailed

say
say "The installation program is going to update the file config.sys now."
say "If you prefer to do this manually, then enter `n' below,"
say "otherwise enter `y' to continue."
say "Continue? (y/n)"
answer = translate(substr(linein(),1,1))
if answer <> "Y" then signal done

tempfile=SysTempFileName(bootdrv||"\config??.sys")
if tempfile="" then signal updfailed

signal on error name updfailed
savedfile = filespec("N", tempfile)
"ren" configfile savedfile
say
say "The file" configfile "has been renamed to" savedfile "."
res = writeconfig(configfile)
if res=0 then do
   call beep 440,250
   say "Writing a new file" configfile "failed".
   say "Rename" savedfile "to config.sys and apply the changes manually."
   end
else  do 
   say
   say "A new file" configfile "has been written."
   say "Shutdown and reboot OS/2 now to make the changes come into effect."
end


/* 
  exiting
*/
done:
say
say "Finis!"
exit


/*
  Error handling:
*/
nobootdrv:
say
say "The installation program could not locate the file config.sys"
say "and was unable to determine whether it needs to be updated."
say "Please, see the installation instructions for the changes required"
say "and how to apply them manually."
exit

updfailed:
say
say "Updating config.sys failed."
say "The file config.sys has *** not *** been changed."
say "Please, read the installation instructions"
say "and apply any changes manually."
exit

error: 
call beep 440,250
say
say 'Error: "'condition('D')'"' 'failed.'
exit

fileerr:
call beep 440,250
say
say "One or more of the files required to"
say "install or update emTeX/TDS were not found."
exit

unziperr:
call beep 440,250
say
say 'The required program "unzip.exe" could not be found.'
exit

cancel:
say "You have terminated the installation program."
exit


/* 
  Procedures used above: 
*/


/*
make_folder: procedure
  location = '<EMTEXTDS_FOLDER>'
  if \SysCreateObject('WPFolder', 'emTeX/TDS', '<WP_DESKTOP>', ,
                      'OBJECTID='||location, 'update') then do
    say 'Cannot create emTeX/TDS folder.'
    exit 1
  end
  return
*/

make_po: procedure
  parse arg title, progtype, exename, parameters, workdir
  
  options = 'PROGTYPE='||progtype||';' ||,
            'EXENAME='||exename||';'
            
  if parameters <> '' then 
    options = options || 'PARAMETERS='||parameters||';'
    
  if workdir <> '' then 
    options = options || 'STARTUPDIR='||workdir||';'
    
  if \SysCreateObject('WPProgram', title, '<WP_DESKTOP>', options, 'Update') then do
    say 'Cannot create program object.'
    return 0
  end
  else
    return 1


/*
   query boot drive 
   usage:   querybootdrv()
   returns: letter of boot drive incl. colon
*/
querybootdrv: PROCEDURE
bootdr = value('COMSPEC', , 'OS2ENVIRONMENT')
bootdr = filespec('D', bootdr)
if bootdr = "" then do
   say "Please, tell me the drive letter of the boot drive:"
   answer = linein()
   bootdr = substr(strip(answer),1,1)||":"
end
return bootdr


/* 
   Do all required files exist?
   usage:   CheckRequiredFiles() or CheckRequiredFiles("V")
   returns: 1 if all files found, 0 otherwise
*/
CheckRequiredFiles: PROCEDURE expose reqfile.
parse arg verbose
/* empty line, to make further messages more visible: */
if verbose = "V" then say 
res = 1
do i = 1 to reqfile.0
   if  stream(reqfile.i, "C", "QUERY EXISTS") = "" then do
     res = 0
     if verbose = "V" then say "missing file:" reqfile.i
   end
end
return res


/*
   read config file
   usage:   call readconfig filename
   returns: (nothing)
*/
readconfig: PROCEDURE expose config. cmax
parse arg infile
cmax=0
do while lines(infile) > 0
   cmax=cmax+1
   config.cmax=linein(infile)
end /* do */
call stream infile, "C", "CLOSE"
return


/*
   write config file
   usage:   call writeconfig filename
   returns: 1 if successful
            0 if writing the file failed
*/
writeconfig: PROCEDURE expose config. cmax
parse arg outfile
call SysFileDelete outfile
do i = 1 TO cmax
   if config.i <> "deleted" then do
      res = lineout(outfile, config.i)
   end
end /* do */
call stream outfile, "C", "CLOSE"
if res <> 0  then return 0
else return 1


/*
   search for a given environment variable
   usage:   checkvar(name)
   returns: setting
            "" if undefined or defined as empty
*/
checkvar: PROCEDURE expose config. cmax
parse upper arg name
i=0
name=strip(name)
trigger=" "||name||"="
do forever
   i=i+1
   if i>cmax then leave
   parse upper value config.i with junk (trigger) setting
   if (setting <> "") & (junk="SET") then leave
end /* do */
if i>cmax then return ""
else return setting


/*
   add environment variable, overwrites current setting
   usage:   call addvar name, value
   returns: (nothing)
*/
addvar: PROCEDURE expose config. cmax
parse arg name, value
name = strip(translate(name))
trigger=" "||name||"="
i=0
do forever
   i=i+1
   if i>cmax then leave
   parse upper value config.i with junk (trigger) setting
   if (setting <> "") & (junk = "SET") then leave
end /* do */
config.i = "SET " || name || "=" || value
if i>cmax then cmax=i
return



/* 
   delete environment variable
   usage:   delvar(name)
   returns: 1 if successful
            0 if variable not found
*/
delvar: PROCEDURE expose config. cmax
parse arg name
name=strip(translate(name))
trigger=" "||name||"="
i=0
do forever
   i=i+1
   if i>cmax then leave
   parse upper value config.i with junk (trigger) setting
   if (setting <> "") & (junk="SET") then leave
end /* do */
if i>cmax then return 0
else do
   config.i = "deleted"
   return 1
end /* do */


/* 
   edit a path
   usage:   pathedit(path, entry, op)
            op = "D" means "delete this entry"
            op = "A" (default) means "add entry"
   returns 1 if successful
           0 if path or entry to be deleted not found
           2 if entry to be added was already present
*/
pathedit: PROCEDURE expose config. cmax
parse upper arg path, directory, op
i=0
path=strip(path)
trigger = " "||path||"="
do forever
   i=i+1
   if i>cmax then leave
   parse upper value config.i with junk (trigger) setting
   if (setting <> "") & (junk="SET") then leave
end /* do */
if i>cmax then return 0
setting=strip(setting,"T")
if substr(directory,length(directory),1) <> ";" then directory = directory || ";"
if substr(setting,length(setting),1) <> ";" then setting = setting || ";"
if op="D" then do
   posi = pos(directory,setting)
   if posi<>0 then config.i= "SET " || path || "=" || delstr(setting, posi, length(directory))
   else return 0
end /* do */
else do
   if pos(directory,setting)<>0 then return 2
   else do
      setting=setting || directory
      config.i = "SET " || path || "=" || setting
   end /* do */
end /* do */
return 1


/* 
   edit LIBPATH
   usage:   libpathedit(entry, op)
            op = "D" means "delete this entry, if existing"
            op = "A" (default) means "add entry"
   returns: 1 if successful
            0 if LIBPATH or entry to be deleted not found
            2 if entry to be added was already present
*/
libpathedit: PROCEDURE expose config. cmax
parse upper arg directory, op
i=0
do forever
   i=i+1
   if i>cmax then leave
   parse upper value config.i with junk "LIBPATH" setting
   if (setting <> "") & (junk = "") then leave
end /* do */
if i>cmax then return 0
setting=strip(setting,"T")
if substr(directory,length(directory),1) <> ";" then directory = directory || ";"
if substr(setting,length(setting),1) <> ";" then setting = setting || ";"
if op="D" then do
   posi = pos(directory,setting)
   if posi<>0 then config.i= "LIBPATH" || delstr(setting, posi, length(directory))
   else return 0
end /* do */
else do
   if pos(directory,setting)<>0 then return 2
   else do
      setting=setting || directory
      config.i = "LIBPATH" || setting
   end /* do */
end /* do */
return 1


emxrevision: procedure
  call RxFuncAdd emx_revision, "emx", emx_revision
  signal on syntax name noemx
  tmp = emx_revision()
  signal off syntax
  call RxFuncDrop emx_revision
  return tmp
noemx: return 0


/*
  Procedures for deleting a directory tree
*/


DDT:
        parse arg user_dir
        /* Check to make sure the directory exists */
        rc=SysFileTree(user_dir,dir_list, 'D')
        IF dir_list.0 = 0  THEN
                DO
                     SAY user_dir 'not found.'
                     RETURN
                END
        
        DROP dir_list.
        
        /* Mark all the read-only files to be non read-only        */
        rc=SysFileTree(user_dir || '\*.*', dir_list, 'FSO', '****','----')
        
        DROP dir_list.
        
        /* Go through the list of files and delete each one        */
        rc=SysFileTree(user_dir || '\*.*', dir_list, 'FSO')
        DO x = 1 TO dir_list.0
        	rc = SysFileDelete(dir_list.x)
        	if rc <> 0 then SAY dir_list.x '........' DELRCText.RC
        END
        
        DROP dir_list.
        
        /* Go through all the subdirectories and remove them.        */
        /* We go backwards through the list in order to delete the   */
        /* lowest level sudirectories first and work our way back up */
        /* the tree.                                                 */
        rc=SysFileTree(user_dir || '\*.*', dir_list, 'DSO')
        DO x = dir_list.0 TO 1 BY -1
        	rc=SysRmDir(dir_list.x)
        	if RC <> 0 then SAY dir_list.x '........' RDRCText.RC
        END
        
        DROP dir_list.
        
        /* Delete the topmost directory */
        rc=SysRmDir(user_dir)
        SAY user_dir '........' RDRCText.RC
        
RETURN

LoadDELRCText:
	/* provides text strings for SysFileDel return codes */
	/* The return codes and strings are in the online Rexx manual */

	DELRCText.0 = 'File deleted successfully. '
	DELRCText.2 = 'Error.  File not found. '
	DELRCText.3 = 'Error.  Path not found. '
	DELRCText.5 = 'Error.  Access denied. '
	DELRCText.26 = 'Error.  Not DOS disk. '
	DELRCText.32 = 'Error.  Sharing violation. '
	DELRCText.36 = 'Error.  Sharing buffer exceeded. '
	DELRCText.87 = 'Error.  Invalid parameter. '
	DELRCText.206 = 'Error.  Filename exceeds range error. '
RETURN

LoadRDRCText:
	/* provides text strings for SysRmDir return codes */
	/* The return codes and strings are in the online Rexx manual */

	RDRCText.0 = 'Directory removal was successful. '
	RDRCText.2 = 'Error.  File not found. '
	RDRCText.3 = 'Error.  Path not found. '
	RDRCText.5 = 'Error.  Access denied. '
	RDRCText.16 = 'Error.  Current Directory. '
	RDRCText.26 = 'Error.  Not DOS disk. '
	RDRCText.87 = 'Error.  Invalid parameter. '
	RDRCText.108 = 'Error.  Drive locked. '
	RDRCText.206 = 'Error.  Filename exceeds range error. '
RETURN

/* finis */