#!/bin/bash

MVN_LOCAL="/usr/bin/mvn-local"
JAVA="/usr/bin/java"
WORKSPACE_DIR=".workspace"
JETTY_LOG=".jetty.log"
COMPILE_LOG=".compile.log"
SPEC_FILE="devassistant-jsf-example.spec"
TARBALL_NAME="devassistant-jsf-example.tar.gz"
PROJECT_NAME="devassistant-jsf-example"

help() {
cat << USAGE
Usage: $0 [goal]

List of goals:
    run      compile and run the project (this is default goal)
    eclipse  run Eclipse IDE
    tarball  create tarball
    rpm      create SRPM and RPM packages
    help     show this text
USAGE
}

run () {
    echo -n "[INFO] Compiling... "
    # Build the project
    $MVN_LOCAL clean package > ${COMPILE_LOG}
    if [ $? -eq 0 ]; then
        echo "done."
    else
        echo "failed."
        echo "See ${COMPILE_LOG} for more information"
        exit 1
    fi

    echo -n "[INFO] Starting Jetty Web Server... "
    # Remove original .war archive
    rm -f target/original-*.war
    # Find .war archive
    WAR=`find ./target -name *.war`
    # Start embedded jetty server
    $JAVA -jar ${WAR} 2> ${JETTY_LOG}
}

# run eclipse IDE
run_eclipse() {
    # start eclipse with workspace ./workspace
    # TODO it looks like there is no easy way how to (from command line)
    # automatically import Maven project into the workspace :/
    if [ -d ${WORKSPACE_DIR} ]; then
        mkdir -p ${WORKSPACE_DIR}
        # TODO inform user that he has to import project manually?
    fi
    /usr/bin/eclipse -data ${WORKSPACE_DIR} > /dev/null &
}


create_tarball() {
    echo -n "[INFO] Creating tarball... "
    mkdir ${PROJECT_NAME}
    cd ${PROJECT_NAME}
    ln -s ../src src
    ln -s ../pom.xml pom.xml
    ln -s ../run run
    cd ..
    /usr/bin/tar czhf ${TARBALL_NAME} ${PROJECT_NAME}/
    rm -Rf ${PROJECT_NAME}
    echo "done."
}


create_rpm() {
    # bump spec file (or let user to do that?)
    #/usr/bin/rpmdev-bumpspec -c "New version" -u "`rpmdev-packager`" ${SPEC_FILE}

    # create and clean setup tree
    echo -n "[INFO] Creating directory structure... "
    /usr/bin/rpmdev-setuptree
    rm -Rf ~/rpmbuild/SPECS/*
    rm -Rf ~/rpmbuild/SRPMS/*
    rm -Rf ~/rpmbuild/RPMS/*
    rm -Rf ~/rpmbuild/SOURCES/*
    echo "done."

    # create tarball
    create_tarball

    # copy tarball and spec file
    echo -n "[INFO] Building SRPM... "
    cp *.spec ~/rpmbuild/SPECS/
    cp *.tar.gz ~/rpmbuild/SOURCES/

    # build SRPM and copy it into the current directory
    /usr/bin/rpmbuild -bs ${SPEC_FILE} > /dev/null
    cp ~/rpmbuild/SRPMS/* ./
    echo "done."

    echo -n "[INFO] Building RPM... "
    /usr/bin/rpmbuild -bb ${SPEC_FILE}
    if [ $? -eq 0 ]; then
        cp ~/rpmbuild/RPMS/noarch/* ./
        echo "done."
    else
        echo "failed."
    fi

    echo -n "[INFO] Cleaning... "
        rm -Rf ~/rpmbuild/
    echo "done."
}

# default goal is "run"
if [ $# -lt 1 ]; then
    run
    exit 0
fi

case $1 in
run)
    run
    ;;
eclipse)
    run_eclipse
    ;;
tarball)
    create_tarball
    ;;
rpm)
    create_rpm
    ;;
help)
    help
    ;;
*)
    echo "Unknown parameter: $1"
    help
    exit 1
    ;;
esac

exit 0

