# !/bin/ksh
USAGE=\
'USAGE: install_dir [ -d config_prefix ] dest_dir_path mount_path map_to_path
   If dest_dir_path is actually a directory below mount_path, duplicate 
   directories below map_to_path to make dest_dir_path be below
   map_to_path.
'
# (C) Copyright 1995 by Michael Coulter.  All rights reserved.
# Basically we create on or for a directory many simple links and then use
# check_links or check_one_link to fix up the link.
set -P # print absolut hw path

# Define std functions

   ISOFS_UTIL_DIR="${ISOFS_UTIL_DIR:-/usr/src/linux/fs/isofs/Utils}"
   . "$ISOFS_UTIL_DIR/ksh_fns"

# Process parameters

   CONFIG_PREFIX=""
   if [ $# -ge 2 -a "$1" = "-d" ]
   then
      shift  # done with -d
      CONFIG_PREFIX="$1"; shift
   fi
   if [ $# -ne 3 ]
   then
      echo "$USAGE" >&2
      echo "$0 Expected 3 parameters, got $#" >&2
      exit 1
   fi
   DEST_DIR_PATH="$1"; shift
   MOUNT_PATH="$1"; shift
   MAP_TO_PATH="$1"; shift
   if [ ! -d "$DEST_DIR_PATH" ]
   then
      echo "$USAGE" >&2
      echo "$DEST_DIR_PATH is not a directory" >&2
      exit 1
   fi
   if [ "$DEST_DIR_PATH" = "${DEST_DIR_PATH#$MAP_TO_PATH}" ]
   then
      if [ "$DEST_DIR_PATH" = '.' -o "$DEST_DIR_PATH" = '/' ]
      then
         exit 0
      fi
      echo "$USAGE" >&2
      echo "$DEST_DIR_PATH is not below $MAP_TO_PATH" >&2
      exit 1
   fi

# Set variables

   TEMP_DIR="install_dir.$$"
   HERE="$PWD"

# Do it

   ##echo "install_dir: $DEST_DIR_PATH"
   check_cmd 1 cd "$DEST_DIR_PATH"
   THERE="$(pwd -H)"
   ##echo "install_dir: THERE is $THERE"
   # If THERE is not below MOUNT_PATH, there is nothing to do
   if [ "$THERE" != "${THERE#$MOUNT_PATH}" ]
   then
      # EQUIV= the mirror of THERE.
      if [ "$MAP_TO_PATH" = "/" ]
      then
	 EQUIV="${THERE#$MOUNT_PATH}"
      else
	 EQUIV="${MAP_TO_PATH}${THERE#$MOUNT_PATH}"
      fi
      ##echo "install_dir: EQUIV is $EQUIV"

      # We need to install THERE.  Do we need to install its parent?
      cd "$(dirname "$EQUIV")"
      if [ "$PWD" != "${PWD#${MOUNT_PATH}}" ]
      then
         ##echo "install_dir: " install_dir "$(dirname "$EQUIV")"  "$MOUNT_PATH"  "$MAP_TO_PATH"
         check_cmd 20 install_dir -d "$CONFIG_PREFIX" "$(dirname "$EQUIV")"  "$MOUNT_PATH"  "$MAP_TO_PATH"
      fi
      cd "$HERE"

      DEST_PARENT="$(dirname "$DEST_DIR_PATH")"
      if [ "$DEST_PARENT" != "$(dirname "$EQUIV")" ]
      then
	 # We need to install DEST_DIR_PATH.  Do we need to install its parent?
	 cd "$DEST_PARENT"
	 if [ "$PWD" != "${PWD#${MOUNT_PATH}}" ]
	 then
	    check_cmd 20 install_dir -d "$CONFIG_PREFIX" "$DEST_PARENT"  "$MOUNT_PATH"  "$MAP_TO_PATH"
	 fi
      fi

      # Install EQUIV
      if [ ! -d "$EQUIV" ]
      then
         ##echo "install_dir: Processing $DEST_DIR_PATH path $EQUIV " >&2
         echo "should be a directory." >&2
         exit 21
      fi

      check_cmd 2 cd "$(dirname "$THERE")"
      THERE_PARENT="$(pwd -H)"
      cd "$HERE"
      if [ -L "$EQUIV" ]
      then
         check_cmd 5 cd "$DEST_PARENT"
         rm -f "$TEMP_DIR"
         check_cmd 6 mkdir "$TEMP_DIR"
         check_cmd 7 cd "$THERE"
         for FILE in * .??* .[^.]
         do
            if [ -e "$FILE" -o -L "$FILE" ]
            then
               ##echo install_dir: ln -s "${THERE}/${FILE}" "${DEST_PARENT}/${TEMP_DIR}/${FILE}"
               ln -s "${THERE}/${FILE}" "${DEST_PARENT}/${TEMP_DIR}/${FILE}"
            fi
         done
         cd "$HERE"
         check_cmd 8 rm -f "$EQUIV"
         check_cmd 12 cd "$DEST_PARENT"
         check_cmd 9 mv "$TEMP_DIR" "$EQUIV"
         check_cmd 10 cd "$HERE"
         ##echo install_dir: check_cmd 11 check_links -d "$CONFIG_PREFIX" "$EQUIV"
         check_cmd 11 check_links -d "$CONFIG_PREFIX" "$EQUIV"
      fi
      if [ -L "$DEST_DIR_PATH" ]
      then
	 check_cmd 13 rm -f "$DEST_DIR_PATH"
	 check_cmd 14 ln -s "$EQUIV" "$DEST_DIR_PATH"
      fi
   fi
   cd "$HERE"
