;;
;; Pine Installation script for Pine 3.93
;;
(if
(and
    ( = (exists "libs:ixemul.library") 1)
    ( < ( / (getversion "libs:ixemul.library") 65536) 43 ) )
    (abort "You must have ixemul.library version >43.0 to run pine.")
)

(set @source-dir
    (if (= 1 (exists @icon) )
	(pathonly (expandpath @icon))
	(expandpath @icon)
    )
)

(set pine-install "")

(set @source-lib-dir (tackon @source-dir "lib") )

(set @source-bin-dir (tackon @source-dir "bin") )

(set @source-etc-dir (tackon @source-dir "etc") )

(debug "source-dir is " @source-dir)
(debug "source-bin-dir is " @source-bin-dir)

(if ( = (exists "pine:" (noreq) )  2 )
    (copyfiles
	(prompt "Updating previous version")
	(source @source-dir)
	(dest "pine:")
	(help @copyfiles-help)
	(all)
	(infos)
    )
    (
	(set @pine-dest
	    (askdir
		(prompt "Enter directory for installing pine. The script will create a directory with pine 3.93")
		(help @askdir-help)
		(default "Work:pine3.93")
	    )
	)

	(set @pine-dest (tackon @pine-dest "pine3.93") )

	;only copy files if installing into another dir
	(if (= @source-dir @pine-dest)
	    (debug "installing into same directory")
	    (
		(makedir @pine-dest)
		(copyfiles
		    (prompt "Copying necessary files")
		    (source @source-dir)
		    (dest @pine-dest)
		    (help @copyfiles-help)
		    (all)
		    (infos)
		)
	    )
	)

	(set pine-install "assign pine: ")
	(set pine-install (cat pine-install @pine-dest) )
	(set pine-install (cat pine-install "\npath pine: add\n") )
    )
)

(if ( = (exists "bin:" (noreq) )  2 )
    (
	(if (= (expandpath "bin:") @source-bin-dir)
	    (debug "installing bin into same directory")
	    (
		(copyfiles
		    (prompt "Copying bin to bin:")
		    (source "bin")
		    (dest "bin:")
		    (confirm)
		    (help @copyfiles-help)
		    (all)
		)
		(delete @source-bin-dir)

		(if ( = (exists "bin:csh" (noreq) )  1 )
		    (debug "bin:csh exists")
		    (copyfiles
			(prompt "Copying bin/sh to bin:csh")
			(source "bin:sh")
			(dest "bin:csh")
			(help @copyfiles-help)
		    )
		)
	    )
	)
    )
    (set pine-install (cat pine-install "assign bin: pine:bin\n") )
)

(if ( = (exists "lib:" (noreq) )  2 )
    (
	(if (= @source-lib-dir (expandpath "lib:") )
	    (debug "installing lib into same directory")
	    (
		(copyfiles
		    (prompt "Copying lib to lib:")
		    (source @source-lib-dir)
		    (dest "lib:")
		    (confirm)
		    (help @copyfiles-help)
		    (all)
		)
		(delete @source-lib-dir)
	    )
	)
    )
    (set pine-install (cat pine-install "assign lib: pine:lib\n") )
)

(if ( = (exists "usr:" (noreq) )  2 )
    (debug "usr: exists")
    (set pine-install (cat pine-install "assign usr: pine:\n") )
)

(if ( = (exists "etc:" (noreq) )  2)
    (
	(if (= @source-etc-dir (expandpath "etc:") )
	    (debug "installing etc into same directory")
	    (
		(copyfiles
		    (prompt "Copying etc to etc:")
		    (source @source-etc-dir)
		    (dest "etc:")
		    (help @copyfiles-help)
		    (confirm)
		    (all)
		)
		(delete @source-etc-dir)
	    )
	)
    )
    (set pine-install (cat pine-install "assign etc: pine:etc") )
)

(debug "pine-install is" pine-install)

(startup "pine" pine-install
    (prompt ("%s%s"
	    "Add the following lines to s:user-startup\n"
	    pine-install
	    )
    )
    (help @startup-help)
    (command pine-install)
)


(if ( = (exists "env:TERM" (noreq) ) 0)
    (
	(message "Setting environment variable TERM to amiga")
	(textfile
	    (dest "env:TERM")
	    (append "amiga")
	)

	(message "Setting envarc:TERM to amiga")
	(textfile
	    (dest "envarc:TERM")
	    (append "amiga")
	)
    )
    (debug "env:term exists")
)

(message "Installation complete. Please read the file pine.guide for information"
     " on configuring and running Pine. Happy emailing"
)
(exit (quiet))

