#!/bin/sh
#
# gen-rename shell script.  See below for usage.
#
# This script generates star renaming scripts from an input configuration file
# This script is not robust.  Check the results carefully.
#
# Author: Kennard White
# Version: @(#)gen-rename	1.1 12/14/92
#

SELF=`basename $0`

if [ "$1" = "" -o "$2" = "" ] ; then
    cat <<EOF
usage: $SELF domain config_file
	domain should be in all upper case
	config_file should end in .conf
	Two files will be generated:
	  .move file:	run this in the toplevel of the domain and
		     	will move all .pl, SCCS, and icon files.
	  .fixref file: script may be run in any number of dirs
			to fix all references to stars
			renamed by the config file.
EOF
    exit 1
fi

awkcmdfile=/tmp/awkcmd$$
domain=$1
domainpath=`echo $domain | tr A-Z a-z`
conffile=$2
movefile=`dirname $conffile`/`basename $conffile .conf`.move
fixreffile=`dirname $conffile`/`basename $conffile .conf`.fixref
rm -f $movefile $fixreffile

cp /dev/null $movefile
cat >> $movefile <<\EOF
#! /bin/sh
#
###################  DO NOT EDIT  ##########################
# This shell script was automatically generated.
###################  DO NOT EDIT  ###########################
#
PTOLEMY=${PTOLEMY-`csh -c 'echo ~ptolemy'`}; export PTOLEMY
PATH=$PTOLEMY/octtools/bin.$ARCH:$PATH; export PATH
EOF

cat >> $movefile <<EOF
domain=$domain
domainpath=$domainpath
# This file was auto-generated by `whoami` on `date`.
# The script $SELF generated this file from config file $conffile
EOF

cp $movefile $fixreffile

cat >> $movefile <<\EOF
# Shell script to move ptlang files and OCT icon facets.
# This should be invoked in the top-level directory for the domain.
EOF

cat >> $fixreffile <<\EOF
# Shell script to fix references.  Invoke this in any directory.
# All ptlang files and octfacets in the directory will be patched as needed.
recurflag=false
if [ "$1" = "-R" ] ; then
    recurflag=true
    shift
fi
docell=
if [ "$1" = "-cell" ] ; then
    shift
    docell=$1
    shift
    if [ ! -d $docell -o ! -d $docell/schematic ] ; then
	echo "$SELF: $docell is not a valid OCT cell"
	exit 1
    fi
fi
if [ -n "$1" ] ; then
    if [ -d "$1" ]; then
	chdir $1
    else
	echo "$SELF: $1 is not directory."
	exit 1
    fi
fi
if [ $recurflag = true ] ; then
    exec find . -type d -exec $0 {} \;
    exit 0
fi
if [ -d stars -a -d icons ] ; then
    for dir in stars icons demo demos; do
        if [ -d $dir ] ; then $0 $dir; fi
    done
    exit 0
fi
EOF



########################################################################
#
#	This section generates the header section of the move file.
#
########################################################################

cat >> $movefile <<\EOF
if [ ! -d icons -o ! -d stars ] ; then
    echo "Missing the icons and/or stars directory."
    echo "Invoke this script in the toplevel directory for the domain."
    exit 0
fi
EOF

########################################################################
#
#	This section generates a section of the move file
#	that renames the appropriate .pl, SCCS, and icon files.
#	Renaming the SCCS files can be tricky.
#
########################################################################



cat >> $movefile <<\EOF
moveprog=/tmp/moveprog$$
cat - > $moveprog <<\BEOF
# This script segment assumes that src,tgt,tgtdir have already
# been defined.
tgtdir=${tgtdir:=.}
srcbase=${domain}$src; srcpl=$srcbase.pl
tgtbase=${domain}$tgt; tgtpl=$tgtbase.pl
if [ -n "$tgtdir" ] ; then
    mkdir $tgtdir $tgtdir/stars $tgtdir/stars/SCCS $tgtdir/icons 2> /dev/null
fi
if [ ! -f stars/$srcpl ] ; then
    echo Warning: ptlang $srcpl missing
else
    echo moving stars/$srcpl '-->' $tgtdir/stars/$tgtpl
    mv -i stars/$srcpl $tgtdir/stars/$tgtpl
    if [ -f stars/SCCS/s.$srcpl ] ; then
	mv -i stars/SCCS/s.$srcpl $tgtdir/stars/SCCS/s.$tgtpl
    fi
    if [ -f stars/SCCS/p.$srcpl ] ; then
	mv -i stars/SCCS/p.$srcpl $tgtdir/stars/SCCS/p.$tgtpl
    fi
    rm -f stars/$srcbase.cc stars/$srcbase.h
fi
for icon in icons/$src icons/$src.*; do
    if [ ! -d "$icon" ] ; then continue ; fi
    newname=`echo $icon | sed -e "s+/$src+/$tgt+"`
    if [ -f $tgtdir/$newname -o -d $tgtdir/$newname ] ; then
	echo Warning: icon $tgtdir/$newname already exists...skipping
    else
    	echo moving icon $icon '-->' $tgtdir/$newname
    	mv -i $icon $tgtdir/$newname
    fi
