#!/bin/bash
MYDIR=`dirname $0` && [ ! `echo "$0" | grep '^\/'` ] && MYDIR=`pwd`/$MYDIR

DATADIR=/opt/data

# Container
PORT=8080
IMAGE=nmrprocflow/nmrprocflow
CONTAINER=npflow
CONF=$MYDIR/npflow.conf

CMD=$1

# If you use a named volume, (assumes that your docker version >= 1.9)
# - First you have to create the /opt/data volume
# sudo docker create -v /opt/data --name npflow_data_volume ubuntu
# - Second, uncomment the line below, and
#   comment the line with 'VOLS' specified further with a local directory .
#VOLS="--volumes-from npflow_data_volume"

# If you use a local directory, first you have to create the /opt/data directory
VOLS="-v $DATADIR:/opt/data"

usage() { echo "usage: sh $0 start|stop|ps|restart|logs|update";  exit 1; }

case "$CMD" in
   start)
        # run NMRProcFlow
        sudo docker run -d --env-file $CONF $VOLS -p $PORT:80 --name $CONTAINER $IMAGE

        # show Logs
        sudo docker logs $CONTAINER
        ;;
   stop)
        sudo docker rm -f $CONTAINER
        ;;
   restart)
        ( sh $0 stop; sh $0 start)
        ;;
   logs)
        sudo docker logs $CONTAINER
        ;;
   ps)
        sudo docker ps | head -1
        sudo docker ps | grep "nmrprocflow/"
        ;;
   update)
        sudo docker pull $IMAGE
        ;;
   *) usage
      exit 2
esac
