;=============================================================================
; $VER: Backup_Roadshow_Config 1.5 (20.9.2023)
;=============================================================================

; Make a backup copy of the Roadshow configuration files
(procedure P_MakeBackup
(
	(message (cat
		"This script will make backup copies of the configuration files "
		"stored in 'DEVS:NetInterfaces', 'DEVS:Internet', 'SYS:Storage/NetInterfaces', "
		"'S:IPF' and 'S:PPP-Configurations', as far as these exist."
		"\n\n"
		"These copies will be stored in a drawer whose name you will be prompted to "
		"enter."
	))

	; Select or make a new directory to store the backup copy in
	(set #dir-acceptable 0)
	(while (not #dir-acceptable)
	(
		(set #backup-dir (askdir
			(prompt (cat
				"Please select or make a new drawer in which backup copies of the "
				"Roadshow configuration files should be stored!"
			))
			(newpath)
			(help @askdir-help)
			(default (tackon @default-dest "Roadshow-Config-Backup"))
		))
		(set #backup-dir-exists (exists #backup-dir))

		(if (= 0 #backup-dir-exists)
		(
			(set #makedir-success 0)

			(trap 4
				(set #makedir-success
					(makedir #backup-dir (infos)
				))
			)

			(if #makedir-success
			(
				(set #dir-acceptable 1)
			)
			(
				(message (cat
					"The drawer '" #backup-dir "' did not already exist and could not be "
					"created either. Please try again and select or make a new drawer."
				))
			))
		))

		(if (= 1 #backup-dir-exists)
		(
			(message (cat
				"Please select or make a new drawer; '" #backup-dir "' already exists."
			))
		))

		(if (= 2 #backup-dir-exists)
		(
			; That's good enough for us
			(set #dir-acceptable 1)
		))
	))

	; Create the respective subdirectories first; the
	; (copyfiles) command cannot do this all by itself.
	(makedir (tackon #backup-dir "DEVS") (infos))
	(makedir (tackon #backup-dir "Storage") (infos))
	(makedir (tackon #backup-dir "S") (infos))

	; Now start copying...
	(if (= (exists "DEVS:Internet" (noreq)) 2)
	(
		(copyfiles
			(source "DEVS:Internet")
			(dest (tackon #backup-dir "DEVS/Internet"))
			(all)
		)
	))

	(if (= (exists "DEVS:NetInterfaces" (noreq)) 2)
	(
		(complete 20)

		(copyfiles
			(source "DEVS:NetInterfaces")
			(dest (tackon #backup-dir "DEVS/NetInterfaces"))
			(all)
			(infos)
		)
	))

	(if (= (exists "SYS:Storage/NetInterfaces" (noreq)) 2)
	(
		(complete 40)

		(copyfiles
			(source "SYS:Storage/NetInterfaces")
			(dest (tackon #backup-dir "Storage/NetInterfaces"))
			(all)
			(infos)
		)
	))

	(if (= (exists "S:IPF" (noreq)) 2)
	(
		(complete 60)

		(copyfiles
			(source "S:IPF")
			(dest (tackon #backup-dir "S/IPF"))
			(all)
			(infos)
		)
	))

	(if (= (exists "S:PPP-Configurations" (noreq)) 2)
	(
		(complete 80)
	
		(copyfiles
			(source "S:PPP-Configurations")
			(dest (tackon #backup-dir "S/PPP-Configurations"))
			(all)
			(infos)
		)
	))

	(complete 100)

	(message (cat
		"Backup copies of your Roadshow configuration files were "
		"stored in '" #backup-dir "'."
	))
))

;=============================================================================
; Start of installation script
;=============================================================================
(complete 0)

(if (< (/ @installer-version 65536) 43)
(
	(abort (cat
		"This installation script requires the Installer tool V43"
	))
))

(if (< (getversion) (* 37 65536))
	(abort (cat
		"Roadshow requires operating system "
		"version 2.04 or higher to work."
	))
)

;=============================================================================

(welcome "Back up the Roadshow TCP/IP stack configuration")

(if (= (exists "LIBS:bsdsocket.library" (noreq)) 0)
(
	(message (cat
		"The Roadshow TCP/IP stack does not seem to be installed on your system, "
		"or at least there is no 'bsdsocket.library' found in the LIBS: "
		"directory."
		"\n\n"
		"You may want to verify if there are still parts of Roadshow installed on "
		"your system if you are certain that you previously installed it, or aborted "
		"an installation in progress."
	))
))

(P_MakeBackup)
