; $VER: libnix installation script V0.1
;

; Test if gcc installed

(set dir "")
(if (exists "gnu:" (noreq)) (set dir "gnu:"))
(if (exists "gcc:" (noreq)) (set dir "gcc:"))

(if (= dir "") (abort "There is no assignment 'gcc:' or 'gnu:'. "
                      "Need a completely installed gcc."))

(set @default-dest dir)
(set @app-name "libnix")

; Ask user level

(welcome "This is the libnix installation script.") 

; Look for some special programs

(if (exists "C:lha" (noreq)) (set lhapath "C:lha") (set lhapath ""))
(procedure setlhapath
  (if (= lhapath "") 
    (set lhapath (askfile (prompt "Couldn't locate your 'lha' program.")
                          (help "No help.")
                          (default "C:lha")
                 )
    )
  )
)

(if (exists "C:Search" (noreq)) (set searchpath "C:Search") (set searchpath ""))
(procedure setsearchpath
  (if (= searchpath "") 
    (set searchpath (askfile (prompt "Couldn't locate your 'Search' program.")
                          (help "No help.")
                          (default "C:Search")
                 )
    )
  )
)

(if (exists "C:Delete" (noreq)) (set deletepath "C:Delete") (set deletepath ""))
(procedure setdeletepath
  (if (= deletepath "") 
    (set deletepath (askfile (prompt "Couldn't locate your 'Delete' program.")
                             (help "No help.")
                             (default "C:Delete")
                    )
    )
  )
)

(if (exists "SYS:Rexxc/RX" (noreq)) (set rxpath "SYS:Rexxc/RX") (set rxpath ""))
(procedure setrxpath
  (if (= rxpath "") 
    (set rxpath (askfile (prompt "Couldn't locate your 'RX' program.")
                         (help "No help.")
                         (default "SYS:Rexxc/RX")
                )
    )
  )
)

; Check for the right directory

(if (<> (expandpath dir) (expandpath (pathonly @icon)))
  ((message "You unpacked libnix into the wrong directory - "
            "I will have to copy files first")
   (copyfiles (source (pathonly @icon)) (dest dir) (infos) (pattern "libnix.guide"))
   (copyfiles (source (pathonly @icon)) (dest dir) (infos) (pattern "libnix.install"))
   (copyfiles (source (pathonly @icon)) (dest dir) (infos) (pattern "libnix.specs.rexx"))
   (copyfiles (source (pathonly @icon)) (dest dir) (infos) (pattern "libnix.specs"))
   (copyfiles (source (pathonly @icon)) (dest dir) (infos) (pattern "lib"))
  )
)

(complete 20)

; Look after specs-file

(working "Looking for specs-file")
(set dirlist (cat dir "0004"))
(set oldnam "")
(while dirlist
  (; Pop subdirectory
   (set curlen (substr dirlist (- (strlen dirlist) 4))) 
   (set curdir (substr dirlist (- (strlen dirlist) (+ 4 curlen)) (+ curlen 0)))
   (set dirlist (substr dirlist 0 (- (strlen dirlist) (+ 4 curlen))))

   (foreach curdir "#?"
     (
      (set curnam (tackon curdir @each-name))

      (if (< @each-type 0) ; Test file
        (if (= @each-name "specs")
          (
           (if (NOT oldnam) (set oldnam curnam))
           (if (earlier oldnam curnam) (set oldnam curnam))
          )
        )
      )

      (if (> @each-type 0) ; Push subdirectory
        (if (AND (<> @each-name "include") (<> @each-name "os-include"))
          (set dirlist (cat dirlist curnam ("%04ld" (strlen curnam))))
        )
      )
     )
   )
  )
)

(if (= @user-level 2)
  (set oldnam 
    (askfile (prompt "Please check the right path for your specs-file.")
             (help "This message appears only in expert mode. "
                   "If you don't know what a specs-file is you "
                   "should choose the default one (which is the "
                   "newest one in the " dir " tree)."
             )
             (default oldnam)
    )
  )
)

(if (OR (<> (fileonly oldnam) "specs")
        (NOT (exists oldnam (noreq))))
  (abort "Couldn't find the specs-file. "
         "I refuse to install over an "
         "incomplete installation.")
)

; Process specs-file
; Check if specs-file already installed

(setsearchpath)
(if (OR (exists (cat oldnam ".old"))
        (NOT (run (cat searchpath " " oldnam " noixemul quiet"))))
  (set def 3)
  (set def 1)
)

