#! /bin/sh
# make-util :  This script is an auxilary part of makefiles
#              it's primary aim is to do make-back - delete 
#              all files generated by previous make invocation.
# 
# This file is a part of GNU SQL Server
#
# Copyright (c) 1996, Free Software Foundation, Inc
# Developed at the Institute of System Programming
# This file is written by Michael Kimelman
# 
# Contacts: gss@ispras.ru
#


# echo "$0: $* at \"`pwd`\" "
# distribution source tree ------------------------
srcdir=`echo $0 | sed 's/make-util$//g;s/etc[/]$//g;s/[/]$//g' `
if [ x$srcdir = x ]; then 
  srcdir="."
fi
localtree=`pwd`
cd $srcdir
srcdir=`pwd`
cd $localtree
# -------------------------------------------------

# set -x

case $1 in
  install)
      ## install cleanning subdirectories from the deepest level to '.'
LN_S="$2"
dbhome="$3"
bindir="$4"
mandir="$5"
infodir="$6"
prefix="$7"
#    put link to clients tools (compiler, runtime, mans, infos etc)
#    into commonly used places
link_it ()  ## what to
{
  if [ -h $2/$1 ]; then
    rm -f $2/$1
  fi
  ${LN_S} `pwd`/$1 $2/$1
}
      ( 
        cd ${dbhome}/client;
        for f in `ls` ; do
          if [ -x $f ]; then
            link_it $f ${bindir}
          fi 
        done
        for f in `ls *.a` ; do
          link_it $f ${prefix}/lib
        done
      )
      link_it ${dbhome}/client ${prefix}/include/gnusql
      
      ( cd ${dbhome}/doc
        for f in `ls gss*` ; do
          link_it $f ${infodir}
        done
        if [ -f ${infodir}/dir ]; then ## add it to the list
          sed "/^.*gss[.]info.*$/d" ${infodir}/dir >$$
          rm -f ${infodir}/dir
          cat $$ - >${infodir}/dir <<EOF
* GNU SQL server: (gss.info).   GNU SQL Server documentation 
EOF
          rm -f $$
        fi
        for f in `ls *.1` ; do
          link_it $f ${mandir}/man1
        done
      )
     ;;
  clean)
      shift
      if  [ "x$1" = x ] ; then
        MAKECLEAN="make clean"
        echo "MAKECLEAN guessed to \'$MAKECLEAN\'"
      else
        MAKECLEAN="$*"
      fi
      ## cleanning subdirectories from the deepest level to '.'
      for dir in `find . -type d -depth -print ` ; do
        if [ $dir = . ]; then continue; fi
        if [ -r $dir/Makefile -o -r $dir/makefile ]; then
          ( cd $dir;  ${MAKECLEAN}  );  ## it can invoke that code recursively
        else
          ( cd $dir; $srcdir/etc/make-util clean "${MAKECLEAN}" );
        fi
        if [ -h $dir/Makefile.in ]; then
          rm -f $dir/Makefile.in
        fi
        # it can be directory comletely generated from RCS.
        # let's check it
        fc=`ls  -1 $dir | wc -l`
        if [ -h $dir/RCS ]; then
          if [ -f $dir/Makefile ]; then
            if [ $fc -eq 3 -a -f $dir/Makefile_h ]; then
              rm -f $dir/Makefile_h
              fc=`ls  -1 $dir | wc -l`
            fi
            if [ $fc -eq 2 ]; then
              rm -f $dir/Makefile
              fc=`ls  -1 $dir | wc -l`
            fi
          fi
          if [ $fc -eq 1 ]; then
            echo "$dir removing..."
            rm -f $dir/RCS
            fc=`ls  -1 $dir | wc -l`
          fi
        fi
        if [ $fc -eq 0 ]; then
          rmdir $dir
        fi
      done
      ## cleanning in current directory
      ## clean garbage
      rm -f *~ \#* core *.bak *.BAK *% *.out TAGS *.__ log tmp *.flc .??*
      rm -f aaa aa qqq qq xx xxx zzz zz
      ## clean gss libraries timestamps
      rm -f libgss* *stamp *.a *.o *.exe
      ## clean dependencies
      rm_list=""
      flist=`ls -1 | sed 's/^\(.*\)[.]\([^.]*\)$/\1 \2/g;s/^\([^ .]*\)$/\1 ./g'`

check_deps () ###
{
  if [ "$1" = "$fext" ]; then
    for ext in $2 ; do
      if [ -f $fn$ext ]; then 
        rm_list="$rm_list $fn$ext"
      fi
    done
  fi
}
      while [ "x$flist" != x ]; do
        set $flist ; fn=$1 ; fext=$2 ; shift 2; flist="$*"
        check_deps "k"  ' .c .h '
        check_deps "kt" ' .c .h '
        check_deps "y"  ' .c .h '
        check_deps "l"  ' .c '
        check_deps "ec" ' .c .Sc .par .sem .s3 .opt .dmp '
        check_deps "x"  ' .h _svc.c _clnt.c _xdr.c '
      done
      if  [ "x$rm_list" != x ]; then 
#        echo rm -f $rm_list
        rm -f $rm_list
      fi
      set +x
      ## clean configurable files.
      for fn in xxx.in `ls *.in 2>/dev/null` ; do
        f1=`echo $fn | sed 's/[.]in$//1'`
        if [ -f $f1 -a $f1 != Makefile -a $f1 != Makefile_h -a $f1 != configure -a \
             $f1 != config.h -a $f1 != stamp-h ]; then
          rm -f $f1
        fi
        if [ -h $fn ]; then
          rm -f $fn
        fi
      done
      if [ -d RCS ] ; then
         ## if there is RCS -- clear makefiles to version files
         rcsclean
      fi
     ;;
  *)
    exit 1
    ;;
esac
exit 0

