#!/bin/bash --

# options:
#    make, maven, ant, byhand
#    app, library
#    gcj

COMPAT=8
STANDARDS=3.9.3

set -e

. /usr/share/javahelper/jh_lib.sh

syntax()
{
   echo -e "Usage: jh_makepkg [options]"
   echo -e "Options:"
   echo -e "\t-h --help: show this text"
   echo -e "\t-V --version: show the version"
   echo -e "\t-p<name> --package=<name>: Set the name of the package (defaults to directory name without version)"
   echo -e "\t-m<name> --maintainer=<name>: Set the maintainer name (defaults to DEBFULLNAME, NAME, or the user's full name)"
   echo -e "\t-e<email> --email=<email>: Set the maintainer email (defaults to DEBEMAIL EMAIL or user@host)"
   echo -e "\t-v<version> --upstream=<version>: Set the upstream version (defaults to the directory name after a -)"
   echo -e "\t-c --clean: Tidy up common problems with Java source packages. Removes .class, .jar files and pre-built javadoc trees before creating the orig.tar.gz"
   echo -e "\tPick One:"
   echo -e "\t-l --library: This is a library package"
   echo -e "\t-a --app: This is an application package"
   echo -e "\tPick One:"
   echo -e "\t-t --ant: Builds with ant"
   echo -e "\t-M --maven: Builds with maven (deprecated, use mh_make instead)"
   echo -e "\t-k --makefiles: Builds with make"
   echo -e "\t-n --none: Create own buildsystem"
   echo -e "\tPick One:"
   echo -e "\t-g --gcj: Build with gcj"
   echo -e "\t-d --default: Build with default free platform compiler"
   echo -e "\t-o --openjdk: Build with openjdk 6"
   echo -e "\t-o7 --openjdk7: Build with openjdk 7"
   echo -e "Environment Variables:"
   echo -e "\tDEBEMAIL: Default maintainer email address"
   echo -e "\tEMAIL: Default maintainer email address"
   echo -e "\tDEBFULLNAME: Default maintainer name"
   echo -e "\tNAME: Default maintainer name"
   exit 1
}

maven_not_supported()
{
    echo "Sorry, but jh_makepkg is not good at generating package templates" >&2
    echo "for maven based packages." >&2
    echo >&2
    echo "Please consider using mh_make instead" >&2

    exit 1
}

ARGS="p package m maintainer e email v upstream l library a app t ant k makefile n none g gcj o openjdk o7 openjdk7 M maven d default" parseargs "$@"

if [ -n "`getarg M maven`" ]; then
    maven_not_supported
fi

BUILDDIR="`pwd`"

if [ -z "`getarg v upstream`" ]; then
   VERSION="`basename "$BUILDDIR" | sed -n 's/^.*-//p'`"
else
   VERSION="`getarg v upstream`"
fi
if [ -z "`getarg p package`" ]; then
   PACKAGE="`basename "$BUILDDIR" | sed -n 's/\(-.*\)\{0,1\}$//p'`"
else
   PACKAGE="`getarg p package`"
fi

if [ -z "$VERSION" ]; then
   echo "Can't determine version from directory name `basename "$BUILDDIR"`, maybe it is not in the form package-version?"
   exit 1
fi

if [ -z "$PACKAGE" ]; then
   echo "Can't determine package name from directory name `basename "$BUILDDIR"`, maybe it is not in the form package-version?"
   exit 1
fi

if [ ! -f ../${PACKAGE}_${VERSION}.orig.tar.gz ]; then

   if [ -n "`getarg c clean`" ]; then
      echo "Cleaning up source tree before creating orig.tar.gz"
      find . -name '*.class' -print0 | xargs -0 rm -f
      find . -name '*.jar' -print0 | xargs -0 rm -f
      IFS='
