#!/bin/sh
# the next line restarts using wish \
exec tclsh $0 $@

source [ file join $env(TCLTOOLS_HOME) TclToolInits.tcl ]
TclToolInits GUI GUIUTILS UTILS
lappend auto_path [file join $env(UDFTOOL_HOME) UtilPgms PkgMgr ]
package require PkgMgr

set gI(pFlds) [list REV DIR OPT PTS ENV DEP SCMD RCMP PFMT WPTS WDEP]
set gI(sFlds) [list REV DIR OPT ENV DEP SCMD RCMP PFMT]
set gI(pdBF) [file join $env(UDFTOOL_HOME) ConFigs Pkg.db]

if [file exists $gI(pdBF)] {
   if ![PMreadPDB] {
       puts stderr "CAN'T OPEN PACKAGE DATABASE $gI(pdBF)"
       exit
   }
} else {
   puts stderr "NO PACKAGE DATABASE $gI(pdBF)"
   exit
}

set cwd [pwd]
set Cmd ""
set pName [lindex $argv 0]
puts stderr "Making Package $pName"

set P [lsearch $pdB(PKG) $pName]
if { $P < 0 } { 
   puts stderr "UNKNOWN PACKAGE $pName"
   exit 
}

###########################################################################
# BUILD THE PACKAGE
###########################################################################

set pRev [lindex $pdB(REV) $P ]
set dDir [split [lindex $pdB(DIR) $P] "^"]
set dNames [split [lindex $dDir 0] "+"]
set sDir $env([lindex $dNames 0])
set pH $env([lindex $dNames 0])
set dLen [llength $dNames]
for { set I 1 } { $I < $dLen } { incr I } {
   set sDir [file join $sDir [lindex $dNames $I]]
}

###########################################################################
# HEAD to the package directory
###########################################################################

cd $sDir

###########################################################################
# THIS is the name of the package that we will be building less the .tgz
#    and a list of the files that are included in that package.
###########################################################################

set tName(0) $pName.$pRev 
set ToTar(0) [PMpkgManifest [lindex $pdB(PTS) $P]]

###########################################################################
#  Get the same thing if there is a WINDOWS specific package which
#     goes along with this package.
###########################################################################

if { [string length [lindex $pdB(WPTS) $P] ] > 0 } {
    set tB 2
    set tName(1) $pName.$pRev.WIN32B
    set ToTar(1) [PMpkgManifest [lindex $pdB(WPTS) $P]]
} else { set tB 1 }

###########################################################################
#  IF we need to delete previous versions do that now.  This will get
#     both the generic and WINDOWS builds should such exist.
###########################################################################

set pHome [file join $pH PKGS]
set fNames [exec $SySCmDs(ls) $pHome ]
set nP [llength $fNames] 
for { set I 0 } { $I < $nP } { incr I } {
    set fN [lindex [split [lindex $fNames $I] "."] 0]
    if { [string compare $fN $pName] == 0 } {
       file delete -force [file join $pH PKGS [lindex $fNames $I]]
       set gI(OLDBUILD) [file rootname [lindex $fNames $I]]
       puts stderr "REMOVING [file rootname [lindex $fNames $I]]"
    }
}

############################################################################
#  NOW loop over the builds which will be 1 if there is no WINDOWS specific
#    build and 2 if there is
############################################################################

for { set L 0 } { $L < $tB } { incr L } {
   cd $sDir

###########################################################################
# TAR them all together and put the tar file into the PKGS directory
#    under the package home.  NOTE: tar and gzip are done separately for
#    those not using the GNU tar.
###########################################################################

   set ToTar($L) [string trim $ToTar($L)]
   eval exec tar -cf $tName($L).tar $ToTar($L)
   eval exec gzip -f $tName($L).tar 
   file rename -force $tName($L).tar.gz [file join $pH PKGS $tName($L).tgz]

###########################################################################
#  CREATE the database portion of the package
###########################################################################

   PMpkgPDE $pH $tName($L) $P $L

###########################################################################
# COMBINE the database and file parts of the package together
###########################################################################

   cd [file join $pH PKGS]

   eval exec tar -cf $tName($L).tar $tName($L).tgz $tName($L).dbe
   file delete -force $tName($L).dbe
   file delete -force $tName($L).tgz

   puts stderr "BUILT $tName($L)"
}

###########################################################################
# BACK to where we started.
###########################################################################

cd $cwd
