#!/bin/sh
#
#  nightly
#
#  This Tcl script is designed to take a snapshot of this
#  sourceforge project's current CVS state, making a "nightly
#  tarball" of the sources, which can be downloaded from
#  the project's sourceforge web site.
#
#  While this could probably be done with a shell script,
#  string handling is significantly easier in Tcl.
#
#  usage:
#    nightly.tcl projname [projname projname ...]
#
#
#  This script is coded specifically to work with sourceforge.
#  It expects the project's HTML directory to be under 
#  /home/groups/p/pr/projectname, and is written specifically
#  to work on the linux shell servers from cron.  It will
#  create the tarballs in htdocs/nightly subdirectory.
#
#  This script has the side effect of leaving a copy of
#  the (anonymously) checked out source code in the
#  project's web directory.
#
#  Copyright (C) Bob Techentin
#  January 2, 2002
#

# restart using tclsh \
exec tclsh "$0" "$@"


# get project names from command line arguments
set projectNames $argv
if { [llength $projectNames] < 1 } {
    puts "usage:  [info script] projectName \[projectName...\]"
    exit
}


# process each named project
foreach project $projectNames {

    #  Generate a project log message for cron output
    puts "$argv0 processing $project"

    # change to directory - this might change if sf.net
    # changes their directory hierarchy
    set directory [file join / home groups \
	    [string range $project 0 0] [string range $project 0 1] \
	    $project]
    cd $directory

    #  make sure that we've got a place to put the
    #  tarballs.
    file mkdir htdocs/nightly

    # set cvs variables for anonymous checkout
    set env(CVS_RSH) "ssh"
    set env(CVSROOT) ":pserver:anonymous@cvs.$project.sourceforge.net:/cvsroot/$project"
    set env(CVS_PASSFILE) $env(HOME)/temp-cvs-passfile

    # fake out cvs login, so we don't have to use expect to
    # drive the login/password prompt.  (sf.net doesn't have expect)
    exec echo $env(CVSROOT) A > $env(CVS_PASSFILE)


    # get list of module names for checkout
    # (default is just to use project name)
    set moduleList [exec cvs checkout -c]
    if { [llength $moduleList] == 0} {
        set moduleList $project
    }

    # check out and tar up each module
    foreach {module junk} $moduleList {

        #  Generate module log message for cron output
        puts "    module: $module"

        # check out latest code
        catch {exec cvs -z3 co $module}

        # delete any old tarballs
        set oldFiles [glob -nocomplain htdocs/nightly/$module-*.tar.gz]
        eval file delete -force $oldFiles

        # create new tarball
        set date [clock format [clock seconds] -format %Y%m%d]
        exec tar cfz htdocs/nightly/$module-$date.tar.gz $module

    }

    #  remove the temporary password file
    file delete -force $env(CVS_PASSFILE)

}