; Installation script for QSendFile
; by Jeremy Friesner.  Miami support by Fredrik Rambris.

(transcript "On installing QSendFile...")

(if (< (/ (getversion "libs:amarquee.library") 65536) 44)
  (message "\nNote:  You will need to install version 44+ of amarquee.library in order to run this software.  You can download this from Aminet: /comm/net/AMarquee1.44.lha"))
  
(set tcpsys
	(askchoice
		(prompt "Which TCP software do you wish to run QSendFile with?")
		(help @askdir-help)
		(choices
			"AmiTCP"
			"Miami"
		)
	)
)
		  
(set servdir
	(askdir
		(prompt "Which directory do you want to put the QSendFile executable in?  (No directory will be created)")
		(help "The QSendFile executable will be executed by inetd to receive files, or manually, from the CLI, to send files.  Thus, if you move the QSendFile executable from the directory specified here to another directory, you will need to edit your inetd.conf file to reflect its new location!")
		(default "c:")
	)
)

(copyfiles
  (source "QSendFile")
  (dest servdir)
)

(set InetDPath (tackon servdir "QSendFile"))

; modify config files!
(if (= tcpsys 0)
  (
   ; modify AmiTCP config files
   (delete "t:edit_text_succeeded")
   (set ARexxRunString (cat "sys:rexxc/rx EditTextFile.rexx amitcp:db/services QSendFile QSendFile^^^^^^^^2958/tcp"))
   (run
     (ARexxRunString)
        (prompt "\n\nTo use QSendFiled with AmiTCP, the line:\n\nQSendFile 2958/tcp\n\nneeds to be present in your amitcp:db/services file.  Do you wish me to update the file?  (Note:  QSendFile will not receive connections if this line is not precisely as shown above!)")
		(help "The inclusion of this line into amitcp:db/services is done by an ARexx script which is smart enough to correctly edit previous installations.  Also it will make a backup (to amitcp:db/services.bak) before writing.")
		(confirm)
	)
	(if (not (exists "t:edit_text_succeeded") )
		(message "The config file editing script (EditTextFile.rexx) did not complete successfully.  You may need to edit your amitcp:db/services file yourself.  Look in the 'Installation' section of the QSendFile docs for instructions.")
	)
	(delete "t:edit_text_succeeded")
	(set ARexxRunString (cat "sys:rexxc/rx EditTextFile.rexx amitcp:db/inetd.conf QSendFile QSendFile^^^^stream^^^^^^tcp^nowait^root^^^^" InetDPath))
	(run
		(ARexxRunString)
		(prompt (cat "\nAlso, the line:\n\nQSendFile stream tcp nowait root " InetDPath "\n\nneeds to be present in your amitcp:db/inetd.conf file.  Do you wish me to update that file?\n(Note:  QSendFile will not receive connections if this line is not precisely as shown above!)"))
		(help "The inclusion of this line into amitcp:db/inetd.conf is done by an ARexx script which is smart enough to correctly edit previous installations.  Also it will make a backup (to amitcp:db/inetd.conf.bak) before writing.")
		(confirm)
	)

	(if (not (exists "t:edit_text_succeeded") )
		(message "The config file editing script (EditTextFile.rexx) did not complete successfully.  You may need to edit your amitcp:db/inetd.conf file yourself.  Look in the 'Installation' section of the QSendFile docs for instructions.")
	)
  )
  (
	; Do config files for Miami: Create the textfile with a pointer to QSendFiled
	(textfile
	   (dest "t:dbtemp")
	   (append
	      "ADD services QSendFile 2958/tcp\n"
	      ("ADD inetd QSendFile stream tcp nowait root %s\n" InetDPath)
	   )
	)

	; Add the lines to envarc:MiamiChangeDB (i.e. don't overwrite! Other programs
	; may have their lines there too)
	(run "type t:dbtemp >>envarc:MiamiChangeDB")

	; If Miami shoule be running... why not make it read the new settings?
	(run "SYS:Rexxc/RX \"ADDRESS MIAMI.1;changedb\"")

	; Clean up
	(delete "t:dbtemp")
  )
)

(
    ; install QSendFile docs
	(set DocDir
		(askdir
			(prompt "Please indicate the directory to put the QSendFile.guide into: (No new directory will be created!)")
			(default "ram:")
			(help @askdir-help)
			(newpath)
		)
	)
	(copyfiles
		(source "QSendFile.guide")
		(dest DocDir)
		(infos)
	)
)

(message "QSendFile is installed.  Re-start AmiTCP to make sure the new config files have taken effect!  You may then send files to other people by typing\n " InetDPath " <hostname> [filename] at any CLI prompt.")