;--------------------------------------------------------------------------
; $VER: Lemmings 2 Level Editor Install V1.6 (08-Jan-97).
;
; Copyright 1996 Chris Hodges. All rights reserved.
;--------------------------------------------------------------------------

(delopts "force" "askuser" "nofail")

;--------------------------------------------------------------------------
; Flexible installer script! So get our installation environment.
;--------------------------------------------------------------------------

(set #kick (/ (getversion) 65536))
(set #keyfile (exists "L2Ed.key"))
(set #deinstall (exists "HDLemmings2"))
(set #lems2hd (or (exists "lems2HD.lzx") (exists "lems2HD.lha")))
(set #home @execute-dir)

(run "Version >ENV:Result L2-Editor" (safe))
(set #ed_vers (getenv "Result"))
(delete "ENV:Result" (safe))

;--------------------------------------------------------------------------
; English Installer Strings
;--------------------------------------------------------------------------

(set #ioerror "An error occured during the installation, please report it to me. \n\nThanks.")

(set #intro
    (cat
        "Welcome to the installation program for the\n\nLemmings2LevelEditor "
        (if #keyfile "REGISTEREDVersion!" "SHAREWAREDemo!")
        "\n\nPlease read the Licence before proceeding.\n"
        "Continuing on the Installation means you have read and accepted the Licence conditions!\n"
        "\nThis script supports uninstallation.\n\n"
        "Note that the Installer which comes with the Kick 3.1 release is buggy and will not "
        "install this software correctly!\n"
        "Installer 1.24 is a working release.\n"
    )
)

(set #installwhere
    (cat
        "Please select the Lemmings 2 drawer on your drive. "
        (if #lems2hd "The game will be installed, if not found!")
    )
)

(set #installwhere_help
    (cat
        "Simply select the directory you have installed "
        "Lemmings 2 into.\n\n"
        (if #lems2hd
            (
                (cat
                    "This script allows the installation of the original "
                    "disks to your harddisk, if you create a new empty "
                    "directory to install it to.\n"
                )
            ) (
                (cat
                    "If you don't have got Lemmings 2 "
                    "on your harddisk, abort the installation and "
                    "do this first.\n\nIf you don't have got the harddisk "
                    "version of Lemmings, have a look in the docs "
                    "where it can be acquired.\n"
                )
            )
        )
    )
)

(set #copyeditor
    (cat "\n"
        "Copying main Editor file...\n\n"
        "Version: " #ed_vers
    )
)

(set #mkdocsdir "\nCreating Docs Drawer...\n")
(set #copydocs_v37 "\nCopying manual (V37)...\n")
(set #copydocs_v39 "\nCopying manual (V39)...\n")

(set #copydocs_help
    (cat "\n"
        "The Docs Drawer will be used for storing the manual of the Editor. "
        "However, you may also store the guide at the following locations:\n\n"
        "PROGDIR:\n"
        "PROGDIR:Help\n"
        "PROGDIR:Docs\n"
        "HELP:\n"
        "HELP:Guide\n"
        "HELP:English\n"
    )
)

(set #copykey "\nCopying keyfile...\n")

(set #copykey_help
    (cat "\n"
        "As this is a registered version of the Editor, a keyfile is supplied "
        "to enable all features of the Editor. Normally, this keyfile is placed "
        "in PROGDIR:. Alternative locations are:\n\n"
        "PROGDIR:\n"
        "S:\n"
        "ENVARC:\n"
        "DEVS:Keyfiles\n"
    )
)

(set #makebak "\nCreating backup of Lemmings 2...\n")
(set #restbak "\nRestoring Lemmings 2 main file...\n")
(set #makebak_help
    (cat "\n"
        "Creating a backup of the main Lemmings file will allow "
        "deinstallation of the Editor.\n"
    )
)

(set #installl2_help
    (cat "\n"
        "As the game has not been found in this drawer, the "
        "installation script will try to install the game from "
        "the original disks and then decrunch the supplied "
        "DMA Design HD patch.\n\n"
        "Note that the Lemmings 2 intro will not be installed.\n\n"
        "As all three disks have the same disk name, you have "
        "to be careful to insert the right disk when prompted.\n"
    )
)

(set #applyhdpatch "\nUnpacking DMA Design HD patch...")
(set #applyhdpatch_help
    (cat "\n"
        "The next step will unpack a patch which was supplied "
        "by DMA Design to run Lemmings from harddisk. Lemmings "
        "won't work from harddisk without this patch.\n"
    )
)

(set #applypatch
    (cat "\n"
        "Applying patch and extracting original Worlds...\n\n"
        "DO NOT SKIP THIS PART!\n"
    )
)
(set #applypatch_help
    (cat "\n"
        "Installing the patch is an essential part of the installation. "
        "The following things are done:\n"
        "1. The Lemmings file is loaded.\n"
        "2. The Original Worlds are saved ('Original.wld' files).\n"
        "3. The catalog files are created ('Cat.dat' files).\n"
        "4. For each Tribe a Levels drawer is created.\n"
        "5. The patches are applied to the main file.\n"
        "6. The Lemmings file is written back.\n"
    )
)

(set #installdemo "\nDo you want me to install the demo Worlds?\n")
(set #installdemo_help
    (cat "\n"
        "Installing the demo worlds will give you new levels to play with. "
        "Each world file consumes about 86 KB.\n\n"
        "You might need 'Update Catalogs' if you're updating from an old version.\n"
    )
)

(set #askupdate "\nOld installation found. Update or install from scratch?\n")
(set #updatechoice1 "Install over old version")
(set #updatechoice2 "Just update...")
(set #update_help
    (cat "\n"
        "Well, it doesn't really matter what you do, both works, but "
        "the complete installation takes a bit longer ;)\n"
    )
)

(set #askl2disk1 "\nPlease insert Lemmings 2 Disk 1...")
(set #askl2disk2 "\nPlease insert Lemmings 2 Disk 2...")
(set #askl2disk3 "\nPlease insert Lemmings 2 Disk 3...")

(set #askremdisk
    (cat "\n"
        "Please remove the disk now. \n\n"
        "Only click on Proceed after you have taken the Disk out!"
    )
)

(set #askeddisk
    (cat "\n"
        "Please insert your L2Ed-Install disk back again.\n\n"
        "Press Proceed after the drive has stopped.\n"
    )
)

(set #askdeinstall
    (cat "\n"
        "Lemmings2LevelEditor Installation found.\n\n"
        "Do you want me remove the Editor again?\n"
    )
)

(set #askdeinstall_help
    (cat "\n"
        "You can now choose to remove the Editor & Patch from you drive. "
        "Note that the custom world files will not be deleted. "
        "The unpatched version of Lemmings 2 will be written back. \n\n"
        "If you don't want to do this, abort now.\n"
    )
)

(set #patchdelete "\nDeleting patched version of Lemmings2!\n")
(set #deinstall_exit "\nDeinstallation finished. The Editor has been removed.")

(set #err_wrongkick "Sorry, this Software requires at least Kickstart 2.04 (V37) to proceed.")
(set #err_wrongkick_help "Cannot help a lame guy like you are! Buy a new kickstart!")

(set #err_nol2bak "Couldn't find original Lemmings 2 file!")
(set #err_wrongdisk "\nWrong disk! Please read more carefully next time!")
(set #err_l2notfound "\nSorry, couldn't find Lemmings 2 in that drawer!")

(set #err_nodiskspace
    (cat "\n"
        "This installation requires at least "
        (if #lems2hd "2.7 MB" "0.5 MB")
        " free disk space. Please make this space available "
        "and start again.\n"
    )
)

(set #err_envbig "Something went wrong. Have a look at ENV:Result please.")
(set #err_prepl2 "Error occured during preparing Lemmings2Mainfile:\n\n")

;--------------------------------------------------------------------------
; Error cleanup code.
;--------------------------------------------------------------------------

(onerror
    (
        (if (> @ioerr 0)
            (message (cat #ioerror "\n\n ERROR CODE: " @ioerr))
        )
    )
)

;--------------------------------------------------------------------------
; Check Kickstart version first. Kick 2.0 or higher is a must!
;--------------------------------------------------------------------------

(set #kick (/ (getversion) 65536))
(if (< #kick 37)
    (
        (message #err_wrongkick (help #err_wrongkick_help))
        (exit (quiet))
    )
)

;--------------------------------------------------------------------------
; Check if editor already installed and run deinstaller if true.
;--------------------------------------------------------------------------

(if #deinstall
    (
        (run
            ""
            (prompt #askdeinstall)
            (help #askdeinstall_help)
            (confirm)
            (safe)
        )
        (if (exists "HDLemmings2.old")
            (
                (delete
                    "HDLemmings2"
                    (prompt #patchdelete)
                    (help #askdeinstall_help)
                    (confirm)
                )
                (rename
                    "HDLemmings2.old"
                    "HDLemmings2"
                )
            ) (
                (abort #err_nol2bak)
            )
        )
        (delete "L2-Editor")
        (delete "L2-Editor.info")
        (delete "Docs/L2Ed.guide")
        (delete "Docs/L2Ed.guide.info")
        (delete "Docs/L2EdRegForm_E.txt")
        (delete "Docs/L2EdRegForm_E.txt.info")
        (delete "Docs")
        (delete "Docs.info")
        (delete "L2EdRegForm_E.txt")
        (delete "L2EdRegForm_E.txt.info")
        (delete "ENVARC:L2Ed.prefs")
        (run "delete #?/original.wld #?/cat.dat FORCE")
        (delete "L2Ed-Deinstall")
        (delete "L2Ed-Deinstall.info")
        (exit #deinstall_exit)

    ) (

;--------------------------------------------------------------------------
; Start of Installing part of script.
;--------------------------------------------------------------------------

        (run "Run >NIL: SYS:Utilities/More Licence.txt" (safe))
        (message #intro)

        (set #path
            (askdir
                (default @default-dest)
                (prompt #installwhere)
                (help #installwhere_help)
            )
        )

;--------------------------------------------------------------------------
; Check for Lemmings in requested directory. If not installed and hd-patch
; is available, install Lemmings 2 from scratch, otherwise abort.
;--------------------------------------------------------------------------

        (if (not (exists (tackon #path "HDLemmings2")))
            (
                (if #lems2hd
                    (
                        (if (< (getdiskspace #path) 2705408)
                            (
                                (abort #err_nodiskspace)
                            )
                        )
                        (askdisk
                            (dest " ")
                            (prompt #askl2disk1)
                            (help #installl2_help)
                        )
                        (if (not (exists (" :L2D1")))
                            (
                                (abort #err_wrongdisk)
                            )
                        )
                        (copyfiles
                            (source " :")
                            (dest #path)
                            (choices "endseq.dat" "endtune" "L2D1")
                        )
                        (message #askremdisk)
                        (askdisk
                            (dest " ")
                            (prompt #askl2disk2)
                            (help #installl2_help)
                        )
                        (if (not (exists (" :L2D2")))
                            (
                                (abort #err_wrongdisk)
                            )
                        )
                        (copyfiles
                            (source " :")
                            (dest #path)
                            (choices "soundfx" "alemms.comp" "logo.dat" "Maintune" "L2D2")
                        )
                        (message #askremdisk)
                        (askdisk
                            (dest " ")
                            (prompt #askl2disk3)
                            (help #installl2_help)
                        )
                        (if (not (exists (" :L2D3")))
                            (
                                (abort #err_wrongdisk)
                            )
                        )
                        (copyfiles
                            (source " :")
                            (dest #path)
                            (all)
                        )
                        (message #askeddisk)
                        (if (exists "lems2HD.lzx")
                            (
                                (run
                                    ("Unlzx x lems2HD.lzx %s/" #path)
                                    (prompt #applyhdpatch)
                                    (help #applyhdpatch_help)
                                    (confirm)
                                )
                            ) (
                                (run
                                    ("Lha x lems2HD.lha %s/" #path)
                                    (prompt #applyhdpatch)
                                    (help #applyhdpatch_help)
                                    (confirm)
                                )
                            )
                        )
                    ) (
                        (abort #err_l2notfound)
                    )
                    (if (not (exists (tackon #path "HDLemmings2")))
                        (
                            (abort #err_l2notfound)
                        )
                    )
                )
            )
        )

;--------------------------------------------------------------------------
; Check for enough disk space.
;--------------------------------------------------------------------------

        (if (< (getdiskspace #path) 431616)
            (if (not (exists (tackon #path "L2-Editor"))) (abort #err_nodiskspace))
        )
;--------------------------------------------------------------------------
; Copy main Editor file.
;--------------------------------------------------------------------------

        (if (exists (tackon #path "L2-Editor"))
            (
                (set #update
                    (askchoice
                        (prompt #askupdate)
                        (choices #updatechoice1 #updatechoice2)
                        (help #update_help)
                        (default 1)
                     )
                 )
            ) (
                (set #update 0)
            )
        )

        (if #update
            (
                (copyfiles
                    (prompt #copyeditor)
                    (help @copyfiles-help)
                    (source "L2-Editor")
                    (dest #path)
                    (confirm)
                )
            ) (
                (copyfiles
                    (prompt #copyeditor)
                    (help @copyfiles-help)
                    (source "L2-Editor")
                    (dest #path)
                    (infos)
                    (confirm)
                )
            )
        )

;--------------------------------------------------------------------------
; Copy manual, either v37 or v39, depending on the kickstartversion.
;--------------------------------------------------------------------------

        (makedir
            (tackon #path "Docs")
            (prompt #mkdocsdir)
            (help #copydocs_help)
            (infos)
            (confirm)
        )
        (if (< #kick 39)
            (
                (copyfiles
                    (prompt #copydocs_v37)
                    (help #copydocs_help)
                    (source "L2Ed_E_V37.guide")
                    (dest (tackon #path "Docs"))
                    (newname "L2Ed.guide")
                    (infos)
                    (confirm)
                )
            ) (
                (copyfiles
                    (prompt #copydocs_v39)
                    (help #copydocs_help)
                    (source "L2Ed_E.guide")
                    (dest (tackon #path "Docs"))
                    (newname "L2Ed.guide")
                    (infos)
                    (confirm)
                )
            )
        )

;--------------------------------------------------------------------------
; Copy keyfile, if available.
;--------------------------------------------------------------------------

        (if #keyfile
            (
                (copyfiles
                    (prompt #copykey)
                    (help #copykey_help)
                    (source "L2Ed.key")
                    (dest #path)
                    (confirm)
                )
            ) (
                (if (exists (tackon #path "Docs"))
                    (
                        (copyfiles
                            (source "L2EdRegForm_E.txt")
                            (dest (tackon #path "Docs"))
                            (infos)
                        )
                        (copyfiles
                            (source "L2EdRegForm_E.txt")
                            (dest #path)
                            (infos)
                        )
                    )
                )
            )
        )

;--------------------------------------------------------------------------
; Create Lemmings 2 backup.
;--------------------------------------------------------------------------

        (if (not #update)
            (

                (if (exists (tackon #path "HDLemmings2.old"))
                    (
                        (copyfiles
                            (prompt #restbak)
                            (help #makebak_help)
                            (source (tackon #path "HDLemmings2.old"))
                            (dest #path)
                            (newname "HDLemmings2")
                            (confirm)
                        )
                    ) (
                        (if @pretend
                            (
                                (run
                                    ""
                                    (prompt #makebak)
                                    (help #makebak_help)
                                    (confirm)
                                    (safe)
                                )
                            ) (
                                (copyfiles
                                    (prompt #makebak)
                                    (help #makebak_help)
                                    (source (tackon #path "HDLemmings2"))
                                    (dest #path)
                                    (newname "HDLemmings2.old")
                                    (confirm)
                                )
                            )
                        )
                    )
                )

;--------------------------------------------------------------------------
; Apply editor patch to Lemmings 2 file.
;--------------------------------------------------------------------------

                (set @execute-dir #path)
                (copyfiles
                    (source "PrepL2")
                    (dest #path)
                )
                (run "delete #?/original.wld FORCE")
                (run
                    (tackon #path "PrepL2 >ENV:Result HDLemmings2")
                    (prompt #applypatch)
                    (help #applypatch_help)
                    (confirm)
                )
                (set @execute-dir #home)
                (delete (tackon #path "PrepL2"))
                (set #reslen (getsize "ENV:Result"))
                (if (> #reslen 512)
                    (
                        (abort #err_envbig)
                    )
                )
                (if (> #reslen 0)
                    (
                        (abort (cat #err_prepl2 (getenv "Result")))
                    )
                )
                (delete "ENV:Result")

;--------------------------------------------------------------------------
; Install demoworlds either from dir or from archive.
;--------------------------------------------------------------------------

                (if (exists ("Demoworlds.lzx"))
                    (
                        (run
                            "Unlzx x Demoworlds.lzx T:"
                            (safe)
                        )
                        (copyfiles
                            (prompt #installdemo)
                            (help #installdemo_help)
                            (source "T:Demoworlds")
                            (dest #path)
                            (all)
                            (confirm)
                        )
                        (run
                            "Delete T:Demoworlds ALL"
                            (safe)
                        )
                    ) (
                        (if (exists ("Demoworlds"))
                            (
                                (copyfiles
                                    (prompt #installdemo)
                                    (help #installdemo_help)
                                    (source "Demoworlds")
                                    (dest #path)
                                    (all)
                                    (confirm)
                                )
                            )
                        )
                    )
                )
           )
       )
;--------------------------------------------------------------------------
; Create Deinstallation script
;--------------------------------------------------------------------------

        (delete (tackon #path "L2Ed-Uninstall"))
        (delete (tackon #path "L2Ed-Uninstall.info"))
        (copyfiles
            (source "L2Ed-Install")
            (dest #path)
            (newname "L2Ed-Deinstall")
            (infos)
        )
        (tooltype
            (dest (tackon #path "L2Ed-Deinstall"))
            (settooltype "SCRIPT" "L2Ed-Deinstall")
        )

;--------------------------------------------------------------------------
; Installation successful!
;--------------------------------------------------------------------------

        (set @default-dest #path)
        (exit)
    )
)