'
      for doctree in `find . -name allclasses-frame.html`; do
         TREE="`dirname $doctree`"
         rm -rf "$TREE"/*
      done
      find * -type d -print0 | xargs -0 rmdir -p --ignore-fail-on-non-empty
   fi

   echo "Creating package $PACKAGE version $VERSION."
   echo 
   echo "Building from source in $BUILDDIR"

   cd ..
   echo "Creating orig source tarball: ${PACKAGE}_${VERSION}.orig.tar.gz"
   tar zcf "${PACKAGE}_${VERSION}.orig.tar.gz" "`basename "$BUILDDIR"`"
   cd "$BUILDDIR"

fi

if [ -n "`getarg e email`" ]; then
   DEBEMAIL="`getarg e email`"
fi

if [ -z "$DEBEMAIL" ]; then 
   DEBEMAIL="$EMAIL"
fi

if [ -z "$DEBEMAIL" ] && [ -f /etc/mailname ]; then 
   DEBEMAIL="`whoami`@`cat /etc/mailname`"
fi

if [ -z "$DEBEMAIL" ]; then 
   DEBEMAIL="`whoami`@`hostname --fqdn`"
fi

if [ -n "`getarg m maintainer`" ]; then
   DEBFULLNAME="`getarg m maintainer`"
fi

if [ -z "$DEBFULLNAME" ]; then 
   DEBFULLNAME="$NAME"
fi

if [ -z "$DEBFULLNAME" ] ; then 
   DEBFULLNAME="$(getent passwd $(whoami) | cut -d: -f5 | cut -d, -f1)"
fi

echo "Packager: $DEBFULLNAME <$DEBEMAIL>"
echo

if [ -n "`getarg a app`" ]; then
   TYPE="app"
elif [ -n "`getarg l library`" ]; then
   TYPE="lib"
else
   echo "What type of package is it? Application, or Library?"
   echo "Select:"
   echo -e "\t[A] Application (Default)"
   echo -e "\t[L] Library"
   echo -n "[Al] $ "
   read t
   echo
   case $t in
      "L"|"l")
         TYPE="lib"
         echo "Selected: Library"
         ;;
      *)
         TYPE="app"
         echo "Selected: Application"
         ;;
   esac    
fi
case $TYPE in
   "app")
      SECTION=misc
      SRCPACKAGE="$PACKAGE"
      BINPACKAGE="$PACKAGE"
   ;;
   "lib")
      SECTION=java
      SRCPACKAGE="$PACKAGE"
      BINPACKAGE="$PACKAGE"
      if ! echo $BINPACKAGE | grep "^lib" >/dev/null; then
         BINPACKAGE="lib$BINPACKAGE"
      fi
      if ! echo $BINPACKAGE | grep -- "-java$" >/dev/null; then
         BINPACKAGE="$BINPACKAGE-java"
      fi
   ;;
esac

if [ -n "`getarg t ant`" ]; then
   BUILD="ant"
elif [ -n "`getarg k makefiles`" ]; then
   BUILD="make"
elif [ -n "`getarg n none`" ]; then
   BUILD="byhand"
else
   echo "What type of build system does it have? Ant, Makefiles, or None?"
   echo "Select:"
   echo -e "\t[A] Ant"
   echo -e "\t[M] Makefiles"
   echo -e "\t[V] Maven"
   echo -e "\t[N] None---make one for me (Default)"
   echo -n "[Namv] $ "
   read t
   echo
   case $t in
      "A"|"a")
         BUILD="ant"
         echo "Selected: Ant"
         ;;
      "M"|"m")
         BUILD="make"
         echo "Selected: Makefiles"
         ;;
      "V"|"v")
         BUILD="maven"
         echo "Selected: Maven"
         ;;
      *)
         BUILD="byhand"
         echo "Selected: No upstream build system"
         ;;
   esac
fi
case $BUILD in
   "make")
   ;;
   "ant")
      DEPENDS="$DEPENDS, ant"
   ;;
   "maven")
        maven_not_supported
   ;;
   "byhand")
   ;;
esac

if [ -n "`getarg d default`" ]; then
   COMP="default"
elif [ -n "`getarg g gcj`" ]; then
   COMP="gcj"
elif [ -n "`getarg o openjdk`" ]; then
   COMP="open"
elif [ -n "`getarg o7 openjdk7`" ]; then
   COMP="open7"
else
   echo "Which Java runtime does it need? Which free runtime?"
   echo "Select:"
   echo -e "\t[F] Default Free compiler/runtime (Default)"
   echo -e "\t[G] GCJ"
   echo -e "\t[o] OpenJDK 6"
   echo -e "\t[o7] OpenJDK 7"
   echo -n "[FGoo7] $ "
   read t
   echo
   case $t in
      "O"|"o")
         COMP="open"
         echo "Selected: OpenJDK 6"
         ;;
      "O7"|"o7")
         COMP="open7"
         echo "Selected: OpenJDK 7"
         ;;
      "g"|"G")
         COMP="gcj"
         echo "Selected: GCJ"
         ;;
      *)
         COMP="default"
         echo "Selected: Default Free compiler"
         ;;
   esac
fi

case $COMP in
   "gcj")
      COMPILER=gcj-jdk
      JAVA_HOME=/usr/lib/jvm/java-gcj
   ;;
   "default")
      COMPILER=default-jdk
      JAVA_HOME=/usr/lib/jvm/default-java
   ;;
   "open")
      COMPILER=openjdk-6-jdk
      JAVA_HOME="/usr/lib/jvm/java-6-openjdk-\$(shell dpkg-architecture -qDEB_HOST_ARCH)"
      JVM=open
   ;;
   "open7")
      COMPILER=openjdk-7-jdk
      JAVA_HOME="/usr/lib/jvm/java-7-openjdk-\$(shell dpkg-architecture -qDEB_HOST_ARCH)"
      JVM=open7
   ;;
esac

YEAR=$(date +%Y)
mkdir -p debian
mkdir debian/source 
echo '3.0 (quilt)' > debian/source/format
cat > debian/control <<END
Source: $SRCPACKAGE
Section: ${CONTRIB}$SECTION
Priority: optional
Maintainer: $DEBFULLNAME <$DEBEMAIL>
Build-Depends: debhelper (>= $COMPAT), $COMPILER, javahelper (>= $JAVATOOLS_VERSION) $DEPENDS
Standards-Version: $STANDARDS
Homepage: <homepage>

Package: $BINPACKAGE
Architecture: all
Depends: \${java:Depends}, \${misc:Depends}
Recommends: \${java:Recommends}
Description: Short Description
 Long Description
END

if [ "$TYPE" = "lib" ] ; then
    # add doc package template
    cat >> debian/control <<END

Package: $BINPACKAGE-doc
Architecture: all
Section: doc
Depends: \${java:Depends}, \${misc:Depends}
Recommends: \${java:Recommends}
Description: Short Description - doc
 Long Description
 .
 This package contains the Javadoc API
END

fi

cat > debian/copyright <<END
Format: [URI OF THE FORMAT SPECIFICATION, SUCH AS http://www.debian.org/doc/packaging-manuals/copyright-format/<VERSION>/]
Upstream-Name: [THE NAME UPSTREAM USES FOR THE SOFTWARE]
Upstream-contact: [THE PREFERRED ADDRESS(ES) TO REACH THE UPSTREAM PROJECT]
Source: [AN EXPLANATION FROM WHERE THE UPSTREAM SOURCE CAME FROM. TYPICALLY AN URL]
[OTHER FIELDS]

Files: *
Copyright: Copyright $YEAR John Doe <jdoe@example.com>
License: [STANDARD ABBREVIATION -- SEE "Short names" SECTION OF SPECIFICATION]
 [LICENSE TEXT]

Files: debian/*
Copyright: Copyright $YEAR $DEBFULLNAME <$DEBEMAIL>
License: [STANDARD ABBREVIATION]
 [LICENSE TEXT]

[OTHER FILE PARAGRAPHS]
END

echo $COMPAT > debian/compat

EDITOR=true DEBFULLNAME="$DEBFULLNAME" DEBEMAIL="$DEBEMAIL" dch --create --package $SRCPACKAGE --newversion ${VERSION}-1 --distribution unstable --urgency low 

cat > debian/rules <<END
#!/usr/bin/make -f

export JAVA_HOME=${JAVA_HOME}

# Put depended upon jars in here
# export CLASSPATH=

%:
	dh \$@ --with javahelper

END
      case $BUILD in
         "ant")
         ;;
         "maven")
      cat >> debian/rules <<END
override_dh_auto_build:
	# Build the package
	mvn-debian build
override_dh_auto_clean:
	mvn-debian clean
END
         ;;
         "make")
         ;;
         "byhand")
		echo ${SRCPACKAGE}.jar src > debian/javabuild
         ;;
      esac
   
case $TYPE in
   "app")
      echo "$SRCPACKAGE.jar usr/share/$BINPACKAGE" > debian/$BINPACKAGE.install
      cat >> debian/$BINPACKAGE.manifest <<END
usr/share/$BINPACKAGE/$SRCPACKAGE.jar:
 Main-Class: <Main Class>
 Debian-Java-Home: $JAVA_HOME
END
      echo "usr/share/$BINPACKAGE/$SRCPACKAGE.jar usr/bin/$SRCPACKAGE" > debian/$BINPACKAGE.links
   ;;
   "lib")
      echo "$SRCPACKAGE.jar" > debian/$BINPACKAGE.jlibs
      if [ "$BUILD" = "byhand" ]; then
	  echo "internal" > debian/$BINPACKAGE-doc.javadoc
      else
	  echo "<javadoc build dir here>" > debian/$BINPACKAGE-doc.javadoc
      fi
   ;;
esac

chmod +x debian/rules

echo "Packaging created. You will have to edit most or all of the files in debian/ before it works"

