#!/bin/sh
# %Z%$RCSfile: ADUPDATE,v $ version $Revision: #9 $ created $Date: 2001/08/29 $
# %Z%%Q%
#
usage="Usage: `basename $0` source dest application-name

 This script merges the 'source' X11 app-defaults file (adfile) into the
 'dest' adfile. It looks for special begin-end lines that it wraps the
 'source' file with, and removes the old version from the 'dest' adfile. It
 then adds the new version (from 'source') onto the 'dest' adfile."

tmpdir=${S_TMP:-$SHOME/tmp}
src=$1; shift
dest=$1; shift
name=$1; shift

if [ $# -ne 0 ]
then echo "$usage" >&2; exit 1
fi
if [ "X$src" = "X" ] || [ ! -r "$src" ]
then echo "Can't read file: '$src'" >&2; exit 2
fi

template="`dirname $dest`/templates/`basename $dest |
           sed -e 's/OpenLook2/OL2/' -e 's/\(............\).*/\1/'`"
if [ -f $dest ]
then mv $dest $tmpdir/ADUP.$$
elif [ -f $template ]
then cat $template >$tmpdir/ADUP.$$
else > $tmpdir/ADUP.$$
fi

echo "### Updated: `date`" > $tmpdir/ADUP2.$$
cat $src >>$tmpdir/ADUP2.$$

cat $tmpdir/ADUP.$$ 2>/dev/null |
awk 'BEGIN {appname="'$name'"; printing=1;}
{
   if (!printing && $0 ~ /^### *End *resources *for / && $5 == appname) {
      printing = 1;
   }
   if (printing) print;
   if ($0 ~ /^### *Begin *resources *for / && $5 == appname) {
      printing = 0; found = 1;
   }
}
END {
   if (! found) {
      print "### Begin resources for " appname " ###";
      print "### End   resources for " appname " ###";
   }
}' | sed '/^### Begin resources for '$name' /r '$tmpdir/ADUP2.$$ > $dest

rm -f $tmpdir/ADUP*.$$
