#!/bin/sh


echo making app for $1

APPNAME=$1

cat << EOF > ${APPNAME}.m

#import <Pike/OCPikeInterpreter.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>

int main(int argc, char** argv) { 
struct object * m;
id i; 
struct svalue * sv;

// required for console mode objective c applications 
NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

// these 2 lines set up and start the interpreter. 
i = [OCPikeInterpreter sharedInterpreter]; 
[i startInterpreter];

// ok, now that we have things set up, let's use it. 
// first, an example of calling pike c level apis directly. 
f_version(0); 
printf("%s\n", Pike_sp[-1].u.string->str); 
pop_stack();
 m = load_pike_master();

 push_text("../Resources/");
 f_cd(1);

  
 push_text(argv[0]);
 apply(m, "dirname", 1);
  f_cd(1);
  push_text("../Resources");
  apply(m, "cd", 1);
  f_getcwd(0);

 printf("%s\n", Pike_sp[-1].u.string->str); 

 apply(m, "add_program_path", 1); 

// push_text(argv[0]);
 push_text("$APPNAME");
 push_text("$APPNAME.pike");
 f_aggregate(2);
 pike_push_env();

 apply(m, "_main", 2); 

// finally, we clean up. 
[i stopInterpreter]; 
[innerPool release]; 
return 0; 
}


EOF

echo "building stub..."

gcc  -F. -c "${APPNAME}.m" -o "${APPNAME}.o"
gcc "${APPNAME}.o" -o "${APPNAME}" -F.  -framework Pike -framework System -framework Foundation
#gcc -arch i386 -arch ppc -F. -c "${APPNAME}.m" -o "${APPNAME}.o"
#gcc -arch i386 -arch ppc "${APPNAME}.o" -o "${APPNAME}" -F. -lc -framework Pike -framework System

if [ ! -d "./${APPNAME}.app" ] ;
then
  echo "creating app bundle..."
  mkdir -p "${APPNAME}.app/Contents/MacOS"
  mkdir -p "${APPNAME}.app/Contents/Resources"
  mkdir -p "${APPNAME}.app/Contents/Frameworks"
  cp -rf Pike.framework "${APPNAME}.app/Contents/Frameworks"

cat << EOF > "${APPNAME}.app/Contents/Resources/${APPNAME}.pike"

import Public.ObjectiveC;
object NSApp;

int main(int argc, array argv)
{
  NSApp = Cocoa.NSApplication.sharedApplication();
  add_constant("NSApp", NSApp);
  NSApp->activateIgnoringOtherApps_(1);

  add_backend_to_runloop(Pike.DefaultBackend, 0.3);
  return AppKit()->NSApplicationMain(argc, argv);
}

EOF

# create bundle special files.
cat << EOF > ${APPNAME}.app/Contents/PkgInfo
gInfo 
APPL????
EOF

cat << EOF > ${APPNAME}.app/Contents/Info.plist

<?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>${APPNAME}</string>
        <key>CFBundleGetInfoString</key>
        <string>1.0, foo</string>
        <key>CFBundleIdentifier</key>
        <string>org.gotpike.${APPNAME}</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleSignature</key>
        <string>WvTx</string>
        <key>NSMainNibFile</key>
        <string>MainMenu</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
</dict>
</plist>
EOF

fi

mv "${APPNAME}" "${APPNAME}.app/Contents/MacOS"

rm "${APPNAME}.m" "${APPNAME}.o"
