#!/usr/bin/pil
## (c) Guillermo R. Palavecino

## This scripts fetches the debian control scripts/info from a
## relatively old version. This one works fine, however newer ones
## shouldn't break anything. Haven't tried though...

(setq *DebCtl "picolisp_3.1.0.7-1.debian.tar.gz"
      *HttpMirror "http://ftp.de.debian.org/debian/pool/main/p/picolisp/")

(de mkTmpDir ()
   (in '(mktemp "-d") (line T)) )

## Execute command and return Ret if successful.
(de callCheck ("Ret" . "Call")
   (when (apply call (mapcar eval "Call"))
      "Ret" ) )

## If Path is not specified, create dir Name in pwd
(de mkDir (Name Path)
   (let D (relative Name Path)
      (callCheck D 'mkdir D) ) )

(de rmDir (Path)
   (when Path (call 'rm "-rf" Path)) )

(de exists? (Path)
   (bool (info Path)) )

(de dir? (Path)
   (and (info Path)
      (=T (car @)) ) )

(de file? (Path)
   (and (info Path)
      (not
         (=T (car @)) ) ) )

(de hgRepo? (Path)
   (dir? (relative ".hg" Path)) )

## Check if Path is a picolisp source dir.
(de pilDir? (Path)
   (file? (relative "pil" Path)) )

## Construct absolute path if base path is specified.
(de relative (File Path)
   (pack (or Path ".") "/" File) )

## Get HG repo current revision number
(de getRevNum ()
   (in '(hg id -n)
      (format
         (car (split (line) "+")) ) ) )

## Get HG repo current revision hash
(de getRevID ()
   (in '(hg id -i)
      (car (split (line) "+")) ) )

## (withTmpDir sym . prg)
## Creates a temporal dir and binds it to sym.
(de withTmpDir "Prg"
   (bind (cons
            (cons (pop '"Prg")
               (mkTmpDir) ) )
      (run "Prg") ) )

## Get the sources version.
(de parseVersion ()
   (use *Version
      (load "src64/version.l")
      (glue "." *Version) ) )

## Sorry the order of these args is backwards.
## If Path (origin) is not specified, copy pwd to Dest
(de copyDir (Dest Path)
   (call 'cp "-r" (relative Path) Dest) )

## Ugh!
## This works, but I haven't tested it thoroughly. It roughly does this:
## * creates a copy of the sources in a temporal dir to keep things clean
## * fetches a deb-src package to reuse the debian control files
## * if the sources contain repo history, get the current version info
## * add a changelog entry in the debian control files with current version
## * build. If anything goes wrong, abort without erasing the temp dir
## * move the .deb to the original sources dir, and erase the temporal copy
(finally (bye)
   (ifn (pilDir?)
      (quit "Not in a picolisp directory")
      (use (StartingDir PilCopy Version)
         (withTmpDir Tmp
            (copyDir (setq PilCopy (mkDir "picolisp" Tmp)))
            (setq
               StartingDir (cd PilCopy)
               Version (parseVersion) )
            (unless (file? *DebCtl)
               (call 'wget (pack *HttpMirror *DebCtl)) )
            (call 'tar 'xzf *DebCtl)
            (when (hgRepo?)
               (setq Version (pack Version "-" (getRevNum)))
               (let Changelog (in "debian/changelog" (till NIL T))
                  (out "debian/changelog"
                     (prinl "picolisp (" Version ") unstable; urgency=low")
                     (prinl "  * HG version " (getRevNum) ":" (getRevID))
                     (prinl "  -- Auto BackPort script "
                        (in '(date "-R") (line T)) )
                     (prinl)
                     (prinl Changelog) ) ) )
            (ifn (call 'fakeroot "debian/rules" "binary")
               (quit (pack "Error, partial build at " Tmp))
               (cd Tmp)
               (when (call 'mv (find '[(X) (pre? (pack "picolisp_" Version) X)] (dir)) StartingDir)
                  (cd StartingDir)
                  (rmDir Tmp) ) ) ) ) ) )
