; $VER: Install-a-Skin 1.2 (19 May 2003)
;  Dave Crawford
; Created with GoldED 6.2

; Version History
; -April 21 Initial release
; -April 27 fixed the test for existing skins
; -April 27 removed the examination for MorphOS:
; -May   18 added routine to look for a skins dir without a default dir
; -May   18 removed bug that looked for a wrong directory

; error handling
(onerror
    (if (> @ioerr 0)
        (
            (message

                ("An error has occurred during installation. Please check the log file to understand the error.")
            )
        )
    )

    (exit (quiet))
)

(set #welcome_msg "\nWelcome to Install-a-Skin, the skin installer for MorphOS.")

(welcome #welcome_msg)

; --- Begin Procedures
;MOVEFILES
(procedure MOVEFILES
    (set #sourcefile
        (askfile
            (prompt "Select a MorphOS Skin archive. Must be in Lha format.")
            (help @askfile-help)
            (default "RAM:")
        )
    )
    (set #skin_name
        (askstring
            (prompt "Enter a name for the skin.")
            (help @askstring-help)
            (default "Rename_Me")
        )
    )
    (set #skin_extract
         (cat "lha x " #sourcefile " T:")
    )
    (run #skin_extract (safe))
    (set #skin_dir
         (cat "T:" #skin_name)
    )

    (if (= (rename "T:MorphOS" #skin_dir) 0)
        (
            (exit "\n\n\nRename of new skin FAILED." (quiet))
        )
    )
    (set #skin_test
        (cat "SYS:Prefs/Presets/Skins/" #skin_name)
    )
    ;--
    (if (>= (exists #skin_test (noreq)) 1)
      (
        (set #ex_result
            (askchoice
                (prompt "A Skin with that name already exists.  What would you like to do?")
                (help @askchoice-help)
                (choices

                    "Abort"
                    "Rename"
                    "Overwrite"
                )
                (default 0)
            )
         )
        (if (= #ex_result 0)
            (
               (exit "\n\n\n User Abort" (quiet))
            )
            ; else
            (
                (if (= #ex_result 1)
                    (
                        (message "rename")
                        (working "\n\n\nCleaning up temp files...")
                        (set #del_command
                          (cat "delete " #skin_dir " force all")
                         )
                         (working "\n\n\nCleaning up temp files...")
                         (run #del_command)
                         (MOVEFILES)
                    )
                    ; else
                    (
                        (set #del_command
                           (cat "delete Sys:Prefs/Presets/Skins/" #skin_name)
                        )
                        (run #del_command)
                    )
                )

            )
        )
      )
    )
    (copyfiles
        (source "T:")
        (pattern #skin_name)
        (dest "SYS:Prefs/Presets/skins/")
        (optional "oknodelete" "force" "askuser")
    )
    (working "\n\n\nCleaning up temp files...")
    (set #del_command
       (cat "delete " #skin_dir " force all")
    )
    (working "\n\n\nCleaning up temp files...")
    (run #del_command)
    (set #another_skin
        (askchoice
            (prompt "Would you like to install another skin?")
            (help @askchoice-help)
            (choices

                "Yes"
                "NO"
            )
            (default 0)
        )
    )
    (if (= #another_skin 0)

        (
            (working "\n\n\nContinuing")
        )

        ; else

        (
           (set #continue 1)
        )
    )
);end MOVEFILES
; SetDefault
(procedure SETDEFAULT
  (working "\n\n\nInstalling needed directories and files.")
  (makedir "SYS:Prefs/Presets/Skins")
  (copyfiles
        (source "Sys:Prefs/Presets/")
        (pattern "MorphOS")
        (dest "SYS:Prefs/Presets/Skins/")
        (optional "oknodelete" "force" "askuser")
  )
  (if (= (rename "SYS:Prefs/Presets/Skins/MorphOS" "SYS:Prefs/Presets/Skins/Default") 0)
      (
         (message "Renaming of Skins/MorphOS to Skins/Default failed.")   )
  )
  (message "\n\n\nSkins directory created, and default skin backed up as \"default\".")
)
; --- End Procedures
; --- MAIN
(working "\n\nChecking for skins path...")
(if (< (exists "SYS:Prefs/Presets/Skins" (noreq)) 2)

    (
      (if (= (exists "SYS:Prefs/Presets/Skins" (noreq)) 1)
          (
             (message "\n\nWARNING\n\nThere is a file named \"Skins\" where a directory should be.  Please remove this file, and restart Install-a-Skin.")
             (exit (quiet))
          )
          ;else
          (
             (SETDEFAULT)
          )
      )

    )
    ;else
    (
      (if (< (exists "SYS:prefs/presets/skins/default" (noreq)) 2)
        (
          (set #cd_skin

              (askchoice

                  (prompt "\nWARNING: Default Skin not detected! Create Default Skin from MorphOS CD?\n")

                  (help @askchoice-help)

                  (choices

                      "Yes"
                      "No"
                  )

                  (default 0)
              )
          )
              (if (= #cd_skin 0)

                  (
                     (makedir "SYS:prefs/presets/skins/Default")
                     (copyfiles

                         (source "MorphOSBoot:Prefs/Presets/MorphOS/")

                         (dest "SYS:Prefs/Presets/Skins/Default/")

                         (infos)

                         (pattern "#?")

                         (optional "fail")
                     )
                     (exit "\n\nDefault Skin installed from CD.  Exiting Script.  Restart installer for adding more skins." (quiet))
                  )

                  ; else

                  (
                     (message "\N\N\NSkipping this time!" )
                  )
              )

          )

        )
    )
)
(set #continue 0)
(until (> #continue 0)
    (MOVEFILES)
)
(set #change_env
    (askchoice
        (prompt "\nDo you want to change your skin for the next reboot?")
        (help @askchoice-help)
        (choices

             "Yes"
             "No"

        )
        (default 0)
    )

)
(if (= #change_env 0)
    (
      (set #skin_env
        (askdir
           (prompt "Choose the directory of the skin you wish to use.")
           (help @askdir-help)
           (default "SYS:Prefs/Presets/Skins")
        )
      )
      (if (< (exists #skin_env (noreq)) 2)
          (
             (message "\n\n\nSelected skin does not exist, settings have not been changed.")
          )
          ;
          (
             (set #set_env
                 (cat "Setenv SAVE SYS/SKINPATH " #skin_env )
             )
             (run #set_env)
          )
      )

    )
)

(exit "Selected skins have been installed to your system. Use SkinShuffle to change skins without the installer."(quiet))

