#!/bin/bash --

#
# Updates the manifest in a jar file to contain a Class-Path attribute
#

set -e
. /usr/share/javahelper/jh_lib.sh

syntax()
{
   echo -e "Usage: jh_manifest [options] [<jar> ...]"
   echo -e "Options:"
   echo -e "\t-h --help: show this text"
   echo -e "\t-v --verbose: show more information while running"
   echo -e "\t-V --version: print the version"
   echo -e "\t-n --no-act: don't actually do anything, just print the results"
   echo -e "When reading manifest files for packages:"
   echo -e "\t-i --indep: run for all Arch: all packages"
   echo -e "\t-s --arch: run for all Arch-specific packages"
   echo -e "\t-p<package> --package=<package>: package to act on (default=all)"  
   echo -e "\t-P<packagedir> --tmpdir=<package>: package directory (default=\$CWD/debian/package)"  
   echo -e "When acting on a jar from the command line:"
   echo -e "\t-c<classpath> --classpath=<classpath>: The classpath to set (space separated)"
   echo -e "\t-j</path/to/java/home> --java-home=</path/to/java/home>: The path to the JRE to use to execute the jar"
   echo -e "\t-m<class> --main=<class>: The class to run when executing the jar"
   echo -e "\t-o<options> --javaopts=<options>: Options passed to java when executing the jar"
   exit 1
}

updatejar()
{
   JAR="`realpath $1`"
   MANIFEST="`realpath $2`"

   if [ ! -f "$MANIFEST" ] ; then
      return;
   fi
   if [ ! -f "$JAR" ] ; then
      echo "Warning: $JAR does not exist"
      return;
   fi

   if [ -n "$VERBOSE" ]; then
      echo -n "Updating manifest in $JAR: "
   fi

   rm -f "$TEMPDIR/META-INF/MANIFEST.MF" 
   ( cd "$TEMPDIR" ; fastjar -x -f "$JAR" META-INF/MANIFEST.MF )
   IFS='
'
   for i in `cat "$MANIFEST"`; do
      KEY="`echo $i | sed 's/^ *\([^:]*\):.*$/\1/'`"
      VALUE="`echo $i | cut -d: -f2`"

      if [ -n "$VERBOSE" ]; then
         echo " $KEY:$VALUE"
      fi

      if grep "^$KEY:" "$TEMPDIR/META-INF/MANIFEST.MF" >/dev/null; then
         sed -i "s%^$KEY:.*$%$KEY:$VALUE%" "$TEMPDIR/META-INF/MANIFEST.MF"
      else
         echo "$KEY:$VALUE" >> "$TEMPDIR/META-INF/MANIFEST.MF"
      fi
   done

   if ! grep "^Class-Path:" "$TEMPDIR/META-INF/MANIFEST.MF" >/dev/null && [ -n "$CLASSPATH" ]; then
      echo $CLASSPATH | sed 's/:/ /g;s/^/Class-Path: /' >> "$TEMPDIR/META-INF/MANIFEST.MF"
   fi

   if [ -n "$VERBOSE" ]; then
      echo ""
   fi

   if [ -n "`getarg n no-act`" ]; then
      cat "$TEMPDIR/META-INF/MANIFEST.MF"
   else
      fastjar -u -f "$JAR" -m "$TEMPDIR/META-INF/MANIFEST.MF"
   fi
}

ARGS="v verbose n no-act i indep s arch p package P tmpdir c classpath j java-home m main o javaopts" parseargs "$@"

TEMPDIR="`mktemp -d`"
OLDDIR="`pwd`"
VERBOSE="`getarg v verbose`"

if [ "$ARGC" != "0" ]; then
   for (( i = 0; i < $ARGC; i++ )); do
      rm -f "$TEMPDIR/newmanifest"
      if [ -n "`getarg c classpath`" ]; then
         echo "Class-Path: `getarg c classpath`" >> "$TEMPDIR/newmanifest"
      fi
      if [ -n "`getarg m main`" ]; then
         echo "Main-Class: `getarg m main`" >> "$TEMPDIR/newmanifest"
      fi
      if [ -n "`getarg j java-home`" ]; then
         echo "Debian-Java-Home: `getarg j java-home`" >> "$TEMPDIR/newmanifest"
      fi
      if [ -n "`getarg o javaopts`" ]; then
         echo "Debian-Java-Parameters: `getarg o javaopts`" >> "$TEMPDIR/newmanifest"
      fi
      updatejar "${ARGV[$i]}" "$TEMPDIR/newmanifest"
   done
   rm -rf "$TEMPDIR"
   exit 0
fi

dh_testdir || (rm -rf "$TEMPDIR"; false)

for p in `findpackages`; do

   PACKAGEDIR="`getarg P tmpdir`"
   if [ -z "$PACKAGEDIR" ]; then
      PACKAGEDIR="`pwd`/debian/$p"
   else
      PACKAGEDIR=`realpath $PACKAGEDIR`
   fi

   if [ -f  "debian/$p.manifest" ]; then
      FILE="debian/$p.manifest"
   elif [ -f debian/manifest ]; then
      FILE=debian/manifest
   elif [ -d "debian/$p" ]; then
      (cd "debian/$p"; find . -name "*.jar" | sed 's/$/:\n/' >> "$TEMPDIR/tmpmanifest")
      FILE="$TEMPDIR/tmpmanifest"
   else
      continue
   fi

   if [ -n "$VERBOSE" ]; then
      echo "Updating jars in $PACKAGEDIR from $FILE"
   fi

   IFS='
'
   JAR=
   rm -f "$TEMPDIR/newmanifest"
   touch "$TEMPDIR/newmanifest"
   for i in `cat "$FILE"`; do
      if [ "$i" == "." ] && [ -s "$TEMPDIR/newmanifest" ] && [ -n "$JAR" ]; then
         updatejar "$PACKAGEDIR/$JAR" "$TEMPDIR/newmanifest"
         JAR=
         rm -f "$TEMPDIR/newmanifest"
         touch "$TEMPDIR/newmanifest"
      elif [ "${i:0:1}" != " " ]; then
         JAR="`echo "$i" | cut -d: -f1`"
      else
         echo $i >> "$TEMPDIR/newmanifest"
      fi
   done
   if [ -n "$JAR" ]; then
      updatejar "$PACKAGEDIR/$JAR" "$TEMPDIR/newmanifest"
   fi
   unset PACKAGEDIR

done
rm -rf "$TEMPDIR"

