#!/bin/sh
# rtc_patch_area.sh 1.21 96/01/10
#
# Copyright 01/10/96 Sun Microsystems, Inc. All Rights Reserved
#

# Print usage message and exit
usage()
{
	echo "usage: $self -o objectname [-size size] [-cc compiler]" 1>&2
	echo "       $self -so sharedlibname [-o objectname] [-size size] [-cc compiler]" 1>&2
	exit 1
}

self=`basename $0`
UNIQUE=${LOGNAME}
TMPDIR=${TMPDIR:-/tmp}          # where to put temporary files

# Set defaults
size=8000000
objectname=
sharedlibname=
compiler="cc"
o_opts="-xMerge"
so_opts="-Kpic"
compileropts=
ldoptions="-G"
os=5x
if [ "`uname -r | cut -f1 -d'.'`" = "4" ] ; then
    compiler="acc"
    o_opts="-R"
    so_opts="-g -pic"
    ldoptions="-assert pure-text -d -dc -dp"
    os=4x
fi
saveobject=no

# Handle flags
while [ $# -gt 0 ]
do
	case $1 in
		-so)	shift; 
			if [ $# -gt 0 ]
			then sharedlibname=$1;
			     compileropts="$so_opts";
			else usage
			fi
			sourcename=$TMPDIR/rtc8M_${UNIQUE}.c
			shift;;
		-o)	shift; 
			if [ $# -gt 0 ]
			then objectname=$1;
			     compileropts="$o_opts";
			else usage
			fi
			saveobject=yes
			sourcename=$TMPDIR/rtc8M_${UNIQUE}.s
			shift;;
		-size)	shift; 
			if [ $# -gt 0 ]
			then size=$1
			else usage
			fi
			shift;;
		-cc)	shift; 
			if [ $# -gt 0 ]
			then compiler="$1"
			else usage
			fi
			shift;;
		-*)	usage;;
		*)	usage;;
	esac
done

# Set object file
if [ "$objectname" = "" ]; then
    if [ "$sharedlibname" = "" ] ; then
	    usage
    fi
    objectname=$TMPDIR/rtc8M_${UNIQUE}.o
fi

# Create source file
/bin/rm -f ${sourcename}
if [ "${sharedlibname}" != "" ] ; then
    echo "static char __RTC_EXPLICIT_PATCH_AREA[${size}+4096];" > ${sourcename}
else
    if [ "${os}" = "5x" ] ; then
	cat > ${sourcename} <<!
        .section        ".text",#alloc,#execinstr
__RTC_EXPLICIT_PATCH_AREA:
        .skip   ${size}+4096
        .type   __RTC_EXPLICIT_PATCH_AREA,#object
        .size   __RTC_EXPLICIT_PATCH_AREA,${size}+4096
!
    else
	cat > ${sourcename} <<!
        .seg    "text"
___RTC_EXPLICIT_PATCH_AREA:
        .skip   ${size}+4096
!
    fi
fi

# Create object file
/bin/rm -f ${objectname}
${compiler} ${compileropts} -c -o ${objectname} ${sourcename}
status=$?
/bin/rm -f ${sourcename}
if [ $status -ne 0 ]
then
    exit 1
fi

# Create loadobject
if [ "${sharedlibname}" != "" ] ; then
    /bin/rm -f ${sharedlibname}
    ld ${ldoptions} -o ${sharedlibname} ${objectname}
    status=$?
    if [ "${saveobject}" = "no" ] ; then
	/bin/rm -f ${objectname}
    fi
    if [ $status -ne 0 ]
    then
	exit 1
    fi
    echo ${sharedlibname}
else
    echo ${objectname}
fi

# All done
exit 0

#pragma	@(#)RELEASE VERSION Workshop 4.0 10/15/96 [dbx 4.0]
