#!/bin/sh
#\
exec tclsh $0 "$@"
########################################################################
#
# Trivial installer. Run
#   ./install -h
# for more information.
#
# (C) 2002 Harald Kirsch
# $Revision: 1.2 $, $Date: 2002/05/05 15:24:04 $
#
########################################################################

set auto_path [concat [list .] $auto_path]
package require xmlgen

set SUB xmlgen-$::xmlgen::VERSION

########################################################################
proc patchFile {from to args} {
  set in [open $from]; set text [read $in]; close $in
  foreach {pat rep} $args {
    if {![regsub -all $pat $text $rep text]} {
      puts stderr "could not apply patch `$pat'->`$rep' to `$from'"
      exit 1
    }
  }
  set out [open $to w]
  puts -nonewline $out $text
  close $out
}
########################################################################
proc usage {} {
  set P [file tail $::argv0]
  set msg \
{usage: $P -prefix dir \[-cgi cgidir\]  \[-doc docdir\] \[-n\]
     copies files to destination directories. 
   dir: should be a directory normally searched by Tcl for 
        library files (check TCLLIBPATH and auto_path). Within
        this directory, a subdirectory called [file join lib $::SUB]
        will be created.
   cgidir: The documentation is also available as cgi-scripts written
        with htmlgen. If given, the scripts are copied into a 
        subdirectory of cgidir called $::SUB.
   docdir: The HTML documentation is copied into a subdirectory
        of docdir called $::SUB. If no docdir is given, the docs
        are copied into [file join dir doc $::SUB].
    -n: Don't actually copy, just show what would be done.
  }
  puts [subst $msg]
  exit 1
}
########################################################################
proc getParam {opt argv i} {
  if {$i>=[llength $argv]} {
    puts stderr "missing parameter for option `$opt'"
    exit 1
  }
  return [lindex $argv $i]
}
########################################################################

set doit 1
for {set i 0} {$i<$argc} {incr i} {
  set opt [lindex $argv $i]
  switch -- $opt {
    -prefix {
      incr i
      set prefix [getParam $opt $argv $i]
    }
    -doc {
      incr i
      set docdst [getParam $opt $argv $i]
    }
    -cgi {
      incr i
      set cgidst [getParam $opt $argv $i]
    }
    -n {
      set doit 0
    }
    default {
      puts "unknown option `$opt'"
      usage
    }
  }
}

if {![info exist prefix]} {
  usage
}
set pkgdir [file join $prefix lib $SUB]
if {[info exist docdst]} {
  set docdir [file join $docdst doc $SUB]
} else {
  set docdir [file join $prefix doc $SUB]
}

## copy package files
puts "Copying package files to $pkgdir"
if {$doit} {
  file mkdir $pkgdir
  file copy -force \
      pkgIndex.tcl xmlgen.tcl htmlgen.tcl sidenav.tcl tab.tcl \
      $pkgdir
}

## copy documentation
puts "Copying HTML manual to $docdir"
if {$doit} {
  file mkdir $docdir
  eval file copy -force \
      [glob doc/*.html] \
      $docdir
}


## copy cgi stuff
if {[info exist cgidst]} {
  set cgidir [file join $cgidst $SUB]
  puts "Copying cgi script for navigable manual to $cgidir"
  if {$doit} {
    file mkdir $cgidir
    eval file copy -force \
	doc/overview doc/xmlgen doc/htmlgen doc/sidenav doc/common.tcl \
	$cgidir
    set INDEX [file join $cgidir index ]
    patchFile doc/index $INDEX \
	{set XMLGEN [^ \n]+} "set XMLGEN $prefix"
    if {"$::tcl_platform(platform)"=="unix"} {
      file attributes $INDEX -permission 0755
    }
  }
}
########################################################################

### Local Variables: ###
### mode: tcl ###
### End: ###
