#!/bin/sh
#
# Copyright 1991-1998 by Open Software Foundation, Inc. 
#              All Rights Reserved 
#  
# Permission to use, copy, modify, and distribute this software and 
# its documentation for any purpose and without fee is hereby granted, 
# provided that the above copyright notice appears in all copies and 
# that both the copyright notice and this permission notice appear in 
# supporting documentation. 
#  
# OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE. 
#  
# IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
# LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 
# NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
#
# cmk1.1

# setuplinks - make links to Pure Mach and Host OS exports

# Ensure that the variables we refer to have values.  If we are invoked
# via make within a sandbox they should.
for i in EXPORTBASE PMKEXPORTLINK INCPREFIX LIBPREFIX MAKEDEFS RULES_MK; do
    if [ "`eval echo \\$$i`" = "" ]; then
	echo "$i undefined or empty.  This script must be run in a \`workon'"
	exit 1
    fi 2>&1
done

prompt() {
    awk 'BEGIN {printf "%s  ", "'"$*"'"; exit}'
}

confirm() {
    while :; do
	prompt "$* [yes or no]?"
	if read yes; then :; else exit 1; fi
	case $yes in
	[yY]*)
	    return 0;;
	[nN]*)
	    return 1;;
	*)
	    echo "Please answer yes or no."
	esac
    done
}

PMKDIR=$EXPORTBASE/$PMKEXPORTLINK
PMKTESTFILE=$MAKEDEFS/$RULES_MK
PMKNAME="Pure Mach export"
PMKBLURB="the export directory of a Mach kernel sandbox.  Definitions for Make
and Mach-related includes and libraries will be obtained from this directory."
INCDIR=$EXPORTBASE/$INCPREFIX
INCTESTFILE=stdio.h
INCNAME="Include"
INCBLURB="the C include directory.  Typically either /usr/include
or the exported usr/include directory of an OSF/1 sandbox."
LIBDIR=$EXPORTBASE/$LIBPREFIX
LIBTESTFILE=libc.a
LIBNAME="Library"
LIBBLURB="the C library directory.  Typically /usr/lib or /usr/ccs/lib,
or the corresponding exported directories of an OSF/1 sandbox."

for exp in PMK INC LIB; do
    echo
    dir=`eval echo \\$${exp}DIR`
    testfile=`eval echo \\$${exp}TESTFILE`
    name=`eval echo \\$${exp}NAME`
    blurb="`eval echo \"\\"\\$${exp}BLURB\\"\"`"
    if [ -d $dir ]; then
	echo "$name directory exists:"
	echo "  $dir"
	if [ -f $dir/$testfile ]; then
	    echo "  and contains $testfile."
	    anyway=" anyway"
	else
	    echo "  but does not contain $testfile (or it is not readable)."
	    anyway=
	fi
	if confirm "Replace it$anyway"; then :
	else
	    continue
	fi
	if rm $dir; then :
	else
	    echo "rm failed:"
	    ls -l $dir
	    if confirm "Try rm -r"; then
		if rm -fr $dir; then :
		else
		    echo "rm -fr $dir failed."
		    echo "Remove $dir and try again."
		    exit 1
		fi
	    else
		continue
	    fi
	fi
    else
	echo "$name directory does not exist (or is not readable):"
	if ls -l $dir; then
	    echo "This looks like a broken symbolic link."
	    if confirm "Remove it"; then
		rm $dir
	    fi
	fi
    fi

    # At this point we have decided to make a link.
    exportdirname=${exp}EXPORTDIR
    exportdir=`eval echo \\$$exportdirname`
    if [ "$exportdir" != "" ]; then
	echo "\$$exportdirname exists with value $exportdir"
	if [ -f $exportdir/$testfile ]; then
	    echo "  and contains $testfile."
	    anyway=
	else
	    echo "  but does not contain $testfile (or it is not readable)."
	    anyway=" anyway"
	fi
	if confirm "Use it$anyway"; then :
	else
	    exportdir=
	fi
    fi
    if [ "$exportdir" = "" ]; then
	echo "This is $blurb"
	echo "It should contain the file $testfile, for example."
	while :; do
	    prompt "$name directory?"
	    if read exportdir; then :
	    else
		exit 1
	    fi
	    if [ -f $exportdir/$testfile ]; then
		break
	    elif [ -d $exportdir ]; then
		echo "$exportdir exists"
		echo "  but does not contain $testfile (or it is not readable)"
		echo "directory contents:"
		ls $exportdir
	    else
		echo "$exportdir does not exist (or is not readable)"
	    fi
	    if confirm "Use it anyway"; then
		break
	    fi
	done
    fi

    # At this point we know what link we will be making.
    if makepath $dir && ln -s $exportdir $dir; then :
    else
	exit 1
    fi
    if [ ! -f $dir/$testfile ]; then
	echo "WARNING: file $dir/$testfile"
	echo "  does not exist (or is not readable)."
    fi
done

# IF we're building using gcc then we need to make another
# symbolic link to point to the compiler includes, otherwise we need
# to make an empty directory.

dir=$EXPORTBASE/compiler-includes
if confirm "Are you self-hosting using the standard gcc to compile"; then

	gcclibname=`gcc -print-libgcc-file-name`
	compilerlibdir=`dirname $gcclibname`
	compilerincludes=$compilerlibdir/include

	echo A symbolic link needs to exist of the form:
	echo
	echo ln -sf $compilerincludes $dir
	echo

	if confirm "create" ; then
		if [ -e $dir ] ; then
			echo "$dir already exists:"
			echo
			ls -ld $dir
			echo
			if confirm "replace"; then :
			else
				exit 1
			fi
			if rm $dir 2>/dev/null; then :
			else
				if rmdir $dir; then :
				else
					echo rm/rmdir failed, aborting
					exit 1
				fi
			fi
		fi

		echo
		if ln -sf $compilerincludes $dir; then
			echo symbolic link created
		else
			echo FAILED - please make this link by hand
			exit 1
		fi
	else
		echo Please create $dir appropriately by hand
	fi
else
	echo The directory $dir must be created and be empty
	echo
	if confirm "create"; then
		if [ -e $dir ] ; then
			echo
			echo "$dir already exists:"
			echo
			ls -l $dir
			echo
			if confirm "replace"; then :
			else
				exit 1
			fi
			if rm $dir 2>/dev/null; then :
			else
				if rmdir $dir; then :
				else
					echo rm/rmdir failed, aborting
					exit 1
				fi
			fi
		fi
		echo
		if mkdir $dir; then
			echo directory created
		else
			echo mkdir failed, aborting
			exit 1
		fi
	else
		echo Please create $dir appropriately by hand
	fi
fi
