/****************************************************/
/* updcd rexx library - install specific functions  */
/* created: 02.19.2002                              */
/* 03.10.2002: value rootdir should not be migrated */
/****************************************************/

/* check rexx version */
call check_rexx

/* get arguments */
parse arg key

/* check system */
insttype = value('UPDCD_INST', , 'OS2ENVIRONMENT')
cdpath   = value('CDROM_PATH', , 'OS2ENVIRONMENT')

/* are you sure? */
'@cls'
say
if insttype <> 1 & cdpath <> '' then do
	say ' UpdCD is already installed on your system. If you want to'
	say ' reinstall it, open the Config.Sys on disk 1 and change the'
	say ' line SET UPDCD_INST=0 to SET UPDCD_INST=1 and reboot.'
	say
	say ' Otherwise change to the UpdCD directory and run Go4it.Cmd.'
end
else do
	say ' This program will install UpdCD on your system.'
	if key <> 'QUIET' then do
		say ' Press ENTER to continue or CTRL-C to exit.'
		'@pause >nul'
	end
end

/* load rexx functions */
call load_rexxutil

/* get and store updcd version */
version = get_updcd_version()
call set_updcd_version version

/* check for url object creation */
call check_wpurl_class

path = directory()
if cdpath = '' then do
	/* create objects */
	say
	say ' -> Creating UpdCD objects.'

	/* store dir */	
	call SysIni , 'UPDCD', 'PATH', path
	call create_objects

	/* check vrobj.dll */
	call check_vrobj
end

/* create some empty dirs */
'@mkdir fixes >nul 2>>&1'
'@mkdir updates >nul 2>>&1'
'@mkdir fix >nul 2>>&1'
'@mkdir burn >nul 2>>&1'

/* start creating cfg file */
say
say ' -> Generating configuration defaults.'
cfgfile = path'\updcd.cfg'
call generate_cfg_defaults

/* read and backup existing config */
if stream(cfgfile, 'c', 'query exists') <> '' then do
	call Read_Config_File cfgfile
	bakfile = path'\updcd.bak'
	'@copy 'cfgfile' 'bakfile' >nul 2>>&1'
end

/* check to see if user has RSJ installed */
call check_if_rsj_installed

/* Locate mkisofs.exe */
call locate_mkisofs

/* write config */
say ' -> Saving default configuration file.'
'@del 'cfgfile' >nul 2>>&1'
rootdir = path
call Save_Config_File cfgfile

/* check installed software */
if cdpath = '' then call check_tooling
else do
	i=1
	cfgfl = 'maint\config.sys'
	do while lines(cfgfl)
		l.i = linein(cfgfl)
		if translate(l.i) = 'SET UPDCD_INST=1' then l.i = 'SET UPDCD_INST=0' 
		i=i+1
	end
	call lineout cfgfl
	l.0 = i-1
	'@del 'cfgfl
	do i=1 to l.0
		call lineout cfgfl, l.i
	end
	call lineout cfgfl
	do while stream('a:\config.sys', 'c', 'query exists') = ''
		say
		say ' Please insert UpdCD disk 1 into drive A: and press any key.'
		'@pause >nul'
	end
	'@copy 'cfgfl' a:\config.sys'
	say
	say ' Use the command "tedit readme.eng" to read the documentation.'
	say ' Use the command "tedit updcd.cfg" to customize the configuration.'
	say ' Use the command "go4it" to start the build procedure.'
	say 
	say ' If you plan to burn the updated CD-ROM under the UpdCD maintanance'
	say ' system, you should add the EMX run-time DLLs to the updcd\maint'
	say ' directory and install CDRecord/2 in the directory specified in the'
	say ' UpdCD configuration file (updcd.cfg). OS2ASPI.DLL and ASPIROUT.SYS'
	say ' are already added to the system.'
end

/* ready */
say
say ' Finished installation!'

exit

/**************************/
/* read updcd config file */
/**************************/
Read_Config_File: 

	/* interpret cfgfile */
	parse arg cfgfile
	do while lines(cfgfile)
		l = linein(cfgfile)
		interpret l
	end
	call lineout cfgfile 

	/* need to know full path */
	fixes   = determine_full_path(fixes)
	updates = determine_full_path(updates)
	addons  = determine_full_path(addons)
	w4cd    = determine_full_path(w4cd)
	burn    = determine_full_path(burn)
	cdrdir  = determine_full_path(cdrdir)
	log     = determine_full_path(log)
	burnlog = determine_full_path(burnlog)
	tempraw = determine_full_path(tempraw)

	/* check virtual disk */
	if substr(flpdrv, 2, 1) <> ':' | length(flpdrv) <> 2 then flpdrv = 'A:' 
	else flpdrv = translate(flpdrv)

	/* get SCSI ID's */
	id.1 = substr(device, 1, 1)
	id.2 = substr(device, 3, 1)
	id.3 = substr(device, 5, 1)
	if datatype(id.1) <> 'NUM' then id.1 = 0
	if datatype(id.2) <> 'NUM' then id.2 = 0
	if datatype(id.3) <> 'NUM' then id.3 = 0

	/* check extrap */
	if Symbol('extrap') <> 'VAR' then extrap = ''

	/* do some other checks */
	if datatype(expert)    <> 'NUM' then expert    = 1
	if datatype(compress)  <> 'NUM' then compress  = 1
	if datatype(add_dani)  <> 'NUM' then add_dani  = 0
	if datatype(add_dummy) <> 'NUM' then add_dummy = 1
	if datatype(speed)     <> 'NUM' then speed     = 2
		
