#!/bin/ksh
## local ksh script to help maintain klone

## very big tmpdir, to find place...
TMP=~/t

RCSDIFFH=1		# to make ci use diff -h
export RCSDIFFH

## funcs defs

rcs_r2v() {
    grep "^$1[ 	]" klone.shar,n |(read Version Revision;
        echo $Revision
	)
}

create_sub_files() {
    if test $2/FILES -nt $1
    then for i in `cat $2/FILES`;do echo $2/$i;done > $1
    fi
}


if test $# = 0 -o "$1" = -?
then echo "USAGE: klone-u ARGUMENTS:";
     grep '^[^ 	].*[^(][)]' $0
exit 1
fi

set -x

COMMAND=$1;shift

case $COMMAND in

fix ) # fix dependencies
    make depend
    cp klparser.lex klparser.l; cp klparser.yac klparser.y
    ifdef-expand -u ISM -d KLONE -u KLEXO -u WLPROFILER -d LOCATORS -u KLCRYPT \
                 -d UNSIGNED \
	klparser.l klparser.y
    (cd kl; klone-update)
    (cd ../sun4; make kllex.yy.c kly.tab.c y.tab.h)
    (cd amiga; for i in  kllex.yy.c kly.tab.c y.tab.h;do cp $i-sun4 $i;done
        TERM=vt100; ex kllex.yy.c << \EOF
/^FILE \*yyin
s/^.*$/FILE *yyin, *yyout;/
w
q
EOF
)
    ;;

