Better make-dist. Can use custom props
[aquarium] / make-dist.sh
index abccb6d..600d6cd 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/bash
 
 # Copyright 2012 GRNET S.A. All rights reserved.
 #
@@ -42,13 +42,34 @@ SERVER_SCRIPTS_SRC=$WHERE/scripts
 CONF_SRC=$WHERE/src/main/resources
 ARG1="$1"
 
+ERROR=""
+P_VERBOSE="no"
+P_PROPS=""
+P_BUILD="normal"
+P_KEEPDIST="no"
+P_FAKEIT="no"
+
+verbose() {
+  if [ "$P_VERBOSE" = "yes" ]; then
+    echo "$@"
+  fi
+}
+
+verbose_p() {
+  verbose "Build type       :" $P_BUILD
+  verbose "Custom properties:" $P_PROPS
+  verbose "Keep dist/ folder:" $P_KEEPDIST
+  verbose "Fake it          :" $P_FAKEIT
+}
+
 fail() {
     echo "failed while $1"
     exit 1
 }
 
 removedist() {
-  if [ -e $DIST ]; then
+  local KEEP=$1
+  if [ "$KEEP" = "no" -a -e $DIST ]; then
     echo Folder $DIST exists. Removing it.
     rm -rf "$DIST"
   fi
@@ -66,8 +87,7 @@ createdist() {
 }
 
 clean() {
-  local p="$ARG1"
-  if [ -z "$p" ]; then
+  if [ "$P_BUILD" = "normal" ]; then
     echo
     echo "==============================="
     echo "=== mvn clean ================="
@@ -75,7 +95,7 @@ clean() {
     echo
     mvn clean || fail "cleaning compilation artifacts"
     echo
-  elif [ "$p"="dev" -o "$p"="fast" -o "$p"="noclean" ]; then
+  elif [ "$P_BUILD"="fast" ]; then
     echo
     echo "==============================="
     echo "=== NOT executing mvn clean ==="
@@ -107,9 +127,14 @@ collectconf() {
   echo Copying config files from $CONF_SRC
   echo
   cp $CONF_SRC/log4j.properties $DIST/conf|| fail "copying log4j.properties"
-  cp $CONF_SRC/aquarium.properties $DIST/conf || fail "copying aquarium.properties"
   cp $CONF_SRC/policy.yaml $DIST/conf || fail "copying policy.yaml"
   cp $CONF_SRC/roles-agreements.map $DIST/conf || fail "copying roles-agreements.map"
+
+  if [ -n "$P_PROPS" ]; then
+    cp $P_PROPS $DIST/conf/aquarium.properties || fail "copying $P_PROPS"
+  else
+    cp $CONF_SRC/aquarium.properties $DIST/conf || fail "copying aquarium.properties"
+  fi
 }
 
 collectscripts() {
@@ -134,13 +159,72 @@ archive() {
   ls -al $ARC
 }
 
-removedist     && \
-createdist     && \
-clean          && \
-build          && \
-collectdeps    && \
-collectconf    && \
-collectscripts && \
-gitmark        && \
-archive        && \
-removedist
\ No newline at end of file
+usage() {
+  echo "Usage: $0 [options]"
+  echo ""
+  echo "OPTIONS:"
+  echo "  -b TYPE   Use build TYPE. One of 'normal', 'fast'."
+  echo "            'normal' is the default and can be omitted."
+  echo "            'fast' means that it will not run mvn clean."
+  echo "  -k        Keep generated dist folder."
+  echo "  -h        Show this message."
+  echo "  -n        As in make -n."
+  echo "  -p FILE   Use FILE as aquarium.properties."
+  echo "  -v        Be verbose."
+
+  exit 0
+}
+
+while getopts ":b:hkp:nv" opt
+do
+  case $opt in
+    b) P_BUILD=$OPTARG
+    ;;
+    h) usage
+    ;;
+    k) P_KEEPDIST="yes"
+    ;;
+    n) P_FAKEIT="yes"
+    ;;
+    p) P_PROPS=$OPTARG
+    ;;
+    v) P_VERBOSE="yes"
+    ;;
+    :) ERROR="Option -$OPTARG requires an argument. Aborting..."
+    ;;
+    \?) ERROR="Invalid option: -$OPTARG"
+    ;;
+  esac
+done
+
+if [ -n "$ERROR" ]; then
+  echo $ERROR >&2
+  exit 1
+fi
+
+if [ -n "$P_PROPS" -a ! -f "$P_PROPS" ]; then
+  echo $P_PROPS is not a file. Aborting... >&2
+  exit 1
+fi
+
+if [ ! "$P_BUILD" = "normal" -a ! "$P_BUILD" = "fast" ]; then
+  echo Build type must be one of normal, fast. $P_BUILD was given. Aborting... >&2
+  exit 1
+fi
+
+if [ "$P_FAKEIT" = "yes" ]; then
+  P_VERBOSE=yes verbose_p
+  exit 0
+fi
+
+verbose_p         && \
+removedist     no && \
+createdist        && \
+clean             && \
+build             && \
+collectdeps       && \
+collectconf       && \
+collectscripts    && \
+gitmark           && \
+archive           && \
+removedist $P_KEEPDIST
\ No newline at end of file