#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992-1994 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
#
# The directory installation part is from Noah Friedman's mkinstalldirs
# utility (<friedman@prep.ai.mit.edu>), declared as public domain by the
# author. This part is not covered by the above copyright.
# ------------------------------------------------------------------------
# 'mkInstall - 6/6/94 - D. Theobald'

# Workhorse for performing (de)installation of several file types. The
# action to be performed is denoted by the first parameter as described
# below.

status=0
action="$1"
  self="mkInstall $action"
shift

case "$action" in
   dirs)# mkInstall dirs <path>...
	#
	# Create the given directories (plus any parent directories) if
	# necessary. Echo any newly created directory to stdout.

	defaultIFS=' 	
'
	IFS="${IFS-${defaultIFS}}"

	for file in ${1+"$@"} ; do 
	   oIFS="${IFS}"
	   # Some sh's can't handle IFS=/ for some reason.
	   IFS='%'
	   set - `echo ${file} | sed -e 's@/@%@g' -e 's@^%@/@'`
	   IFS="${oIFS}"
	
	   pathcomp=''

	   for d in ${1+"$@"} ; do
	     pathcomp="${pathcomp}${d}"

	     if test ! -d "${pathcomp}"; then
	        echo "mkdir $pathcomp" 1>&2
	        mkdir "${pathcomp}" || status=$?
	     fi

	     pathcomp="${pathcomp}/"
	   done
	done
	;; # -----------------------------------------------------------------

   file)# mkInstall file <command> [<opts>...] <src> <target>
	#
	# Invoke the <command>/<options> given as parameters, echo <src> and
	# <target> to stdout.

	for arg
	do
	      src="$target"
	   target="$arg"
	done

	echo "$src --> $target"
	eval "$*"
	;; # -----------------------------------------------------------------

   man)	# mkInstall man <srcdir> <targetdir> <old_ext> <new_ext>
	#
	# Install the OBST manpages and echo the moved manpages.
	# Each call moves the manpages from $srcdir to $targetdir.  If the
	# old and new extensions of the manpages differ, the manpage will be
	# renamed and occurences of the old extensions inside manpage
	# (typically in ".so ..." include statements) are replaced by the
	# new extension.

	usage='<srcdir> <targetdir> <old_ext> <new_ext>'

	[ $# -le 1 ]  && { echo >&2 "usage: $self $usage"; exit 1; }

	   srcdir="$1"
	targetdir="$2"
	  old_ext="$3"
	  new_ext="$4"
	 old_sect="`echo $old_ext | sed 's|^\.||'`"
	 new_sect="`echo $new_ext | sed 's|^\.||'`"
	 new_suff="`echo $targetdir | sed 's|.*man\(.*\)|\1|'`"

	cd "$srcdir"
	for f in *$old_ext
	do
	   cmd="s|$old_ext\$|$new_ext|"
	   target="$targetdir/`echo $f | sed $cmd`"
	   echo "$f --> $target"

	   sed -e "s|^\([ ]*\.so[ ]*man\)[^/]*\(/.*\)$old_ext|\1$new_suff\2$new_ext|"\
	       -e "s|^\(\.TH .* \)$old_sect |\1$new_sect |"\
	       "$f"\
	     > "$target"
	done
	;; # -----------------------------------------------------------------

   rm)  # mkInstall rm <path>
	#
        # Remove file/directory <path>, echo action to stdout.

	echo `basename $1`
	rm -rf $1
	;;
esac

exit $status