(if (AND (exists (tackon dir "libnix.specs.rexx"))
         (exists (tackon dir "libnix.specs")))
  (select (askchoice
           (prompt "I have to change the specs-file now. Please choose:") 
           (help "To get the '-noixemul' option you will have to "
                 "use a specs-file that takes care of this. "
                 "libnix comes with an ARexx-script that can "
                 "change any specs-file to do the trick. "
                 "If the ARexx-server is not active you can use "
                 "a sample specs-file that comes with libnix."
                 "From gcc2.6.0 on the specsfile is aware of libnix "
                 "and need not be changed."
                 "If you don't know what to do use the sample specs-file.")
           (choices "Change specs-file (needs ARexx)"
                    "Use sample specs-file"
                    "Restore old specs-file"
                    "Skip this part")
           (default def)
          )
    ;the rexx command seems to be broken
    (
     (setrxpath)
     (run (cat rxpath " " (tackon dir "libnix.specs.rexx") " -a " oldnam))
    )
    (
     (if (NOT (exists (cat oldnam ".old")))
       (rename oldnam (cat oldnam ".old"))
     )
     (copyfiles (source (tackon dir "libnix.specs")) (dest "") (newname oldnam))
    )
    (if (exists (cat oldnam ".old"))
      (
       (delete oldnam (infos))
       (rename (cat oldnam ".old") oldnam)
      )
      (
       (setrxpath)
       (run (cat rxpath " " (tackon dir "libnix.specs.rexx") " -r " oldnam))
      )
    )
    (working "skipping...")
  )
)

(complete 40)

(if (AND (exists (tackon dir "libnix.guide"))
         (exists (tackon dir "lib/libnix/sources.lha")))
  (
   (set c1 (askoptions
            (prompt "libnix comes with (english) documentation. Please choose:")
            (help "No help.")
            (choices "amigaguide format"
                     "texinfo format")
            (default 1)
           )
   )
   (if (BITAND c1 1)
     (
      (copyfiles (source (tackon dir "libnix.guide"))
                 (dest (askdir (prompt "Please choose the destination for "
                                       "the amigaguide document.")
                               (help "No help.")
                               (default (tackon dir "guide"))
                               (newpath)
                       )
                 )
                 (infos)
      )
     )
   )
   (if (BITAND c1 2)
     (
      (working "Unarchiving texinfo document.")
      (setlhapath)
      (run (cat lhapath " x " dir "lib/libnix/sources.lha libnix.texi t:"))
      (copyfiles (source "t:libnix.texi")
                 (dest (askdir (prompt "Please choose the destination for "
                                       "the texinfo document.")
                               (help "No help.")
                               (default (tackon dir "docs"))
                               (newpath)
                       )
                 )
      )
      (delete "t:libnix.texi")
     )
   )
  )
)

(complete 60)

(if (exists (tackon dir "lib/libnix/sources.lha"))
  (select (askchoice
           (prompt "libnix comes with full sources. Please choose:")
           (help "I hope you know if you need the sources. "
                 "If you don't know delete them!")
           (choices "Unarchive them"
                    "Delete them"
                    "Skip this part")
           (default 1)
          )

    (
     (working "Unarchiving source files.")
     (setlhapath)
     (run (cat lhapath " x " dir "lib/libnix/sources.lha " dir "lib/libnix/"))
     (delete (tackon dir "lib/libnix/sources.lha") (infos))
    )
    (
     (setdeletepath)
     (run (cat deletepath " " (tackon dir "lib/libnix/~((#?.a)|(#?.o)|(libb))") " all"))
    )
    (working "skipping...")
  )
)

(complete 80)
     
(set c1 (askoptions
         (prompt "Let's clean up now: What would you like deleted?")
         (help "If you don't know what to do just choose the default.")
         (choices "installation script"
                  "original documentation file"
                  "libnix libraries and startups")
         (default 3)
        )
)
(if (BITAND c1 1)
  (
   (delete (tackon dir "libnix.install") (infos))
   (delete (tackon dir "libnix.specs.rexx") (infos))
   (delete (tackon dir "libnix.specs") (infos))
  )
)
(if (BITAND c1 2)
  (delete (tackon dir "libnix.guide") (infos))
)
(if (BITAND c1 4)
  (
   (setdeletepath)
   (run (cat deletepath " " (tackon dir "lib/libnix") " all"))
  )
)

(complete 100)
(exit)
