:
#!/bin/sh
# Copyright 1995 Network Computing Devices, Inc. (hereafter referred to
#  as NCDI).
#
# $NCDId: @(#)mapnames,v 1.5 1995/11/13 19:26:52 jcc Exp $
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of NCDI may not be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY NCDI AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# An unpublished work.

# --------------------------------------------------------------------------
# Set defaults
VERBOSE=
IS_DIR=-d

# --------------------------------------------------------------------------
# Print usage message

echo_usage='
cat <<!

Usage: mapnames [-v] cdrom_directory [ destination_directory ]

!
exit 1
'

# --------------------------------------------------------------------------
# Parse any arguments.

if [ $# -lt 1 -o $# -gt 3 ] ; then
	eval "$echo_usage"
fi

while [ X"$1" != "X" ] ; do
	case $1 in
		-v|-V)
			# This verbose option is used to see the links as they are created
			VERBOSE="-v"; shift
			if [ $# -lt 1 ] ; then
				eval "$echo_usage"
			fi
			;;
		-h|-help|-Help|-HELP) eval "$echo_usage" ;;
		-*) echo "Unexpected argument \"$1\""; eval "$echo_usage" ;;
		*) CDROM=$1; shift; break ;;
	esac
done

if [ X"$1" != "X" ] ; then
	DEST=$1
	DESTPARM=$1
	# create the directory if it doesn't already exist
	OLD_DIR=`ls -d $DEST 2>/dev/null`
	if [ "X$OLD_DIR" = "X" ] ; then
		mkdir $DEST
	fi
	chmod 755 $DEST >/dev/null 2>&1
	if [ ! $IS_DIR $DEST ] ; then
		echo "mapnames, ERROR: $DEST is not accessable. Exiting"
		exit 1
	fi
else
	# If the user didn't specify the destination, assume the current
	# directory.  DESTPARM is used if mapnames must be reinvoked 
	# using /bin/sh5.
	DEST=`/bin/pwd`
	DESTPARM=	
fi

# --------------------------------------------------------------------------
# Set LANG environment variable.

if [ X"$LANG" != "X" -a \( X"$LANG" != "XC" -o X"$LANG" != "POSIX" \) ] ; then
	# Changing LANG on systems like AIX and HP can cause changes in
	# sorting algorithms, pattern matching, case conversion etc.
	LANG=C; export LANG
fi

# --------------------------------------------------------------------------
# Determine how the CD appears to the user
# mapnames appears as either "mapnames." or "MAPNAMES.;1"
# On some systems the "dot" only appears if the cd was mounted to "showdots"
# so that mapnames appears as "mapnames"

MAPFILE="NCDFILE.MAP;1"

#	The construct [pattern|pattern] doesn't work with HP-UX grep
#	so another more verbose form must be used to find the mapfile
#	on the CD.

NAME1=`ls $CDROM | grep "NCDFILE.MAP" ``ls $CDROM | grep "ncdfile.map" `
NAME2=`echo "$NAME1" | cut -d";" -f1 `

#	If the filename appears as lowercase, you'll need to shift the
#	CD filename to lowercase to make the link.

if [ "X$NAME2" = "XNCDFILE.MAP" ]
then
    SHFTCASE="no"
else
    SHFTCASE="yes"
    MAPFILE=`echo "$MAPFILE" | tr [A-Z] [a-z] `
fi

#	If the filename appears without the version number, you'll need to 
#	cut off the version number from CD filename to make the link.

if [ "X$NAME1" != "X$NAME2" ]
then
    XVERSION="no"
else
    XVERSION="yes"
    MAPFILE=`echo "$MAPFILE" | cut -d";" -f1 `
fi
#
#	Now that you have the MAPFILE name to read from, verify
#	that the read command works.  On ULTRIX, /bin/sh reads
#	differently, and /bin/sh5 must be used.

LINE=`while read F1 F2 F3 F4 F5 F6
	do
		echo $F6
		break
	done<$CDROM/$MAPFILE`

if [ "X$LINE" != "X" ]
then
	SH=`ls /bin/sh5 2>/dev/null`
	if [ "X$SH" = "X" ]
	then
		echo "$0: Error: /bin/sh5 not available."
		exit 1
	else
		/bin/sh5 $0 $VERBOSE $CDROM $DESTPARM
		exit 0
	fi
fi
#
#	CDNAME is the name as it appears on the CD
#	UNAME is the normal file name
#	LNAME is the name of the previous directory hierarchy when
#	      the directories are more than 8 levels deep.
#

while read TYPE CDNAME UNAME LNAME
do

#	Shift to lower case and cut off the version string as necessary

    if [ "$SHFTCASE" = "yes" ]
    then
        CDNAME=`echo "$CDNAME" | tr [A-Z] [a-z] `
        LNAME=`echo "$LNAME" | tr [A-Z] [a-z] `
    fi

    if [ "$XVERSION" = "yes" ]
    then
        CDNAME=`echo "$CDNAME" | cut -d";" -f1 `
    fi

#		For files do a simple link

    if [ $TYPE = 'F' ]
    then
        if [ "X$VERBOSE" = "X-v" ]
        then
	    echo $DEST/$UNAME
        fi
	if  [ X"$CDNAME" != X"mapnames." -a X"$CDNAME" != X"MAPNAMES.;1" ]
	then
		ln -s $CDROM/$CDNAME $DEST/$UNAME
	fi

#		For directories recurse

    elif [ $TYPE = 'D' ]
    then
        if [ "X$VERBOSE" = "X-v" ]
        then
	    echo $DEST/$UNAME/
        fi
	$0 $VERBOSE $CDROM/$CDNAME $DEST/$UNAME


#		For moved directories recurse, using the linked directory

    elif [ $TYPE = 'M' ]
    then
        if [ "X$VERBOSE" = "X-v" ]
        then
	    echo $DEST/$UNAME/
        fi
	$0 $VERBOSE $CDROM/$LNAME $DEST/$UNAME

#		For links do a simple link

    elif [ $TYPE = 'L' ]
    then
	case $LNAME in
	    /*)
		echo $DEST/$UNAME -\> $LNAME
		ln -s $LNAME $DEST/$UNAME
		;;
	    *)
		echo $DEST/$UNAME -\> $DEST$UNAME/$LNAME
		(cd $DEST; ln -s $LNAME $UNAME)
		;;
	esac
    else
	echo Unknown type $TYPE
    fi
done < $CDROM/$MAPFILE
