#!/bin/bash

# The jar analyzer provides two different entry points,
# one for a GraphViz dot format result and one for an
# XML result.
MAIN_CLASS_DOT=com.kirkk.analyzer.textui.DOTSummary
MAIN_CLASS=com.kirkk.analyzer.textui.XMLUISummary

# We only depend on the jarnalyzer jar as the jar's
# manifest contains a Class-Path entry pointing to
# further dependencies.
CLASSPATH=/usr/share/jaranalyzer/jaranalyzer.jar

# This method displays usage instructions.
syntax()
{
  echo "JarAnalyzer is a dependency management utility for jar files."
  echo "It's primary purpose is to traverse through a directory, parse"
  echo "each of the jar files in that directory, and identify the"
  echo "dependencies between the jar files. The output is an xml or"
  echo "GraphViz dot file representing the PhysicalDependencies between"
  echo "the jar files."
  echo
  echo "JarAnalyzer will ask for a directory to analyze and for a"
  echo "filename where results should been written to."
  echo
  echo "Usage: jaranalyzer [OPTION]"
  echo
  echo "Options:"
  echo -e "\t-h --help\tshow this text"
  echo -e "\t-V --version\tprint the version"
  echo -e "\t-d --dot\tproduce GraphViz output (dot-format), default is XML format."
}

# This method displays version info.
version()
{
  VERSION=`ls /usr/share/jaranalyzer/jaranalyzer-*.jar | \
           sed "s%/usr/share/jaranalyzer/jaranalyzer-\(.*\)\.jar$%\1%"`

  echo "JarAnalyzer $VERSION"
  echo
  echo "Copyright (C) 2005, 2008 Kirk Knoernschild. See the source for"
  echo "copying conditions. There is NO warranty; not even for MERCHANTABILITY"
  echo "or FITNESS FOR A PARTICULAR PURPOSE."
  echo
  echo "Written by Kirk Knoernschild."
}

# Parse options
OPTIONS=`getopt -o hVd -l help,version,dot \
                -n 'jaranalyzer' -- "$@"`

# Did we get an error while parsing options?
if [ $? -ne 0 ]; then
  echo
  syntax
  exit 1
fi

# Interpret options
for OPT in $OPTIONS; do
  case "$OPT" in
    -h|--help) SHOW_HELP=true ; shift ;;
    -V|--version) SHOW_VERSION=true ; shift ;;
    -d|--dot) MAIN_CLASS=$MAIN_CLASS_DOT ; shift ;;
    --) continue ;;
    *) echo "Internal error while parsing options!" ; exit 1 ;;
  esac
done

if [ $SHOW_HELP ]; then
  syntax
  exit 0
fi

if [ $SHOW_VERSION ]; then
  version
  exit 0
fi

# Set the default java executable
if [ -z "$JAVA_CMD" ]; then
  JAVA_CMD=/usr/bin/java
fi

$JAVA_CMD -cp "$CLASSPATH" "$MAIN_CLASS"
