#!/bin/sh
PACKAGE=samurai-graph
VERSION=0.3.1
APPNAME="Samurai Graph"

BASEDIR=`dirname $0`
cd "$BASEDIR"
DISTDIR=../../dist
TOPDIR=../..

UNAME=`uname`
# error check
if [ $UNAME != "Darwin" ]; then
  echo "Error: running host is not MacOS X";
  exit 65;
fi

APPLICATION="$APPNAME.app"
APP_CONTENTS="$APPLICATION/Contents"
APP_MACOS="$APP_CONTENTS/MacOS"
APP_RESOURCES="$APP_CONTENTS/Resources"
APP_JAVA="$APP_RESOURCES/Java"
APP_JAVALIB="$APP_RESOURCES/Java/lib"
APP_JAVALIBVG="$APP_JAVALIB/vectorgraphics"
APP_DOC="$APP_RESOURCES/Documents"
APP_DOCVG="$APP_DOC/vectorgraphics"
BUNDLER="resources/bundler"
IMGFILE="$PACKAGE-mac-$VERSION.dmg"
IMGDIST="diskimage"

# delte Java bundler
delete_java_bundler () {
  echo -n "[Samurai Graph] delete java bundler application...  "
  rm -rf "$APPLICATION"
  echo 'done'
}

# check dist directory
check_dist_directory () {
  if [ ! -d $DISTDIR ]; then
    echo "Error: $DISTDIR not found"
    exit 65;
  fi
  if [ ! -f $DISTDIR/samurai-graph.jar ]; then
    echo "Error: $DISTDIR/samurai-graph.jar not found"
    exit 65;
  fi
}

# build Java bundler
build_java_bundler () {
  echo -n "[Samurai Graph] build java bundler application...  "
  mkdir "$APPLICATION"
  mkdir "$APP_CONTENTS"
  mkdir "$APP_MACOS"
  mkdir "$APP_RESOURCES"
  mkdir "$APP_JAVA"
  mkdir "$APP_JAVALIB"
  mkdir "$APP_JAVALIBVG"
  mkdir "$APP_DOC"
  mkdir "$APP_DOCVG"
  cp "$BUNDLER/Info.plist"         "$APP_CONTENTS"
  cp "$BUNDLER/PkgInfo"            "$APP_CONTENTS"
  cp "$BUNDLER/version.plist"      "$APP_CONTENTS"
  cp "$BUNDLER/Samurai Graph"      "$APP_MACOS"
  cp "$BUNDLER/samurai-graph.icns" "$APP_RESOURCES"
  cp "$DISTDIR/samurai-graph.jar"  "$APP_JAVA"
  cp "$DISTDIR/LICENSE.txt"        "$APP_DOC"
  cp "$DISTDIR/COPYING.txt"        "$APP_DOC"
  cp "$DISTDIR/lib/vectorgraphics/freehep-base.jar"            "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphics2d.jar"      "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio.jar"      "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-cgm.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-emf.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-gif.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-java.jar" "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-pdf.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-ppm.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-ps.jar"   "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-svg.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/lib/vectorgraphics/freehep-graphicsio-swf.jar"  "$APP_JAVALIBVG"
  cp "$DISTDIR/doc/vectorgraphics/LGPL.txt"                    "$APP_DOCVG"
  cp "$DISTDIR/doc/vectorgraphics/LICENSE.txt"                 "$APP_DOCVG"
  cp "$DISTDIR/doc/vectorgraphics/ReleaseNotes-1.2.2.html"     "$APP_DOCVG"
  cp "$DISTDIR/doc/vectorgraphics/SAMURAIGRAPH-ChangeLog.txt"  "$APP_DOCVG"
  echo 'done'
}

# delete disk image file
delete_diskimage_file () {
  echo -n "[Samurai Graph] delete disk image file...  "
  rm -f "$IMGFILE"
  rm -f "$IMGFILE.gz"
  rm -rf "$IMGDIST"
  echo 'done'
}

# build disk image file
build_diskimage_file () {
  echo -n "[Samurai Graph] build disk image file...  "
  mkdir "$IMGDIST"
  mkdir "$IMGDIST/Example Data"
  # put the resource forks back together
  #/System/Library/CoreServices/FixupResourceForks -nodelete diskimage
  cp -R  "$APPLICATION" "$IMGDIST"
  cp     "$DISTDIR/COPYING.txt" "$IMGDIST"
  cp     "$DISTDIR/LICENSE.txt" "$IMGDIST"
  cp     "$TOPDIR/examples/data/"*.txt "$IMGDIST/Example Data"
  cp     "$TOPDIR/examples/data/"*.sgp "$IMGDIST/Example Data"
  cp     "resources/diskimage/_DS_Store" "$IMGDIST/.DS_Store"
  cp     "resources/diskimage/_DS_Store2" "$IMGDIST/Example Data/.DS_Store"
  hdiutil create -srcfolder "$IMGDIST" -volname "$APPNAME" "$IMGFILE" >/dev/null
  gzip "$IMGFILE"
  echo 'done'
}

case "$1" in
build)
  check_dist_directory
  delete_java_bundler
  delete_diskimage_file
  build_java_bundler
  build_diskimage_file
  ;;
clean)
  delete_java_bundler
  delete_diskimage_file
  ;;
*)
  echo "Usage: ${0##*/}: { build | clean }" 2>&1
  exit 65
  ;;
esac

exit 0
