; $VER: Install WebVision 2.1 (17.2.99)
;
; WebVision webcam viewer, by Troels Walsted Hansen <troels@stud.cs.uit.no>
; Copyright Ultima Thule Software © 1999, All Rights Reserved.
;
; Installer script for WebVision.

(set #exitmsg "\n\nThank you for installing WebVision.\n\nRemember to register if you use it for more than 30 days.")

; Welcome screen

(welcome "\nThis installer script will install WebVision.\nThe ultimate webcam viewer")

; Check the environment

(if (< (/ @installer-version 65536) 43)
	(abort "The WebVision installation script needs Installer\n"
	"version 43 or higher. It is available from Aminet.")
)

(if (< (/ (getversion) 65536) 39)
	(abort "WebVision needs AmigaOS 3.0 or higher.\n")
)

(if
	(not (= (exists "MUI:" (noreq)) 2))
	(abort "WebVision needs MUI 3.8 or higher.\n")
)

; Some smartass previous installation detection

(procedure p_tackoff string1 string2
	(set #p_tackoff_result string1)

	(if
		(>= (strlen string1) (strlen string2))

		(
			(set #p_tackoff_diff (- (strlen string1) (strlen string2)))

			(if
				(= string2 (substr string1 #p_tackoff_diff (strlen string2)))

				(set #p_tackoff_result (substr string1 0 #p_tackoff_diff))
			)
		)
	)

	(#p_tackoff_result)
)

(set #oldpath (getenv "WebVision_LASTUSEDDIR"))

(if
	#oldpath

	(set @default-dest (p_tackoff #oldpath "WebVision"))

	(
		(set #oldpath (getenv "WebTV_LASTUSEDDIR"))

		(if #oldpath
			(set @default-dest (p_tackoff #oldpath "WebTV"))
		)
	)
)

; Ask for destination directory

(set #destdir
	(askdir
		(prompt "Select destination directory. A directory called \"WebVision\" will be created here.")
		(help @askdir-help)
		(default @default-dest)
    )
)
(set @default-dest (tackon #destdir "WebVision"))

; RobR detection

(if
	(= (expandpath "") (expandpath @default-dest))
	(abort "Please select a different directory than the one you're installing from.\n")
)

; Create installation directory and copy icon for it

(makedir @default-dest)

(copyfiles
	(source "/WebVision.info")
	(dest #destdir)
)

; Copy files

(copyfiles
	(prompt "The following files will be installed:")
	(help @copyfiles-help)
	(source "")
	(choices "WebVision" "WebVision.guide" "Registration" "Register" "DockBrushes")
	(dest @default-dest)
	(confirm)
	(infos)
)

; Install OpenURL

(if 
	(askbool
		(prompt "Would you like to install the OpenURL package version 2.0?\n\nOpenURL is a shared library that allows easy sending of URLs to webbrowsers. The library is used extensively in WebVision, and is also supported by such programs as AmIRC and YAM.\n\nWithout it, you can only send URLs to the clipboard.\n\n")
		(help "It is quite harmless to install this. You will get four new files, a library called openurl.library, a handler called OpenURL-Handler, a prefs program and a MUI class called Popport.mcc which is used by the prefs program.")
		(choices "Install" "Don't install")
		(default 1)
	)

	(
		(copylib
			(prompt "Installing the OpenURL library")
			(help @copyfiles-help)
			(source "OpenURL/openurl.library")
			(dest "Libs:")
		)

		(copylib
			(prompt "Installing the OpenURL handler")
			(help @copyfiles-help)
			(source "OpenURL/OpenURL-Handler")
			(dest "L:")
		)

		(copyfiles
			(prompt "Installing the OpenURL preference program")
			(help @copyfiles-help)
			(source "OpenURL/OpenURL")
			(dest "SYS:Prefs")
			(infos)
			(noposition)
		)
		
		(copylib
			(prompt "Installing the Popport.mcc MUI class")
			(help @copyfiles-help)
			(source "OpenURL/Popport.mcc")
			(dest "MUI:Libs/MUI")
		)

		(if
			(askbool
				(prompt "You should run the OpenURL preferences program now to set your browser path(s). Would you like me to launch it?\n\nNote: You should quit all OpenURL using apps now, to allow the old version of the library to be flushed from memory.")
				(help "")
				(default 1)
			)

			(
				(run "Avail FLUSH >NIL:")
				(run "SYS:Prefs/OpenURL")
			)
		)
	)
)

; Check for prior installations

(set #webtvpath (getenv "WebTV_LASTUSEDDIR"))

(if #webtvpath
	(set #webtvinstalled
		(and
			(= (exists (tackon #webtvpath "WebTV"))     1)
			(= (exists (tackon #webtvpath "WebVision")) 0)
		)
	)

	(set #webtvinstalled 0)
)

; Copy old files over from old WebTV installation

(if
	#webtvinstalled

	(set
		#copyoldfiles
		(askbool
			(prompt "An installation of WebTV has been detected. Would you like to copy over the keyfile (if you have one), the site configuration file and the MUI configuration file from this installation?")
			(help "The following operations are performed:\nCopy WebTV.key TO WebVision.key\nCopy WebTV.sites TO WebVision.sites\nCopy ENV:mui/WEBTV.1.prefs TO ENV:mui/WEBVISION.1.prefs\nCopy ENVARC:mui/WEBTV.1.prefs TO ENVARC:mui/WEBVISION.1.prefs")
			(default 1)
		)
	)

	(set #copyoldfiles 0)
)

(if
	#copyoldfiles

	(
		(copyfiles
			(source (tackon #webtvpath "WebTV.sites"))
			(dest @default-dest)
			(newname "WebVision.sites")
			(optional "nofail")
		)

		(copyfiles
			(source (tackon #webtvpath "WebTV.key"))
			(dest @default-dest)
			(newname "WebVision.key")
			(optional "nofail")
		)

		(copyfiles
			(source "ENV:mui/WEBTV.1.prefs")
			(dest "ENV:mui")
			(newname "WEBVISION.1.prefs")
			(optional "nofail")
		)

		(copyfiles
			(source "ENVARC:mui/WEBTV.1.prefs")
			(dest "ENVARC:mui")
			(newname "WEBVISION.1.prefs")
			(optional "nofail")
		)
	)
)

; Optionally install script to make V work

(if
	(askbool
		(prompt "The Voyager webbrowser supports WebTV through sending URLs to it from the context menu of images.  To make this work with WebVision you will need a small script to send the URL on to WebVision.  Would you like to install this script?")
		(help "A script called WebTV is installed in the WebVision directory and the WebTV_LASTUSEDDIR is set to point to the WebVision directory.")
		(default 1)
	)

	(
		(copyfiles
			(source "WebTV")
			(dest @default-dest)
		)

		(protect
			(tackon @default-dest "WebTV")
			"+s"
		)

		(run (cat "Echo " @default-dest " >ENV:WebTV_LASTUSEDDIR NOLINE"))
		(run (cat "Echo " @default-dest " >ENVARC:WebTV_LASTUSEDDIR NOLINE"))
	)
)

; Optionally avoid adult cams in the installation and future auto updates

(if
	(= @user-level 0)
	(
		(set #restore-novice-level 1)
		(user 1)                        ; yes, I know this is evil
	)
	(set #restore-novice-level 0)
)

(if
	(askbool
		(prompt "WebVision is distributed together with a list of camsites.  Some of these sites are only intended for adult audiences.  If you like, these sites can be left out of the installation.\n\nAdditionally, through the new automatic sitelist update feature (registered users only) you may get new sites that are only suitable for adult audiences. If you like, you can filter out such sites when using the update service.\n\nAvoid adult webcams?")
		(help "The filter setting can be changed later inside the WebVision settings window if you wish.")
		(default 1)
	)

	(
		(textfile
			(dest "ENV:WebVision.settings")
			(include "ENV:WebVision.settings")
			(append "NoAdult: Yes\n")
		)

		(textfile
			(dest "ENVARC:WebVision.settings")
			(include "ENVARC:WebVision.settings")
			(append "NoAdult: Yes\n")
		)

		(set #newsitefile "WebVision.sites")
	)

	(set #newsitefile "WebVision_Adult.sites")
)

(if
	#restore-novice-level
	(user 0)
)

; Check for prior existance of sitefile

(set #oldsitefile (tackon @default-dest "WebVision.sites"))

(if
	(exists #oldsitefile)

	(set #sitefilechoice
		(askchoice
			(prompt "You have a web camera site configuration file called WebVision.sites already.")
			(help "Overwriting should be safe if you haven't configured any cameras of your own. Appending may create duplicate entries.")
			(choices "[2pOverwrite old with new file" "Append new file to old one" "Put old sites into \"Old sites\" group" "Skip copying new file")
			(default 2)
		)
	)

	(set #sitefilechoice 0)
)

; Overwrite

(if 
	(= #sitefilechoice 0)

	(copyfiles
		(source #newsitefile)
		(dest @default-dest)
		(newname "WebVision.sites")
;		(optional force)
	)
)

; Append

(if
	(= #sitefilechoice 1)

	(textfile
		(include #oldsitefile)
		(include #newsitefile)
		(dest #oldsitefile)
	)
)

; Old sites into group

(if
	(= #sitefilechoice 2)

	(textfile
		(append "GroupClosed: Old sites\n")
		(include #oldsitefile)
		(append "GroupEnd\n")
		(include #newsitefile)
		(dest #oldsitefile)
	)
)

; Exit

(exit #exitmsg)