done
BEOF

EOF

# $1 in the script is the domain (in caps)
# for the icons, need to watch out for the .multiport=# syntax
cat - > $awkcmdfile <<\EOF
{
    printf "src=%s; tgt=%s; tgtdir=%s; . $moveprog\n", $1, $2, $3

}
EOF

awk -f $awkcmdfile < $conffile	>> $movefile

cat >> $movefile <<\EOF
rm -f $moveprog
EOF


########################################################################
#
#	This section generates a section of the fixref file
#	that fixes up references in the "derivedFrom and "seealso"
#	fields of the pl files.  First we generate the sed script
#	inline in the fixref file, and then have the fixref script
#	go through and run this on every pl file.
#
########################################################################

sed -e "s/DOMAIN/$domain/g" > $awkcmdfile <<\EOF
{
    printf "/name/		s/\<%s\>/%s/\n", $1, $2
    printf "/derivedFrom/	s/\<%s\>/%s/\n", $1, $2
    printf "/seealso/		s/\<%s\>/%s/\n", $1, $2
    printf "/\<%s\>[ 	]::/	s/\<%s\>/%s/\n", $1, $1, $2
    printf "s/\<DOMAIN%s\.pl\>/DOMAIN%s.pl/\n", $1, $2
}
EOF

cat - >> $fixreffile <<\EOF
sedcmdfile=/tmp/sedcmd$$
cat - > $sedcmdfile <<\BEOF
EOF

awk -f $awkcmdfile < $conffile	>> $fixreffile
echo "BEOF"			>> $fixreffile

cat - >> $fixreffile <<\EOF
for i in *.pl make.template; do
    if [ ! -f $i ] ; then
	continue
    fi
    if [ ! -r $i ] ; then
	echo "Skipping $i (not readable)"
	continue
    fi
    sed -f $sedcmdfile < $i > $i.n
    if diff $i $i.n > /dev/null ; then
	rm -f $i.n
	echo $i: No changed required
    else
	if [ ! -w $i ] ; then
	    if [ -d SCCS ] ; then
	        sccs edit $i
	    fi
	    if [ ! -w $i ] ; then
		echo $i: is not writable: cant fix references
		continue
	    fi
	    echo $i: SCCS checked out in order to fix references
	    # Do sed over again on the checked out file
            sed -f $sedcmdfile < $i > $i.n
	fi
	echo $i: Fixing references
	mv $i $i.orig
	mv $i.n $i
    fi
done
rm -f $sedcmdfile
EOF

########################################################################
#
#	This section generates a section of the fixref file
#	that fixes up references in the OCT database of
#	schematics (galaxies and palettes).
#	We generate an inline script of octmvlib commands,
#	then go through all the schematics invoking octmvlib
#	The octmvlib script is generated using awk.
#
########################################################################


cat - > $awkcmdfile <<\EOF
{
    if ( $3 != "" ) { tgtpath = $3"/" } else { tgtpath = "" }
    printf "octmvlib -O \"$1/icons/%s\" -N \"$2/%sicons/%s\" $3\n", $1, tgtpath, $2
}
EOF


cat - >> $fixreffile <<\EOF
octcmdfile=/tmp/octcmd$$
cat - > $octcmdfile <<\BEOF
EOF
awk -f $awkcmdfile < $conffile				>> $fixreffile
echo "BEOF"						>> $fixreffile

cat - >> $fixreffile <<\EOF
dirname=`pwd`
basedirname=`basename $dirname`


octprsfile=/tmp/octprs$$
cat - > $octprsfile <<\BEOF
BEGIN {
    totsub = 0
}
$3 == "substitutions." {
     totsub += $2
}
$3 != "substitutions." {
     print $0
}
END {
     if ( totsub != 0 ) {
         printf "\t\t%d substitutions\n", totsub
     }
}
BEOF

if [ -n "$docell" ] ; then	celllist=$docell
else				celllist=*	; fi
for i in $celllist; do
    facet=$i/schematic/contents\;
    if [ ! -d $i -o ! -d $i/schematic -o ! -f $facet ] ; then
	continue
    fi
    if [ $basedirname = icons -a `expr $i : '[A-Z]'` != 0 ] ; then
	echo $facet: Skipping: should be leaf node
    else
	if [ ! -w $facet ] ; then
	    echo $facet: Missing write permision
	    continue
	fi
	echo Fixing OCT $i:schematic:contents
	usrpth='~ptolemy'/src/domains/$domainpath
	varpth='$PTOLEMY'/src/domains/$domainpath
	octf=$i:schematic:contents
	sh $octcmdfile $usrpth $varpth		$octf | awk -f $octprsfile
	sh $octcmdfile $varpth $varpth		$octf | awk -f $octprsfile
	octmvlib -O '~ptolemy' -N '$PTOLEMY'	$octf | awk -f $octprsfile
    fi
done
rm -f $octcmdfile $octprsfile
EOF

chmod 555 $movefile $fixreffile
rm -f $awkcmdfile
