# Script for creating "sig_cpu.inc" from the C headers.
# Necessary for iPhone but can not be legally distributed!

# This is cut from Jonas Maebe's installation script
# to make a bare essentials standalone version

# sudo sh sigcpuscript.sh
# sudo needed if FPCSOURCEDIR below is not user writable

# This line must match your iPhone SDK version!
IPHONESDKDIR=iPhoneOS4.3.sdk

# This line must match your FPC source location (default matches my build instructions)
FPCSOURCEDIR="/Developer/ObjectivePascal/fpc/"

# some required/used file and directory names
IPHONEPLATFORMBASEDIR=/Developer/Platforms/iPhoneOS.platform/Developer
INCLUDEDIR="$IPHONEPLATFORMBASEDIR"/"SDKs/$IPHONESDKDIR/usr/include"
STRUCTSHEADER="$INCLUDEDIR"/arm/_structs.h

# create the Pascal version of the _structs.h header, the whole reason why we
# we have to go through all this trouble...
/usr/bin/gcc -I"$INCLUDEDIR" -E -D__need_mcontext_t -D__DARWIN_UNIX03 "$STRUCTSHEADER" | sed -e 's/__uint32_t/cuint32/g' -e 's/_STRUCT_ARM_EXCEPTION_STATE/__darwin_arm_exception_state/' -e 's/_STRUCT_ARM_THREAD_STATE/__darwin_arm_thread_state/' -e 's/_STRUCT_ARM_VFP_STATE/__darwin_arm_vfp_state/' -e 's/__darwin_mcontext/mcontext_t/' > "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.h
if [ $? != 0 ]; then
  echo "Unable to preprocess the _structs.h header file at \"$STRUCTSHEADER\" using \"/usr/bin/gcc\""
  failure
fi
/usr/local/bin/h2pas -i -o "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.inc "$FPCSOURCEDIR"/rtl/darwin/arm/sig_cpu.h
if [ $? != 0 ]; then
  echo "Unable to translate the preprocessed header file at \"$FPCSOURCEDIR/rtl/darwin/arm/sig_cpu.h\""
  echo "Make sure not to distribute this file in source form, because you would violate"
  echo "your iPhone SDK agreement!"
  failure
fi
