(procedure DoComplete
  (set anz (+ anz 17))
  (complete anz)
)

(complete 0)

(if (< (/ (getversion) 65536) 37)
(
        (abort wrongkickstart)
))


(set pbilldest
        (askdir
                (prompt "Where do you want to have the `phonebill' drawer installed?")
                (help @askdir-help)
                (default @default-dest)
        )
)
(set pbilldir (tackon pbilldest "Phonebill"))
(set @default-dest pbilldir)
(set docdir (tackon pbilldir "Documentation"))
(set configdir (tackon pbilldir "Config"))
(set catalogdir (tackon pbilldir "Catalogs"))
(set bakdir (tackon pbilldir "Backup"))

(set extype (exists pbilldir))
(if (= extype 1) (abort "There already exists a file called \"Phonebill\""))
(if (= extype 2)
    (
        (set oldvernum (getversion (tackon pbilldir "Phonebill")))
        (set oldver (/ oldvernum 65536))
        (set oldrev (- oldvernum (* oldver 65536) ) )
        (if (= 1 (askbool (prompt "There already exists a `phonebill' drawer. "
                                  "During installation certain files will be overwritten. "
                                  "Do you want to create a backup (\"" bakdir "\") of the files "
                                  "located in the `phonebill' drawer first?")
                          (help @askbool-help)
                 )
            )
            (
                (working "Creating backup...")
                (makedir bakdir (infos))
                (makedir (tackon bakdir "Phonebill"))
                (run ("copy \"%s\" \"%s\" all" (tackon pbilldir "~(Backup)") (tackon bakdir "Phonebill")))
            )
        )
        (run ("delete \"%s\" all" (tackon pbilldir "~(Config|Backup|#?.Log|#?.Key)")))
    )
    (
        (set oldver 0)
        (set oldrev 0)
        (set oldvernum 0)
        (makedir pbilldir (infos))
    )
)

(makedir docdir (infos))
(makedir catalogdir)

(DoComplete)

(copylib
    (help @copylib-help)
    (prompt "Installing gtlayout.library\nCopyright  1990-1994 Olaf Barthel")
    (confirm)
    (source "Libs/gtlayout.library")
    (dest "LIBS:")
)

(DoComplete)

(copyfiles
    (help @copyfiles-help)
    (source "Phonebill")
    (dest pbilldir)
    (prompt "Installing the executable program")
    (confirm)
    (infos)
)

(DoComplete)

(set docchoice
    (askoptions
        (prompt "There are three formats available for the documentation.")
        (choices "AmigaGuide Format (required for online help)" 
                 "TeX DVI Format (for printed manuals)"
                 "Plain ASCII Format")
        (help @askoptions-help)
    )
)
(if (<> 0 (BITAND docchoice 1))
(
    (copyfiles
            (prompt "Installing AmigaGuide manual...")
            (source "Documentation/Phonebill.guide")
            (dest docdir)
            (infos)
            (help @copyfiles-help)
    )
)
)

(DoComplete)

(if (<> 0 (BITAND docchoice 2))
(
    (copyfiles
            (prompt "Installing DVI manual...")
            (source "Documentation/Phonebill.DVI")
            (dest docdir)
            (infos)
            (help @copyfiles-help)
    )
)
)

(if (<> 0 (BITAND docchoice 4))
(
    (copyfiles
            (prompt "Installing ASCII manual...")
            (source "Documentation/Phonebill.doc")
            (dest docdir)
            (infos)
            (help @copyfiles-help)
    )
)
)

(copyfiles
        (prompt "Installing registration forms...")
        (source "Documentation")
        (dest docdir)
        (pattern "#?form")
        (infos)
        (help @copyfiles-help)
)

(set installedcfg 0)
(DoComplete)
(if (exists "envarc:phonebill/phonebill.prefs")
    (
        (if (= 1    (askbool(prompt (cat "Old (pre 3.0) `phonebill' configuration files have been found in \"ENV:\" and \"ENVARC:\". "
                                         "Starting from release 3.0, the configuration files "
                                         "have changed from IFF format to plain ASCII text files. "
                                         "Do you want to update these configuration files for use "
                                         "with this release, or do you want to use "
                                         "the default configuration files ?"
                                    )
                             )
                            (help @askbool-help)
                            (choices "Update configuration" "Default configuration")
                    )
            )

            (
                (working "Updating configuration")
                (makedir configdir (infos))
                (run "UpdateConfig2.x")
                (run "copy config/Scanner.config t:")
                (copyfiles
                    (help @copyfiles-help)
                    (prompt "Installing the updated configuration files")
                    (source "T:")
                    (dest configdir)
                    (pattern "#?.config")
                    (confirm)
                )
                (set installedcfg 1)
            )
        )
        (if (= 1 (askbool (prompt (cat "The files in \"ENV:\" and \"ENVARC:\" are no longer "
                                       ("needed. Do you want to move them to \"%s\" ?" (tackon bakdir "ENV"))
                                  )
                          )
                          (help @askbool-help)
                 )
            )
            (
                (working "Removing `phonebill' related files from \"ENV:\" and \"ENVARC:\"")
                (if (= 0 (exists bakdir)) (makedir bakdir))
                (makedir (tackon bakdir "ENV"))
                (makedir (tackon bakdir "ENV/Phonebill"))
                (copyfiles
                    (source "ENVARC:Phonebill")
                    (dest (tackon bakdir "ENV/Phonebill"))
                    (pattern "#?.prefs")
                )
                (run "delete env:phonebill all")
                (run "delete envarc:phonebill all")
            )
        )
    )
)


(if (= installedcfg 0)
    (
        (if (= 0 (exists configdir))
            (
                (makedir configdir (infos))
                (copyfiles
                    (help @copyfiles-help)
                    (prompt "Installing the default configuration files")
                    (source "Config")
                    (dest configdir)
                    (all)
                    (confirm)
                )
                (message "There are a lot of rates for several countries defined in the "
                         "\"rates.config\" file. Each time `phonebill' is run it has to "
                         "read this file, which takes its time. To speed-up this process, "
                         "remove the rates that you don't need.")
            )
            (
                (if (and (= oldver 3) (= oldrev 0))
                    (
                        (message "The configuration needs to be updated for use with this release of `phonebill'.")
                        (working "Updating configuration")
                        (run ("UpdateConfig3.0 \"%s\"" configdir))
                        (copyfiles
                            (help @copyfiles-help)
                            (prompt "Installing the updated configuration files")
                            (source "T:")
                            (dest configdir)
                            (pattern "#?.config")
                            (confirm)
                        )
                    )
                )
                (copyfiles
                    (help @copyfiles-help)
                    (source "Config/Scanner.config")
                    (dest configdir)
                    (prompt "Installing the newest `Scanner.config'")
                    (confirm)
                    (infos)
                )
            )
        )
        
    )
)

(
    (copyfiles
        (help @copyfiles-help)
        (prompt "Installing catalog files")
        (source "Catalogs")
        (dest catalogdir)
        (all)
        (confirm)
    )
)

(complete 100)
(exit)

