#!/bin/sh
#============================================================
# create a source tar ball from the tcl/websh project 
# $Id: makedist 383181 2006-03-04 19:19:44Z ronnie $
#============================================================
# \
exec tclsh "$0" "$@"

set repository "http://svn.apache.org/repos/asf/tcl/websh"

proc usage {} {
    global argv0
    fatal "usage: $argv0 <build>\n  where <build> is the name of a tagged build (e.g. 3.5.0)\n  or trunk for the latest repository version\n  or builds for a list of available tagged builds"
}

proc fatal {msg} {
    puts stderr $msg
    exit 1
}

if {$argc != 1} {
    usage
}

# get build from command line
set build [lindex $argv 0]
if {"$build" == "trunk"} {
    set trunk 1
    append build "-[clock format [clock seconds] -format "%Y%m%d"]"
} elseif {"$build" == "builds"} {
    set builds ""
    regsub -all "/" [exec svn list $repository/tags] "" builds
    puts $builds
    puts "trunk"
    exit
} else {
    set trunk 0
}

if {!$trunk} {
    # check whether build is valid
    puts "checking for build $build"

    if {[catch {
	set svnlist [exec svn list $repository/tags]
	if {![regexp "$build/" $svnlist]} {
	    fatal "build $build is not tagged"
	}
    } msg]} {fatal $msg}
}

# checkout build in a temporary directory
set tmpdir [pid].tmp
file mkdir $tmpdir
cd $tmpdir

if {$trunk} {
    puts "checking out trunk in temporary directory"
    catch {exec svn co $repository/trunk websh-$build}
} else {
    puts "checking out build $build in temporary directory"
    catch {exec svn co $repository/tags/$build websh-$build}
}

if {![file exists websh-$build/README]} {
    cd ..
    fatal "checkout in directory $tmpdir failed"
}

cd websh-$build
## -------------------------------------------------------------
## do some cleanup in the build (just add more fixes or stuff

# remove the examples
file delete -force examples

# make the docs (quickref in html format)
cd doc
file mkdir html
catch {exec make}
cd ..

## end of cleanup
## -------------------------------------------------------------

# creating tar ball for quickref
puts "creating tar ball quickref-$build.tar.gz"
cd doc
file rename html quickref-$build
exec tar czf ../../../quickref-$build.tar.gz --exclude .svn quickref-$build
file rename quickref-$build html
cd ../..

# creating tar ball
puts "creating tar ball websh-$build-src.tar.gz"
exec tar czf ../websh-$build-src.tar.gz --exclude .svn websh-$build


# cleanup
cd ..
file delete -force $tmpdir

puts "done."
