#!/bin/sh
# Compile script to compile against Elysion frameworks
# Works for Mac OS X and Linux



FPC_BIN=`which fpc`
SRC_MAIN="main.dpr"
BIN_FOLDER="../bin"
SRC_UNITS="./units"
SRC_HEADER="../lib/header"
LIB_FOLDER="../lib"
EXEC_NAME="caverndefense"
APP_NAME="Cavern Defense"

if [ -f /System/Library/Frameworks/Cocoa.framework/Cocoa ]
then

  FPC_BIN=`which ppc386`

  # Compiling Intel binary
  ${FPC_BIN} -Mdelphi -XMSDL_main -O3 -FE${BIN_FOLDER} -Fu${LIB_FOLDER} -Fu${SRC_UNITS} -Fu${SRC_HEADER} -k"-L${LIB_FOLDER}/frameworks -L/usr/X11R6/lib -framework Cocoa -framework SDL -framework SDL_image -framework SDL_mixer -framework SDL_ttf -framework SDL_gfx" ${SRC_MAIN}
  mv ${BIN_FOLDER}/main ${BIN_FOLDER}/main-intel
  rm ${BIN_FOLDER}/link.res
  rm ${BIN_FOLDER}/*.o ${BIN_FOLDER}/*.ppu
  #rm ${SRC_HEADER}/*.o ${SRC_HEADER}/*.ppu ${SRC_HEADER}/*.dcu ${SRC_HEADER}/*.a
  
  
  FPC_BIN=`which ppcppc`
  
  # Compiling PowerPC binary
  ${FPC_BIN} -Mdelphi -XMSDL_main -O3 -FE${BIN_FOLDER} -Fu${LIB_FOLDER} -Fu${SRC_UNITS} -Fu${SRC_HEADER} -k"-L${LIB_FOLDER}/frameworks -L/usr/X11R6/lib -framework Cocoa -framework SDL -framework SDL_image -framework SDL_mixer -framework SDL_ttf -framework SDL_gfx" ${SRC_MAIN}
  mv ${BIN_FOLDER}/main ${BIN_FOLDER}/main-ppc
  rm ${BIN_FOLDER}/*.o ${BIN_FOLDER}/*.ppu
  
  
  # Creating universal binary
  if [ -f ../bin/main-intel ] &&  [ -f ../bin/main-ppc ] 
  then
    strip ../bin/main-intel
    strip ../bin/main-ppc
      
    lipo -create ../bin/main-intel ../bin/main-ppc -output ../bin/${EXEC_NAME}
    rm -rf ../bin/main-intel
    rm -rf ../bin/main-ppc
  fi
  
  if [ -d ../bin/"${APP_NAME}.app" ] 
  then
    echo "  ... Removing old application"
	rm -rf ../bin/"${APP_NAME}.app"
  fi

  echo "  ... Creating Application Bundle"

  mkdir "../bin/${APP_NAME}.app"
  mkdir "../bin/${APP_NAME}.app/Contents"
  mkdir "../bin/${APP_NAME}.app/Contents/MacOS"
  mkdir "../bin/${APP_NAME}.app/Contents/Resources"
  mkdir "../bin/${APP_NAME}.app/Contents/Frameworks"
  
  cp -R  ../resources/ "../bin/${APP_NAME}.app/Contents/Resources/"
  cp -R ${LIB_FOLDER}/frameworks/*.framework "../bin/${APP_NAME}.app/Contents/Frameworks/"
  
	mv ../bin/${EXEC_NAME} "../bin/${APP_NAME}.app/Contents/MacOS/" 
	
	echo "<?xml version='1.0' encoding='UTF-8'?>\
	<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\
	<plist version=\"1.0\">\
	<dict>\
	        <key>CFBundleDevelopmentRegion</key>\
	        <string>English</string>\
	        <key>CFBundleExecutable</key>\
	        <string>${EXEC_NAME}</string>\
	        <key>CFBundleIconFile</key>\
	        <string>icons.icns</string>\
	        <key>CFBundleIdentifier</key>\
	        <string>ger.edu.5eb.playthecity</string>\
	        <key>CFBundleInfoDictionaryVersion</key>\
	        <string>6.0</string>\
	        <key>CFBundleName</key>\
	        <string>${APP_NAME}</string>\
	        <key>CFBundlePackageType</key>\
	        <string>APPL</string>\
	        <key>CFBundleSignature</key>\
	        <string>FDRS</string>\
	        <key>CFBundleVersion</key>\
	        <string>1.0</string>\
	        <key>CSResourcesFileMapped</key>\
	        <true/>\
	</dict>\
	</plist>" >> "../bin/${APP_NAME}.app/Contents/Info.plist"

	echo "APPLFDRS" >> "../bin/${APP_NAME}.app/Contents/PkgInfo"
	  
else
  if [ -f /usr/lib64 ]
  then
    fpc -Mdelphi -O3 -Fu${SRC_UNITS} -Fu${SRC_HEADER} -Fu${LIB_FOLDER} -FE${BIN_FOLDER} ${SRC_MAIN}
	rm ${BIN_FOLDER}/*.o ${BIN_FOLDER}/*.ppu
  else
    fpc -Mdelphi -O3 -Fu${SRC_UNITS} -Fu${SRC_HEADER} -Fu${LIB_FOLDER} -FE${BIN_FOLDER} ${SRC_MAIN}
	rm ${BIN_FOLDER}/*.o  ${BIN_FOLDER}/*.ppu
  fi
  
  if [ -f ../bin/main ]
  then
    mv ../bin/main ../bin/${EXEC_NAME}
  fi
fi
