mkCTM revision 5014
12886Sphk#!/usr/local/bin/tclsh
22886Sphk
32926Sphkset CTMignoreCVS 0
42926Sphkset CTMapply 1
53833Sphkset CTMdont {^///}
65014Sphkset CTMmail {}
72926Sphk
82886Sphksource $argv
92886Sphk
103833Sphkcd /u1/CTM/SW
113833Sphk
122886Sphkset tmp $CTMtmp
132886Sphkset dd  $CTMdest
142886Sphkset d1  $CTMcopy
152886Sphkset d2  $CTMref
162886Sphkset foo $CTMdate
172886Sphkset foo $CTMprefix
182886Sphkset foo $CTMname
192886Sphk
202886Sphk####
212886Sphk# Find CTM#
222886Sphkfor {set i 0} {1} {incr i} {
232886Sphk    if {[file exists [format "%s/$CTMname.%04d" $dd $i]]} continue
242886Sphk    if {[file exists [format "%s/$CTMname.%04d.gz" $dd $i]]} continue
252886Sphk    break
262886Sphk}
272886Sphkset CTMnbr $i
282886Sphk
295014Sphkset fo [open $d2/.ctm_status w]
305014Sphkputs $fo "$CTMname $CTMnbr"
315014Sphkclose $fo
325014Sphk
332886Sphkputs "Doing CTMname $CTMname CTMnbr $CTMnbr CTMdate $CTMdate"
342886Sphk
355014Sphkexec sh -x -c "rm -f ${tmp}.*" >&@ stdout
365014Sphk
375014Sphkset f1 [open "| ./ctm_scan $d1"]
385014Sphkset f2 [open "| ./ctm_scan $d2"]
395014Sphk
405014Sphkset fo_del   [open $tmp.del w]
415014Sphkset fo_rmdir [open $tmp.rmdir w]
425014Sphkset fo_mkdir [open $tmp.mkdir w]
435014Sphkset fo_files [open $tmp.files w]
445014Sphk# set this to minus one, to compensate for .ctm_status
455014Sphkset changes -1
465014Sphk
472886Sphk#####
482886Sphk# Type Name Mode User Group Barf Size Hash
492886Sphk
502886Sphkproc CTMadd {t n m u g b s h} {
512886Sphk    global fo_files fo_mkdir changes d2
522886Sphk    puts stderr "A $b  $t  $n"
532886Sphk    if {$t == "d"} {
542886Sphk	puts $fo_mkdir "CTMDM $n $u $g $m"
552886Sphk	incr changes
562886Sphk	return
572886Sphk    }
582886Sphk    puts $fo_files "CTMFM $n $u $g $m $h $s"
592886Sphk    flush $fo_files
602886Sphk    exec cat $d2/$n >@ $fo_files
612886Sphk    puts $fo_files ""
622886Sphk    incr changes
632886Sphk    return
642886Sphk}
652886Sphkproc CTMdel {t n m u g b s h} {
662886Sphk    global fo_del fo_rmdir changes
672886Sphk    puts stderr "D $b  $t  $n"
682886Sphk    if {$t == "d"} {
692886Sphk	puts $fo_rmdir "CTMDR $n"
702886Sphk	incr changes
712886Sphk	return
722886Sphk    }
732886Sphk    puts $fo_del "CTMFR $n $h"
742886Sphk    incr changes
752886Sphk    return
762886Sphk}
772886Sphkproc CTMchg {t1 n1 m1 u1 g1 b1 s1 h1 t2 n2 m2 u2 g2 b2 s2 h2} {
782886Sphk    global fo_files d2 d1 changes
792886Sphk    if {$t1 == "d" && $t2 == "d"} {
802886Sphk	return
812886Sphk    }
822886Sphk    if {$t1 == "d" || $t2 == "d"} {
832886Sphk	CTMdel $t1 $n1 $m1 $u1 $g1 $b1 $s1 $h1
842886Sphk	CTMadd $t2 $n2 $m2 $u2 $g2 $b2 $s2 $h2
852886Sphk	return
862886Sphk    }
872886Sphk    if {"x$h1" == "x$h2" && $s1 == $s2} {
882886Sphk	return
892886Sphk        puts stderr "M $b1$b2 $t1$t2 $n1"
902886Sphk	puts $fo_files "CTMFA $n2 $u2 $g2 $m2 $h2"
912886Sphk	incr changes
922886Sphk	return
932886Sphk    }
942948Sphk    if {$b1 == "0" && $b2 == "0"} {
952971Sphk	set i [catch "exec diff -n $d1/$n1 $d2/$n2 > tmp" j]
962926Sphk	set s [file size tmp]
972926Sphk	if {$s < $s2} {
983112Sphk	    puts stderr "E $b1$b2 $t1$t2 $n1"
992971Sphk	    puts $fo_files "CTMFN $n1 $u2 $g2 $m2 $h1 $h2 $s"
1002926Sphk	    flush $fo_files
1012926Sphk	    exec cat tmp >@ $fo_files
1022926Sphk	    puts $fo_files ""
1032926Sphk	    incr changes
1042926Sphk	    return
1052926Sphk	}
1062886Sphk    }
1072926Sphk    puts stderr "R $b1$b2 $t1$t2 $n1"
1082926Sphk    puts $fo_files "CTMFS $n2 $u2 $g2 $m2 $h1 $h2 $s2"
1092886Sphk    flush $fo_files
1102926Sphk    exec cat $d2/$n2 >@ $fo_files
1112886Sphk    puts $fo_files ""
1122886Sphk    incr changes
1132926Sphk    return
1142886Sphk}
1152886Sphk#####
1162886Sphkset l1 ""
1172886Sphkset l2 ""
1182886Sphk
1192886Sphkwhile 1 {
1202886Sphk
1212886Sphk    if {$l1 == ""} {gets $f1 l1}
1222886Sphk
1232886Sphk    if {$l2 == ""} {gets $f2 l2}
1242886Sphk    
1252886Sphk    if {$l1 == "" && $l2 == ""} break
1262886Sphk
1272886Sphk    set n1 [lindex $l1 1]
1282886Sphk    set n2 [lindex $l2 1]
1292886Sphk
1303833Sphk    #if {[regexp $CTMdont /$n1]} { set l1 "" ; continue }
1313833Sphk    if {[regexp $CTMdont /$n2]} { set l2 "" ; continue }
1323833Sphk
1335014Sphk    # they're all the same...
1342886Sphk    if {$l1 == $l2}  { set l1 "" ; set l2 "" ; continue }
1352886Sphk
1362926Sphk    if {$CTMignoreCVS } {
1372926Sphk	if {[regexp {/CVS/} $l1]} {set l1 ""; continue }
1382926Sphk	if {[regexp {/CVS/} $l2]} {set l2 ""; continue }
1392926Sphk    }
1402926Sphk
1412886Sphk    if {$l1 == "" }   { eval CTMadd $l2 ; set l2 "" ; continue }
1422886Sphk
1432886Sphk    if {$l2 == "" }   { eval CTMdel $l1 ; set l1 "" ; continue }
1442886Sphk
1455014Sphk    # if the name is the same we're safe...
1465014Sphk    if {$n1 == $n2}  { eval CTMchg $l1 $l2 ; set l1 "" ; set l2 "" ; continue }
1475014Sphk
1485014Sphk    # To avoid this anomaly:
1495014Sphk    # A -  d  src/gnu/lib/libreadline/readline/Attic
1505014Sphk    # A 0  f  src/gnu/lib/libreadline/readline/Attic/readline.h,v
1515014Sphk    # A 0  f  src/gnu/lib/libreadline/readline.c,v
1525014Sphk    # D 0  f  src/gnu/lib/libreadline/readline/readline.h,v
1535014Sphk    # D 0  f  src/gnu/lib/libreadline/readline.c,v
1545014Sphk    # we have to make things somewhat complicated...
1555014Sphk
1565014Sphk    # if they have the same number of components...
1575014Sphk    set ll1 [llength [split $n1 /]]
1585014Sphk    set ll2 [llength [split $n2 /]]
1595014Sphk    if {$ll1 == $ll2} {
1605014Sphk	if {$n1 < $n2 } { 
1615014Sphk	    eval CTMdel $l1 ; set l1 "" ; continue 
1625014Sphk	} else {
1635014Sphk	    eval CTMadd $l2 ; set l2 "" ; continue 
1645014Sphk	}
1655014Sphk    } 
1665014Sphk    if {$ll1 < $ll2} {
1675014Sphk	    puts "<$ll1> < <$ll2>\n  <$l1>\n  <$l2>"
1685014Sphk	    eval CTMadd $l2 ; set l2 "" ; continue 
1695014Sphk    } else {
1705014Sphk	    puts "<$ll1> > <$ll2>\n  <$l1>\n  <$l2>"
1715014Sphk	    eval CTMdel $l1 ; set l1 "" ; continue 
1725014Sphk    }
1735014Sphk
1742886Sphk    
1752886Sphk
1762886Sphk}
1772886Sphk
1782886Sphkclose $fo_del
1792886Sphkclose $fo_rmdir
1802886Sphkclose $fo_mkdir
1812886Sphkclose $fo_files
1822886Sphk
1832886Sphkexec echo CTM_BEGIN 2.0 $CTMname $CTMnbr $CTMdate $CTMprefix > $tmp.begin
1842886Sphkexec echo -n "CTM_END " >> $tmp.end
1852926Sphkset m [exec cat $tmp.begin $tmp.del $tmp.rmdir $tmp.mkdir $tmp.files $tmp.end | /sbin/md5]
1862886Sphkexec echo "$m" >> $tmp.end
1872886Sphk
1882886Sphkif {!$changes} {
1892886Sphk    puts "no changes"
1903112Sphk    exec sh -c "rm -f ${tmp}.*"
1912886Sphk    exit 0
1922886Sphk}
1933112Sphkflush stdout
1943833Sphkset  nm [format "%s.%04d" $CTMname $CTMnbr]
1952886Sphk
1963112Sphkexec cat $tmp.begin $tmp.del $tmp.rmdir $tmp.mkdir $tmp.files $tmp.end \
1973833Sphk	| gzip -9 > ${tmp}:${nm}.gz 
1983112Sphk
1993112Sphkexec sh -x -c "rm -f ${tmp}.*" >&@ stdout
2003112Sphk
2012926Sphkif {$CTMapply} {
2023833Sphk	exec sh -e -x -c "cd $CTMcopy ; /u1/CTM/SW/ctm -v -v -v ${tmp}:${nm}.gz" >&@ stdout
2032926Sphk}
2043833Sphkexec mv ${tmp}:${nm}.gz $dd/.tmp_${nm}.gz >&@ stdout
2053833Sphkexec mv $dd/.tmp_${nm}.gz $dd/${nm}.gz >&@ stdout
2065014Sphk
2075014Sphkif {$CTMmail != ""} {
2085014Sphk    exec /u1/phk/ctm_smail/ctm_smail -m 50000 -c 300000 $dd/${nm}.gz $CTMmail >&@ stdout
2095014Sphk}
210