#!/bin/sh
# rtc_patch_area.ver.sh 1.3 00/05/01
#
# Copyright 05/01/00 Sun Microsystems, Inc. All Rights Reserved
#

# This string is here for the 'version' command:
# It gets sed'ed in at build-time:
# @(#)RELEASE VERSION Sun Dbx Debugger 7.4 117844-05 2006/03/18


# Print usage message and exit
usage()
{
	echo "usage: $self -o objectname [-v9] [-size size] [-cc compiler]" 1>&2
	echo "       $self -so sharedlibname [-o objectname] [-v9] [-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"
o_opts_v9="-xMerge -xarch=v9"
so_opts_v9="-Kpic -xarch=v9"
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
		-V)     echo $self : "Sun Dbx Debugger 7.4 117844-05 2006/03/18";
		        exit 0;;
		-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
			if [ "$sharedlibname" = "" ]
			then sourcename=$TMPDIR/rtc8M_${UNIQUE}.s
			fi
			shift;;
    		-v9)    if [ "$sharedlibname" = "" ] 
			then
			    compileropts="$o_opts_v9"
			else
			    compileropts="$so_opts_v9"
			fi
			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