return

/**************************/
/* save updcd config file */
/**************************/
Save_Config_File: 

	parse arg cfgfile
	if cdpath <> '' then w4cd = cdpath
	call lineout cfgfile, "/* UpdCD configuration file, edit with care! */"
	call lineout cfgfile, " "
	call lineout cfgfile, "/* Go4It parameters */"
	call lineout cfgfile, "burn    = '"burn"' /* location updated CD-ROM */"
	call lineout cfgfile, "w4cd    = '"w4cd"' /* location original CD-ROM */"
	call lineout cfgfile, "fixes   = '"fixes"' /* fixes directory */"
	call lineout cfgfile, "updates = '"updates"' /* updates directory */"
	call lineout cfgfile, "addons  = '"addons"' /* addons directory */"
	call lineout cfgfile, "expert  = "expert" /* 0 = stop between steps */"
	call lineout cfgfile, "log     = '"log"'  /* log file */"
	call lineout cfgfile, " "
	call lineout cfgfile, "/* updif parameters */"
	call lineout cfgfile, "flpdrv  = translate('"flpdrv"') /* <----- change it match your virtual floppy */"
	call lineout cfgfile, "compress = "compress" /* 0 = do not compress files on boot diskette */"
	call lineout cfgfile, "add_dani = "add_dani" /* 0 = do not add DANI IDE if it is available as an addon */"
	call lineout cfgfile, "add_dummy = "add_dummy" /* 0 = do not add new ADD's and do not replace SCSI drivers with dummy driver to save space */"
	call lineout cfgfile, " "
	call lineout cfgfile, "/* burning parameters */"
	call lineout cfgfile, "rootdir = '"rootdir"'"
	call lineout cfgfile, "device  = '"id.1","id.2","id.3"' /* SCSI device ID (bus, unit, lun) */"
	call lineout cfgfile, "speed   = "speed" /* burning speed  */"
	call lineout cfgfile, "tempdir = '"strip(filespec('drive', tempraw)||filespec('path', tempraw), 'T', '\')"' /* imagefile dir  */"
	call lineout cfgfile, "tempraw = '"tempraw"' /* ISO image file */"
	call lineout cfgfile, "source  = '"burn"' /* burn directory */"
	call lineout cfgfile, "cdrdir  = '"cdrdir"' /* mkisofs (and cdrecord) dir */"
	call lineout cfgfile, "svdc_drive = flpdrv /* virtual flop   */"
	call lineout cfgfile, "burnlog = '"burnlog"' /* burn log file  */"
	call lineout cfgfile, "viewer  = 'more <'"
	call lineout cfgfile, "extrap  = '"extrap"' /* Extra parameters for cdrecord/2 */"
	call lineout cfgfile

return

/***********************************/
/* find CD or virtual floppy drive */
/***********************************/
find_drive: procedure

	parse arg option
	drives = SysDriveMap('C:', 'LOCAL')
	do while length(drives) > 0
		parse var drives drv drives
		rc = sysdriveinfo(drv)
		if rc <> '' then do
			parse var rc . free total label
			if option = 'VFDISK' & (total = 1457664 | total = 2931712) then return drv
			if option = 'CD-ROM' & free = 0 then return drv
		end
		else do
			trace -3
			'@vfctrl 'drv' 0 >nul 2>>&1'
			'@vfctrl 'drv' 1 >nul 2>>&1'
			if option = 'VFDISK' & rc = 0 then return drv
			else do
				trace -5
				'@SVDC /ef     'drv' >nul 2>>&1'
				'@SVDC /r:1.44 'drv' >nul 2>>&1'
				'@SVDC /i:1.44 'drv' >nul 2>>&1'
				if option = 'VFDISK' & rc = 0 then return drv
				else better_than_nothing = drv
			end
		end
	end

	/* best guess */
	alphabet = 'CDEFGHIJKLMNOPQRSTUVWXYZ'
	if option = 'VFDISK' then do
		p = pos(translate(substr(drv, 1, 1)), alphabet)
		if p > 0 & p <= 23 then guess = substr(alphabet, p+1, 1)||':'
		else guess = better_than_nothing
	end
	else guess = better_than_nothing

return guess

/************************************/
/* check if we have the WPURL class */
/************************************/
check_wpurl_class: procedure expose found_wpurl

	found_wpurl = 0
	cdpath = value('CDROM_PATH', , 'OS2ENVIRONMENT')
	if cdpath <> '' then return
	Call SysQueryClassList 'junk.'
	i = 1
	Do i = 1 to junk.0
		If translate(word(junk.i, 1)) = "WPURL" Then do
			found_wpurl = 1
			LEAVE
		End
	End

return

/************************/
/* Create UpdCD objects */
/************************/
create_objects: 

	rc1 = SysCreateObject('WPFolder', 'UpdCD 'version, '<WP_DESKTOP>', 'OBJECTID=<UpdCD_Folder>', 'R')
	rc2 = create_object('PRG*UpdCD*EXENAME='path'\pmupdcd.exe;')
	rc3 = create_object('PRG*Readme*EXENAME=e.exe;PARAMETERS='path'\readme.eng;')
	rc4 = create_object('PRG*Default Configuration*EXENAME=e.exe;PARAMETERS='path'\updcd.cfg;')
	rc5 = create_object('PRG*LxLite Configuration*EXENAME=e.exe;PARAMETERS='path'\bin\lxlite.cfg;')
	rc6 = create_object('PRG*UpdCD Log*EXENAME=e.exe;PARAMETERS='path'\updcd.log;')
	if found_wpurl = 1 then do /* warp 4 or higher */
		rc7 = create_object('URL*UpdCD HomePage*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=http://xenia.sote.hu/~kadzsol/rexx/sajat/updcd.htm;')
		rc8 = create_object('URL*UpdCD HomePage Mirror*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=http://www.quasarbbs.net/updcd/;')
		rc9 = create_object('URL*UpdCD Discussion Group*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=http://groups.yahoo.com/group/updcd/messages;')
		rc10 = create_object('URL*UpdCD FAQ*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=file:///'translate(translate(path,'|',':'), '/', '\')'/doc/updcdfaq.htm;')
		rc11 = create_object('URL*Order Warp Update Kit*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=file:///'translate(translate(path,'|',':'), '/', '\')'/doc/wupdkit.htm;')
		rc12 = create_object('URL*UpdCD HomePage (local)*NOTDEFAULTICON=YES;DEFAULTVIEW=CONTENTS;URL=file:///'translate(translate(path,'|',':'), '/', '\')'/updcd.htm;')
	end
	else do
		rc7 = create_object('PRG*UpdCD HomePage*Exename=netscape.exe;PARAMETERS=http://xenia.sote.hu/~kadzsol/rexx/sajat/updcd.htm;')
		rc8 = create_object('PRG*UpdCD HomePage Mirror*Exename=netscape.exe;PARAMETERS==http://www.quasarbbs.net/updcd/;')
		rc9 = create_object('PRG*UpdCD Discussion Group*Exename=netscape.exe;PARAMETERS=http://groups.yahoo.com/group/updcd/messages;')
		rc10 = create_object('PRG*UpdCD FAQ*Exename=netscape.exe;PARAMETERS=file:///'translate(translate(path,'|',':'), '/', '\')'/doc/updcdfaq.htm;')
		rc11 = create_object('PRG*Order Warp Update Kit*Exename=netscape.exe;PARAMETERS=file:///'translate(translate(path,'|',':'), '/', '\')'/doc/wupdkit.htm;')
		rc12 = create_object('PRG*UpdCD HomePage (local)*Exename=netscape.exe;PARAMETERS=file:///'translate(translate(path,'|',':'), '/', '\')'/updcd.htm;')
	end

	/* check result */
	if rc1 <> 1 | rc2 <> 1 | rc3 <> 1 | rc4 <> 1 | rc5 <> 1 | rc6 <> 1 | rc7 <> 1 | rc8 <> 1 | rc9 <> 1 | rc10 <> 1 | rc11 <> 1 | rc12 <> 1 then do
		say ' -> Error: some objects were not created.'
		say ' RC = 'rc1 rc2 rc3 rc4 rc5 rc6 rc7 rc8 rc9 rc10 rc11 rc12
		exit 9
	end
	else say ' -> The required objects were created succesfully!'

return

/***********************/
/* create updcd object */
/***********************/
create_object: procedure

	parse arg parameters
	parse value parameters with type '*' name '*' parameters
	select
		when type = 'PRG' then rc = SysCreateObject('WPProgram', name, '<UpdCD_Folder>', parameters, 'R')
		when type = 'URL' then rc = SysCreateObject('WPUrl',     name, '<UpdCD_Folder>', parameters, 'R')
		otherwise rc = 1000
	end

return rc

/**************************/
/* generate some defaults */
/**************************/
generate_cfg_defaults:

	/* get CDRecord env */
	speed  = value('CDR_SPEED',  , 'OS2ENVIRONMENT')
	if datatype(speed) <> 'NUM' then speed = 2
	cdr_dev  = value('CDR_DEVICE', , 'OS2ENVIRONMENT')
	parse value cdr_dev with id.1 ',' id.2 ',' id.3
	if datatype(id.1) <> 'NUM' then id.1 = 0
	if datatype(id.2) <> 'NUM' then id.2 = 1
	if datatype(id.3) <> 'NUM' then id.3 = 0

	/* define additional defaults */
	burn       = path'\burn'
	rc         = sysdrivemap('C:', 'LOCAL')
	w4cd 	  	 = find_drive('CD-ROM')
	fixes  	 	 = path'\fixes'
	updates  	 = path'\updates'
	addons     = path'\addons'
	expert   	 = 1
	log        = path'\updcd.log'
	flpdrv   	 = find_drive('VFDISK')
	compress 	 = 1 
	add_dani 	 = 0 
	add_dummy  = 1
	rootdir  	 = directory()
	tempdir 	 = path
	tempraw 	 = path'\temp.raw'
	source  	 = burn
	cdrdir  	 = ''
	svdc_drive = flpdrv
	burnlog 	 = path'\burn.log'
	viewer  	 = 'more <'
	extrap  	 = '' 

return

/******************************************/
/* check to see if user has RSJ installed */
/******************************************/
check_if_rsj_installed: procedure

	cdpath = value('CDROM_PATH', , 'OS2ENVIRONMENT')
	if cdpath <> '' then return
	Call SysQueryClassList 'junk.'
	i = 1
	Do i = 1 to junk.0
		If translate(word(junk.i, 1)) = 'CDWRITER' Then Do
			call SysIni , 'UPDCD', 'BPROG', 'RSJ'
			LEAVE
			End
	End

return

/********************/
/* find mkisofs.exe */
/********************/
locate_mkisofs: procedure expose cdrdir

	if cdrdir = '' then Do
		cdrdir = find_file('mkisofs.exe')
		if cdrdir = '' Then Do
			cdrdir = translate(curdir)
			say 'Cannot find mkisofs.exe, using default directory.'
		End
	End

return

/*********************************************************/
/* determine full dir path and remove trailing backslash */
/*********************************************************/
determine_full_path: procedure

	parse arg dir
	curdir = strip(directory(), 'T', '\')
	if pos(':', dir) = 0 then
		if substr(dir, 1, 1) = '\' then dir = translate(substr(curdir, 1, 2)||dir)
		else dir = translate(curdir'\'dir)
	else dir = translate(dir)
	dir = strip(dir, 'T', '\')

return dir

/**********************************************************/
/* check if vrobj.dll version 2.14 or higher is installed */
/* if not copy it to os2\dll                              */
/**********************************************************/
check_vrobj: procedure

	call RXFuncAdd 'VRLoadFuncs', 'VROBJ', 'VRLoadFuncs'
	call VRLoadFuncs
	call VRVersion('vrobj.dll')
	parse var result vx_version .
	if datatype(vx_version) <> 'NUM' | vx_version < 2.14 then do
		bdrive = bootdrive()
		say 'Copying vrobj.dll to 'bdrive'\os2\dll'
		'@copy bin\vrobj.dll 'bdrive'\os2\dll\. >nul'
	end

return

/******************/
/* check software */
/******************/
check_tooling:

	flag. = ''
	call check_installed_components bootdrive()'\config.sys'
	say
	call charout ,' -> Checking virtual disk: '
	if found.svdisk = 0 then do
		if found.vfdisk = 0 then say '	not found, you may need to install it!'
		else say '	found VFDISK!'
	end
	else say '	found SVDISK!'
	call charout ,' -> Checking ASPI router: '
	if flag.3 <> '' then say '	not found you may need to install it!'
	else say '	found it!'
	call charout ,' -> Checking EMX run-time: '
	if flag.4 <> '' then say '	not found you may need to install it!'
	else say '	found it!'
	call charout ,' -> Checking DANI IDE drv: '
	if found.daniid = 0 then say '	not found you may need to install it!'
	else say '	found it!'
	call charout ,' -> Checking DANI FLT drv: '
	if found.danifl = 0 then say '	not found you may need to install it!'
	else say '	found it!'
	call charout ,' -> Checking burning prg: '
	if found.cdrec2 = 0 then do
		if found.rsj = 0 then say '	not found you may need to install it!'
		else say '	found RSJ!'
	end
	else say '	found CDRecord!'

return
