$OpenBSD: patch-support_installation_actions_tcl,v 1.6 2014/10/21 05:10:19 stu Exp $

Retrieve module info from man pages.
Perform shebang adjustments.

--- support/installation/actions.tcl.orig	Fri Aug 29 18:30:12 2014
+++ support/installation/actions.tcl	Tue Sep 16 19:54:45 2014
@@ -90,23 +90,113 @@ proc _trt {module libdir} {
     return
 }
 
-proc _manfile {f format ext docdir} { return }
-proc _man {module format ext docdir} { return }
+proc _manfile {fn dfn} {
+    global modinfos
+    log "Installing $fn to $dfn"
+    set f [open $fn]
+    chan configure $f -buffersize 100000
+    set d [read -nonewline $f]
+    close $f
+    if {[regexp {\n\.SH NAME\n(.*?) \\- (.*?)\n} $d -> shname shtitle] && [regexp -line {^\.TH.*$} $d th]} {
+	lappend modinfos [list $shname [lindex $th 3] [lindex $th 5] $shtitle]
+    }
+    set f [open $dfn w]
+    puts -nonewline $f $d
+    close $f
+    file attributes $dfn -permissions 0o444
+}
 
-proc _exa {module exadir} {
-    global distribution
-    xcopy \
-	    [file join $distribution examples $module] \
-	    [file join $exadir $module] \
-	    1
-    return
+proc _man {module srcdir dstdir} {
+    foreach fn [glob -nocomplain -dir [file join $srcdir $module] *] {
+	if {[file extension $fn] eq ".n"} {
+	    _manfile $fn [file join $dstdir [file tail $fn]]
+	} else {
+	    xcopyfile $fn $dstdir
+	}
+    }
 }
 
-proc _exax {actual module exadir} {
-    global distribution
-    xcopy \
-	    [file join $distribution examples $actual] \
-	    [file join $exadir $module] \
-	    1
+proc _tcm {module libdir} {
+    global distribution config pkgs
+    file mkdir $config(mp)
+    xcopyfile [file join $distribution modules $module $module.tcl] \
+	[file join $config(mp) $module-$pkgs($module).tm]
     return
+}
+
+proc _exafile {fn dfn tclsh} {
+    set f [open $fn]
+    set d [read $f 2]
+    if {$d ne "#!"} {
+	close $f
+	xcopyfile $fn [file dirname $dfn]
+	return
+    }
+    chan configure $f -buffersize 100000
+    append d [read -nonewline $f]
+    close $f
+    log "Installing $fn to $dfn"
+    set execreplace "exec $tclsh "; append execreplace {"$0" ${1+"$@"}}
+    set envreplace "#! $tclsh"
+    set perms 0o444
+    set l [split $d \n]
+    set l2 [string trim [lindex $l 2]]
+    if {
+	[string match {#!*/*} [lindex $l 0]] &&
+	[string match {#*\\} [lindex $l 1]] && 
+	([string match {exec*tclsh*} $l2] ||
+	 [string match {exec*wish*} $l2])
+    } {
+	if {[string match {exec*wish*} $l2]} {
+	    set execreplace [string map {tclsh wish} $execreplace]
+	}
+	lset l 2 $execreplace
+	set perms 0o555
+    } elseif {[string match {#!*/env*tclsh*} [lindex $l 0]]} {
+	lset l 0 $envreplace
+	set perms 0o555
+    }
+    set f [open $dfn w]
+    puts $f [join $l \n]
+    close $f
+    if {$perms == 0o555 &&
+	    "[file tail [file dirname $dfn]]/[file tail $dfn]" in {
+		ftp/hpupdate.tcl ftp/mirror.tcl ftp/newer.tcl
+		ftpd/ftpd ftpd/ftpd.test ftpd/ftpd.unix
+		log/logtofile.tcl
+		mime/maildemo.tcl mbot/personal.tcl mbot/impersonal.tcl
+		nntp/postnews
+    	    }
+    } {
+	set perms 0o444
+    }
+    file attributes $dfn -permissions $perms
+}
+
+proc _exa {module srcdir dstdir tclsh} {
+    _exax $module $module $srcdir $dstdir $tclsh
+}
+
+proc _exax {actual module srcdir dstdir tclsh} {
+    set dstdir [file join $dstdir $module]
+    if {![file exists $dstdir]} {
+	log "Making directory $dstdir"
+	file mkdir $dstdir
+	file attributes $dstdir -permissions 0o755
+    }
+    foreach fn [glob -nocomplain -dir [file join $srcdir $actual] *] {
+	if {[file isdirectory $fn]} {
+	    set m [file tail $fn] 
+	    _exax $m $m [file dirname $fn] $dstdir $tclsh
+	    continue
+	}
+	set t [file tail $fn]
+	set e [file extension $fn]
+	if {$t eq "ChangeLog" || $e in {.orig .beforesubst}} { continue }
+	if {$e in {.tcl ""} || $t in {ftpd.test ftpd.unix}} {
+	    _exafile $fn [file join $dstdir $t] $tclsh
+	} else {
+	    xcopyfile $fn $dstdir
+	}
+    }
 }
