#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992-1994 by Forschungszentrum Informatik (FZI)
# All rights reserved.
#
# You can use and distribute this software under the terms of the license
# you should have received along with this software; either version 1.1 of
# the license, or (at your option) any later version.
# For a copy of the license or for additional information about this software,
# write to Xcc Software, Durlacher Allee 53, D-76131 Karlsruhe, Germany;
# Email: obst@xcc-ka.de.
#
# 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.
# ------------------------------------------------------------------------
# 'obst-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="obst-mkInstall $action"
shift

case "$action" in
   dirs)# obst-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)# obst-mkInstall file [-s] [-m <mode>] <src> <target>
	#
	# Install the given file <src> as <target> with the given permission
	# modes (default: 0755). <target> can optionally be stripped.

	strip=
	 mode=0755

	while [ $# -gt 0 ]; do
	   case "$1" in
	      -s)  strip=1		;;
	      -m)   mode="$2"
		   shift		;;
	       *)    src="$1"
		  target="$2"
		   shift		;;
	   esac
	   shift
	done

	echo "$src --> $target"

	cp -p $src $target
	[ -z "$strip" ] || strip $target
	[ -z "$mode"  ] || chmod $mode $target
	;; # -----------------------------------------------------------------

   man)	# obst-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)  # obst-mkInstall rm <path>
	#
        # Remove file/directory <path>, echo action to stdout.

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

exit $status
