/* GetExtra.cmd
 * get extra files from user
 * Parameters:
 *       testFile    - file needed
 *       keyword     - application keyword
 *       productName - name of product
 *       description - description of files needed
 *       app - optional name of app that downloads extra file
 *
 * Returns: full path of testFile found or empty string to abort
 *
 * Notes: if testFile is a semicolon delimited list, prompt user for each file
 *        look for testFile appended to current directory
 *        look for testFile in the download directory
 *        if app = 'fileURL' then download from the fileURL specified for keyword
 *        if app is not blank, call the app to download the file
 *        prompt user for the file
 *
 * Copyright (C) 2025 Blonde Guy
 * All Rights Reserved
 */

testFile = arg(1)
keyword = arg(2)
productName = arg(3)
description = arg(4)
downloadApp = arg(5)

if testFile = '' then do
   say GetExtra('config.sys', 'configsys', 'testing', 'Enter some data')
   return
end

originalDir = directory()

/* if testfile is a single file, not a semicolon delimited list */
if pos(';', testFile) = 0 then do
   /* if testFile exists in current directory, return */
   fullTestFile = stream(testFile, 'c', 'query exists')
   if fullTestFile <> '' then do
      return fullTestFile
   end
   
   /* if testFile exists in download directory, return */
   downloadDir = cdglobal('downloadDir')
   fullTestFile = stream(downloadDir || '\' || testFile, 'c', 'query exists')
   if fullTestFile <> '' then do
      return fullTestFile
   end

   /* try to download fileURL */
   if downloadApp = 'fileURL' then do
      cf = originalDir || '\cdi.cmd'
      fileURL = GetFileURL(cf)      
      if getRemoteFile(fileURL,,,downloadDir || '\' testFile) = 0 then do
         fullTestFile = stream(downloadDir || '\' || testFile, 'c', 'query exists')
         return fullTestFile
      end
   end

   /* try to download testFile */
   else if downloadApp <> '' then do
      if installApp(downloadApp, testFile) = 0 then do
         fullTestFile = stream(downloadDir || '\' || testFile, 'c', 'query exists')
         if fullTestFile <> '' then do
            return fullTestFile
         end
      end
   end
end

/* prompt user for testFile */
cr = d2c(13)
message = "Extra files are needed to install" productName || ".",
          "Enter the location of the needed files, or clear the",
          "entry field to skip the installation." || cr || cr ||,
          "File(s):" testFile || cr ||,
          "Description:" description

numTestFiles = 0
do while testFile <> ''
   numTestFiles = numTestFiles + 1
   parse var testFile testFName.numTestFiles ';' testFile
end
testFName.0 = numTestFiles

userFile = query(productName, testFName.1, message, 'File')

if userFile = '' then return userFile

if stream(userFile, 'c', 'query exists') <> '' then do
   if cdglobal('saveWebApps') = 1 then do
      setupDir = cdglobal('setupDir') || '\' || keyword
      address cmd '@copy' userFile setupDir '> nul'
      if rc <> 0 then do
         call LogCdi "Error" rc "copying" userFile "to" setupDir
      end
      else do
         call LogCdi userFile "saved in" setupDir
      end
   end
   return stream(userFile, 'c', 'query exists')
end

call LogCdi "Error," userFile "not found"
return ''



/* GetRemoteFile(url, userid, passsword, localFile)
   returns 0 for success
           1 for failure
*/
GetRemoteFile:

url = arg(1)
userid = arg(2) /* only for ftp://ftp.blondeguy.com/SuntanV6 */
password = arg(3)
localFile = arg(4) /* full path to local file */

localDir = strip(filespec('Drive', localFile) || filespec('Path', localFile), 'Trailing', '\')
call SysMkDir localDir

/* After cruzio moved the server, I changed URL. old apps still have old url */
if pos('ftp://ftp.blondeguy.com/hidden/SuntanV6', url) = 1 then do
   call LogCdi "url shortened"
   call LogCdi "before:" url
   url = 'ftp://ftp.blondeguy.com/SuntanV6/' || substr(url, 40)
   call LogCdi "after:" url
end

pwstring = ''
if pos('ftp://ftp.blondeguy.com/SuntanV6', url) then do
   pwstring = '--user=' || userid '--password=' || password
end

call Directory localDir
call SysFileDelete localFile
tempDir = value('temp',,'os2environment')
errFile = tempDir || '\cd-error.txt'
zFile = substr(url, lastpos('/', url) + 1)
zTest = pos('F=', zFile)
if zTest > 0 then do
   zFile = substr(zFile, lastpos('F=', zFile) + 2)
end
zTest = pos('&', zFile)
if zTest > 0 then do
   zFile = left(zFile, zTest - 1)
end
wgetua = '--user-agent="Mozilla/5.0 (OS/2; Warp 4.5; rv:10.0.5) Gecko/20100101 Firefox/10.0.5"'
call LogCdi "Trying" SysSearchPath('Path', 'wget.exe') "to get" url
call LogCdi "Downloading to" localDir", please wait..."
address cmd '@wget --tries=1 --no-check-certificate --output-document=' || zFile pwstring wgetua '--output-file=' || errFile '"' || url || '"'
wrc = rc
if wrc = 0 then call LogCdi "retrieved" zFile
else do
   do while lines(errFile)
      lline = linein(errFile)
      if pos('..........', lline) = 0 then do
         call LogCdi 'log>>' lline
      end
   end
   call stream errFile, 'c', 'close'
   call LogCdi "Error" wrc "retrieving" zFile
   call Directory originalDir
   return 1
end
call SysFileDelete errFile
if stream(zFile, 'c', 'query exists') = '' then do
   call LogCdi "file not found:" zFile
   call LogCdi "wget failed to retrieve" url
   call Directory originalDir
   return 1
end
outFile = filespec('Name', localFile)
if zFile <> outFile then do
   address cmd '@copy' zFile outFile '> nul'
   if rc <> 0 then do
      call LogCdi "Error" rc "copying" zFile "to" outFile
   end
   call SysFileDelete zFile
end
call Directory originalDir
return 0


GetFileURL:
/*
 * argument: file to scan for fileURL
 */
parse arg cf
fileURL = getCdiVar('fileURL' cf)
if fileURL = '' then return ''
if right(fileURL,1 ) <> ',' then return fileURL

/* fileURL is multi-line */
fileURL = ''
ffileURL = 0

rc = stream(cf, 'c', 'open read')
if rc <> 'UNKNOWN' & rc <> 'READY:' then do
   call LogCdi "GetFileURL(" || cf || ") error" rc
   return ''
end

do while lines(cf)
   sLine = linein(cf)
   if pos('fileURL =', sLine) > 0 & fileURL = '' then do
      urlLine = ''
      ffileURL = 1
   end
   if ffileURL = 1 then do
      sline = translate(sline,"  ","'"||'"') /* remove quotes */
      urlLine = urlLine || strip(strip(sLine), 'Trailing', ',')
      if right(strip(sLine), 1) <> ',' then do
         parse var urlLine fv '=' fileURL
         ffileURL = 0
      end
   end
end

call stream cf, 'c', 'close'

return fileURL


/* get Cdi Variable */
getCdiVar:

parse arg myVar myCdiFile

if myVar = '' then do
   call LogCdi "getCdiVar called with no myVar argument"
   return ''
end

if myCdiFile = '' then do
   call LogCdi "getCdiVar called with no myCdiFile argument"
   return ''
end

interpret myVar "=''"

call SysFileSearch myVar, myCdiFile, 'file.'
do i=1 to file.0
   pp = pos(myVar, file.i)
   ep = pos('=', file.i)
   if pp > 0 & pp < ep then do
      if right(file.i, 1) = ',' then do
         return ','
      end
      else do
         interpret file.i
         return value(myVar)
      end
   end
end

return value(myVar)

