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

BASEDIR=`dirname $0`
cd "$BASEDIR"
DISTDIR=../../dist
HTMLDIR=../../../doc/usersman/html-ja
HTMLIMAGEDIR=../../../doc/usersman/images
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"
VECTORIMGDIST="diskimage_vector"
VECTORVERSION=`echo m$VERSION | sed -e 's+\.++g'`
VECTORIMGFILE="$PACKAGE-$VECTORVERSION.dmg"

# 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
}

# check manual directory
check_html_directory () {
  if [ ! -d $HTMLDIR ]; then
    echo "Error: $HTMLDIR not found"
    exit 65;
  fi
  if [ ! -d $HTMLIMAGEDIR ]; then
    echo "Error: $HTMLIMAGEDIR not found"
    exit 65;
  fi
  if [ ! -f $HTMLDIR/index.html ]; then
    echo "Error: $HTMLDIR/index.html not found"
    exit 65;
  fi
}

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

# 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 "$BUNDLER/samurai-graph-property.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 "$PACKAGE-mac-"*.*.*".dmg"
  rm -f "$PACKAGE-mac-"*.*.*".dmg.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'
}

# delete disk image file for vectordesign
delete_vector_diskimage_file () {
  echo -n "[Samurai Graph] delete disk image file for vectordesign...  "
  rm -f "$PACKAGE-m"*".dmg"
  rm -f "$PACKAGE-m"*".dmg.gz"
  rm -rf "$VECTORIMGDIST"
  echo 'done'
}

# build disk image file for vectordesign
build_vector_diskimage_file () {
  echo -n "[Samurai Graph] build disk image file for vectordesign...  "
  mkdir "$VECTORIMGDIST"
  mkdir "$VECTORIMGDIST/Example Data"
  mkdir "$VECTORIMGDIST/Users Manual"
  mkdir "$VECTORIMGDIST/Users Manual/images"
  # put the resource forks back together
  #/System/Library/CoreServices/FixupResourceForks -nodelete $VECTORIMGDIST
  cp -R  "$APPLICATION" "$VECTORIMGDIST"
  cp     "$DISTDIR/COPYING.txt" "$VECTORIMGDIST"
  cp     "$DISTDIR/LICENSE.txt" "$VECTORIMGDIST"
  cp     "resources/vector/README.txt" "$VECTORIMGDIST"
  cp     "$TOPDIR/examples/data/"*.txt "$VECTORIMGDIST/Example Data"
  cp     "$TOPDIR/examples/data/"*.sgp "$VECTORIMGDIST/Example Data"
  cp -R  "$HTMLDIR" "$VECTORIMGDIST/Users Manual/ja"
  cp     "$HTMLIMAGEDIR/"*.gif "$VECTORIMGDIST/Users Manual/images"
  cp     "resources/vector/_DS_Store" "$VECTORIMGDIST/.DS_Store"
  cp     "resources/vector/_DS_Store2" "$VECTORIMGDIST/Example Data/.DS_Store"
  cp     "resources/vector/_DS_Store3" "$VECTORIMGDIST/Users Manual/.DS_Store"
  cp     "resources/vector/_DS_Store4" "$VECTORIMGDIST/Users Manual/images/.DS_Store"
  cp     "resources/vector/_DS_Store5" "$VECTORIMGDIST/Users Manual/ja/.DS_Store"
  cp     "resources/vector/_DS_Store6" "$VECTORIMGDIST/Users Manual/ja/images/.DS_Store"
  hdiutil create -srcfolder "$VECTORIMGDIST" -volname "$APPNAME" "$VECTORIMGFILE" >/dev/null
  gzip "$VECTORIMGFILE"
  echo 'done'
}

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

exit 0