add-file) # files...
#       ) # adds files to the brother compilation dirs
	  for file in $*; do
	      for i in ../*/SRC;do ln -s SRC/$file ${i%/SRC}/$file;done
	  done
    ;;

## RCS ARCHIVING

ci|CI) # check-in a new version by RCS
    set +x
    if test ! -s Log ; then echo "please make a Log file!";exit 1;fi
    set -x
    klone-u fix
    cp Makefile Makefile.distrib
    rm -f klone.shar
##    create_sub_files FILES_DOC doc
    if cat FILES FILES_KIT| fshar >klone.shar;then :
    else echo ci aborted;exit 1
    fi
    if ci -u$1 klone.shar < Log
    then >Log; funshar REVISIONS klversion.h  < klone.shar
         echo "Unsharing done"
	 if test "$COMMAND" = CI;then cp klone.shar $FTP/pub/klone;fi
    else echo ci aborted
    fi
    ;;

co) # version-name
# ) # check-out a version by RCS
    REV1=$1
    RCSREV1=`rcs_r2v $1`
    if test -z "${RCSREV1}"
	then echo "USAGE: klone-u co Revision";exit
    fi
    co -r${RCSREV1} klone.shar
    ;;

import ) # import from genitor directory
    if test ! -s ~/Wool2/src/Log ; then (cd ~/Wool2/src; wool-u ci);fi
    set -e
    (cd KlonePrivate;for i in *;do if test -f ../$i;then cp ../$i .;fi;done)
    ifdef-expand -u ISM -d KLONE -u KLEXO -u WLPROFILER -d LOCATORS -u WLCRYPT \
	-e wl/wool-ref.txt -e wl_exo.c -e wl_exo.h -e Make.TEMPLATE \
        -e wlprofiler.c -e wlprofiler.h -e REVISIONS -e wlversion.h \
	-e README -e wl/pretty.wl -e amiga/Makefile \
        -e wool-u -e wlformat.c < ~/Wool2/src/wool.shar \
    | w2k | funshar -u 
    replace-strings -e '("klprofiler[.][cho]\n?" "")' \
        -e '("klformat[.][cho]\n?" "")' -e '("kl_exo[.][cho]\n?" "")' \
        FILES Makefile
    replace-strings -e '("(\n:[^\n]*)\n" "")' Makefile
    cat KlonePrivate/FILES_KLONE >> FILES
    cat KlonePrivate/FILES_KIT_KLONE >> FILES_KIT
    make depend;     cp Makefile Makefile.distrib
    (cd kl; wool-update)
    echo .>Log
    klone-u ci
    ;;

import-file ) # files...
    (cd ~/Wool2/src; ls -1 $* | fshar > ~/t/woolpartial.shar)
    set -e
    ifdef-expand -u ISM -d KLONE -u KLEXO -u WLPROFILER -d LOCATORS -u WLCRYPT \
	-e wl/wool-ref.txt -e wl_exo.c -e wl_exo.h -e Make.TEMPLATE \
        -e wlprofiler.c -e wlprofiler.h -e REVISIONS -e wlversion.h \
	-e README -e wl/pretty.wl -e amiga/Makefile \
        -e wool-u -e wlformat.c < ~/t/woolpartial.shar \
    | w2k | funshar -u 
    ;;

shar ) # generates a work version for my home in ~/t
    INTREV=`grep '^["][$]Id: klone.shar,v ' klone.shar|\
		 (read A B C D E;echo ${C##*.})`  
    gzip -v9 < klone.shar > ~/t/klone${INTREV#[0-9].}.gz
    ls -l ~/t/klone${INTREV#[0-9].}.gz
    ;;

dist ) # generates a public Klone version in $FTP/pub/Klone
     set -e
     INTREV=`grep '^["][$]Id: klone.shar,v ' klone.shar|\
		 (read A B C D E;echo ${C##*.})`  
     cp FILES FILES_KIT Makefile Dist
     cd Dist
     add-remove-filenames \
        -e CHANGES.Wool \
	-e klwppb.c -e klwppb.h -e kl/preparse.kl -e scripts/wool-preparse \
        -e kl/wool.wdd -e scripts/preparse-check -e kl/wppb.kl \
	-e scripts/wdd-make \
	-e klparser.yac -e klparser.lex \
	-e kl_exo.c -e kl_exo.h -e klformat.c -e doc/FastParser.txt \
        -e klprofiler.c -e klprofiler.h -e REVISIONS -e TODO \
        FILES FILES_KIT Makefile
     ifdef-expand -u ISM -d KLONE -u KLEXO -u KLPROFILER -d LOCATORS -u KLCRYPT \
	-u OLD_KLONE_PACKAGE -u NO_BYTE_STRINGS -u DO_BUS_ERROR -u UNSIGNED \
	-u NO_SELECT -u OLD_ERROR_ON_NO_NEXT_METHOD -u OLD_MAPHASH \
	-u OBSOLETE -u EVALHOOK -u OLD_POSITION -u OLD_GET_DEFAULT \
	-u BOSF -u COMMENT -u DONT_EXECUTE_NUMBERS -u WPPB \
        -u OLD_ATOM_EQUAL -u UNBUFFERED_SYSTEM \
	-e CHANGES.Wool \
	-e klwppb.c -e klwppb.h  -e kl/preparse.kl -e scripts/wool-preparse \
        -e kl/wool.wdd -e scripts/preparse-check -e kl/wppb.kl \
	-e scripts/wdd-make \
	-e klparser.yac -e klparser.lex \
	-e kl_exo.c -e kl_exo.h -e klformat.c  -e doc/FastParser.txt \
        -e klprofiler.c -e klprofiler.h -e REVISIONS -e TODO \
	-e FILES -e FILES_KIT -e Makefile \
	-i FILES -i FILES_KIT -i Makefile \
	< ../klone.shar | funshar
    rm -f $FTP/pub/Klone/Klone[0-9]*[0-9].tar.gz
    tar cf - . | gzip -v9 > $FTP/pub/Klone/Klone$INTREV.tar.gz
    ls -l $FTP/pub/Klone/Klone$INTREV.tar.gz
    cd ..
    rm -rf Dist/*
    ;;


export ) # export current klone.shar to Wool2
    echo "Warning: did you update CHANGES.Wool and Wool/wlversion.h?"
    set -e
    INTREV=`grep '^["][$]Id: klone.shar,v ' klone.shar|\
		 (read A B C D E;echo ${C##*.})`  
    cd Wool
    cp ../FILES ../FILES_KIT .
    add-remove-filenames \
	-e CHANGES.Klone \
        -e kl/klone-ref.txt -e REVISIONS -e klversion.h -e README \
	-e kl/pretty.kl -e amiga/Makefile -e klone-u -e klone_util \
	-e kl/poor-clos.kl -e Makefile -e FILES -e FILES_KIT \
	-e KloneLight -e KloneLight/KlLib.c -e KloneLight/KlLib.h \
        -e klparser.y -e klparser.l \
	-e klparser.yac -e klparser.lex \
        -e klko.c -e klko.h -e TODO -e kl/poor-clos.kl \
	-i Makefile -i README -i FILES -i FILES_KIT -i wlversion.h \
	-i wl/exo-completions.wl -i klparser.l -i klparser.y -i wool_util \
	FILES FILES_KIT 
    ed FILES_KIT << \EOF
1,$s|^Configs[/]|Make/Make.|
1,$s/^Configs$/Make/
w
q
EOF
    k2w FILES FILES_KIT 
    cp ../klparser.lex klparser.l; cp ../klparser.yac klparser.y
    ifdef-expand -u KLONE -d KLEXO -d UNSIGNED -d LOCATORS klparser.l klparser.y
    cat ../klone.shar | ifdef-expand -u KLONE -d KLEXO -d UNSIGNED -d LOCATORS \
	-e CHANGES.Klone \
        -e kl/klone-ref.txt -e REVISIONS -e klversion.h -e README \
	-e kl/pretty.kl -e amiga/Makefile -e klone-u -e klone_util \
	-e kl/poor-clos.kl -e Makefile -e FILES -e FILES_KIT \
        -e klparser.y -e klparser.l \
	-e klparser.yac -e klparser.lex \
	-e klstartup.kl -i wlstartup.wl \
        -e klko.c -e klko.h -e TODO  -e kl/poor-clos.kl \
	-i Makefile -i README -i FILES -i FILES_KIT -i wlversion.h \
	-i wl/exo-completions.wl -i klparser.l -i klparser.y -i wool_util \
	-e FILES -e FILES_KIT -e Makefile -i FILES -i FILES_KIT -i Makefile \
    | k2w | (cd ~/Wool2/src; funshar -u)
    cd ~/Wool2/src
    make depend
    (cd wl; klone wool-update)
    echo "[$INTREV]">> Log
    wool-u ci
    grep Kl wool.shar
    ;;

export-file ) # files...
#           ) # exports files to Wool2 directory
    (for i in $*; do echo $i; done) | fshar | \
    ifdef-expand -u KLONE -d KLEXO -u LOCATORS -d UNSIGNED \
		 | k2w | (cd ~/Wool2/src; funshar -u -v)
    ;;

inst)

## system_name machine 
inst_bin () {
    if test ! -d ../${1};then klone-u mksdir ${1};fi
    rsh ${2} "PATH=`cat ~/bin/${1}/.path`
    cd ~/Klone/${1};nice -40 make;
    cp klone /net/koala/packages/klone/${1}/klone;
    " </dev/null  2>&1 | prefix_output ${1}
}
prefix_output () {
    while read L;do echo "$1: $L";done
}

    (
##    inst_bin sun3 falbala &
    inst_bin sun4 indri &
    inst_bin sony avahi &
    inst_bin mips aye &
    inst_bin dec rataxes &
    inst_bin sgi entropy &
    inst_bin aix viso &
    inst_bin solaris taquilee&
    inst_bin alpha ganesa&

## purify version
##    if test ! -d ../sun4p;then klone-u mksdir sun4p;fi
##    rsh indri "PATH=`cat ~/bin/sun4/.path`
##    PURIFYHOME=/net/lib/purify
##    PURIFYCACHEDIR=~/t
##    PATH=$PATH:$PURIFYHOME
##    LM_LICENSE_FILE=$PURIFYHOME/license.dat
##    cd ~/Klone/sun4p; nice -40 make klonepf
##    cp libklone.a /net/koala/lib/klone/libklone_pure.a
##    ranlib /net/koala/lib/klone/libklone_pure.a" </dev/null &

    wait

    cd kl;../scripts/klone-update
    cp -pr . /net/koala/packages/klone/kl
    cd ..
##    cp *.h /net/koala/include/klone
##    cat FILES|fshar|(cd /net/koala/src/klone;funshar)
    echo "*************** Installation complete, log available in ~/t/LOG"
    ) 2>&1 | tee ~/t/LOG
    ;;

test) echo ok;;

mksdir)		# mksdir dirnames
    if test -z "$1";then echo "USAGE: klone-u mksdir new-dir";exit 1;fi
    for file  in $*; do
    case "$file" in 
         */*)DIR="$file";;
         *)DIR=../$file;;
    esac
    file="${DIR##*/}"
    mkdir $DIR
    curdir=`pwd`
    case $curdir in
	 /[0-9]/* ) curdir="/u/$machine$curdir";;
	 /auto/* ) curdir="${curdir#/auto}";;
    esac
    if test "$DIR" = "../$file"; then ln -s ../src $DIR/SRC
       else ln -s $curdir $DIR/SRC
    fi
    pwd >$DIR/.rootdir
    echo  $file>$DIR/.dir
    if test ! -f Configs/$file
    then cp Configs/TEMPLATE Configs/$file
    fi
    ln -s SRC/Configs/$file $DIR/Config.$file
    for i in `cat FILES` tests tests/test.kl tests/test-funcs.kl; do
        if test -d "$i"
	   then mkdir "$DIR/$i"; ln -s ../SRC "$DIR/$i/SRC"
	   else ln -s "SRC/$i" "$DIR/$i"
	fi
    done
    echo "Now, go in $DIR and edit Config.$file"
    done
    ;;

esac
