/* AddProg.CMD */

/*
** Creates a program object on the desktop
** for a user selectable program and icon
**
*/

/* First, load the VREXX functions */
'@echo off'
call RxFuncAdd 'VInit', 'VREXX', 'VINIT'
initcode = VInit()
if initcode = 'ERROR' then signal CLEANUP
signal on failure name CLEAN_F
signal on halt name CLEAN_H
signal on syntax name CLEAN_S


/* Next, load in the utility REXX functions */
call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
call SysLoadFuncs


/* Show a title screen and get ok or cancel */
msg.0 = 4
msg.1 = '    Program Object Creator'
msg.2 = ''
msg.3 = '         by Mark Ziller'
msg.4 = ''
call VDialogPos 50, 50
rb = VMsgBox('Program Object Creator', msg, 3)
if rb = 'CANCEL' then signal CLEANUP


/* Get the program title from the user */
prompt.0 = 1
prompt.1 = 'Enter the program title:'
prompt.vstring = ''
button = VInputBox('Title', prompt, 30, 3)
if button = 'CANCEL' then signal STOPPED
Obj.Title = prompt.vstring
if Obj.Title = '' then Obj.Title = 'Program'


/* get the filename and icon from the user */
button = VFileBox('Select the executable program:', 'c:\*.exe', 'file')
if button = 'CANCEL' then signal STOPPED
Obj.ProgName = file.vstring

button = VFileBox('Select the icon:', 'c:\icons\*.ico', 'file')
if button = 'CANCEL' then signal STOPPED
Obj.IconName = file.vstring

Obj.WorkDir = FILESPEC("drive", Obj.ProgName) || FILESPEC("path", Obj.ProgName)


/* confirm the choices to make sure */
msg.0 = 6
msg.1 = 'Are these choices correct?'
msg.2 = ' '
msg.3 = 'Title         = ' || Obj.Title
msg.4 = 'Program     = ' || Obj.ProgName
msg.5 = 'Working Dir = ' || Obj.WorkDir
msg.6 = 'Icon         = ' || Obj.IconName 
call VDialogPos 50, 50
rb = VMsgBox('Confirm', msg, 6)
if rb = 'YES' then signal DO_IT
else signal STOPPED 


/* User selected cancel -- exit without doing anything */
STOPPED:
msg.0 = 1
msg.1 = '   Program stopped at user request.'
call VMsgBox 'Exiting', msg, 1
signal CLEANUP


/* All data entered correctly -- create program object and icon */
DO_IT:
/* now set object options for working directory, icon, etc */
Obj.Options = 'EXENAME=' || Obj.ProgName || ';'
Obj.Options = Obj.Options || 'STARTUPDIR=' || Obj.WorkDir || ';'
Obj.Options = Obj.Options || 'ICONFILE=' || Obj.IconName || ';'

msg.0 = 1
result = SysCreateObject("WPProgram",Obj.Title,"<WP_DESKTOP>",Obj.Options) then
if result = 0 then do
    msg.0 = 1
    msg.1 = 'Unable to create Program Object'
    call VMsgBox 'Program Object Creator', msg, 1
    end
  


/* Cleanup and exit */
CLEANUP:
   call VExit
exit

CLEAN_S:
   call VExit
   say "Syntax error -- Abnormal Program Termination"
exit

CLEAN_F:
   call VExit
   say "Failure error -- Abnormal Program Termination"
exit

CLEAN_H:
   call VExit
   say "Halt error -- Abnormal Program Termination"
exit


