#!/bin/bash
###############################################
# Script to build an Palladium app docker image
# Assumption: palladium-base image available
###############################################

# Usage info
usage() {
cat << EOF
Usage: ${0##*/} [-d] PATH_TO_APP BASE_IMAGE_NAME IMAGE_NAME

Create a Docker image for a Palladium application that lives in
PATH_TO_APP.  BASE_IMAGE_NAME is the Docker image that we'll base our
image on, while IMAGE_NAME is the name that you want to use for the
Docker container that I'll create.

Example values:
  - PATH_TO_APP: .../examples/iris/
  - BASE_IMAGE_NAME: ottogroup/palladium-base:1.1.0.1
  - IMAGE_NAME: myname/my-palladium-app:1.0

Options:
    -h          display this help and exit
    -d          create the Dockerfile files but do not build them
EOF
}

# Initialize our own variables:
dry_run=0

OPTIND=1
while getopts "hd" opt; do
    case "$opt" in
        h)
            usage
            exit 0
            ;;
        d)  dry_run=1
            ;;
        '?')
            usage >&2
            exit 1
            ;;
    esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.

if [ "$3" = "" ]; then
    usage >&2
    exit 1
fi

# Copy files into same directory as Dockerfile 
tar cvzf app.tar.gz --exclude .git --directory=$1 .


# Get folder name and modify path:
# /path/to/folder ->  /path/to/folder/
f=${1##*/}
LEN=$(echo ${#f})
if [ $LEN -lt 1 ]; then
    f=${1%*/}
    f=${f##*/}
    p=$1
else
   p=$1
fi


# Write Dockerfile
FILE="Dockerfile"

/bin/cat <<EOM >$FILE-app
#############################################################
# Dockerfile to build palladium app
# Based on palladium-base image
#############################################################

# Set the base image to $2
FROM $2

# Copy file
# COPY $f /root/palladium/app
ADD app.tar.gz /root/palladium/app

#####################################################
# If you want to add conda channels, please add here
# Example: RUN conda config --add channels <channel>



#####################################################

ENV LANG C.UTF-8 

# Set Workdir
WORKDIR /root/palladium/app
EOM



# Install dependencies if needed. Look for directory "python_packages"
if [ -f $p\requirements.txt ]; then 
    echo "RUN conda install --yes --file requirements.txt" >> $FILE-app
fi

if [ -d $p\python_packages ]; then 
    FILES=$1python_packages/* 
    echo "RUN cd python_packages \ " >> $FILE-app
    for f in $FILES 
    do
       f="${f##*python_packages/}"
       fname="${f%.tar.gz}"
       echo " && tar -xvf $f && rm $f &&  cd $fname && python setup.py install && cd .. && rm -r $fname \  " >> $FILE-app
    done 
    echo " && echo 'Done installing packages' " >> $FILE-app
fi

/bin/cat <<EOM >>$FILE-app
# Set PALLADIUM_CONFIG 
ENV PALLADIUM_CONFIG /root/palladium/app/config.py
EOM

if [ -f $p\setup.py ]; then 
    # Install app
    echo "RUN  python setup.py install" >>$FILE-app
fi

# Build image
if [ "$dry_run" -eq 0 ]; then
    sudo docker build -f $FILE-app -t $3 .
    rm app.tar.gz
fi




#########################################
# Build gunicorn server image on top 
# of the app image
#########################################


/bin/cat <<EOM >$FILE-predict
############################################################
# Dockerfile to build palladium with gunicorn autostart
# Based on $3
############################################################

FROM $3

# File Author / Maintainer
MAINTAINER Palladium

RUN pld-fit

RUN conda install --yes gunicorn

# For Postgres support
# RUN apt-get update && apt-get install -y libpq-dev

EXPOSE 8000


EOM

echo "CMD gunicorn --workers=3 -b 0.0.0.0:8000  palladium.wsgi:app" >>$FILE-predict

# Build image
if [ "$dry_run" -eq 0 ]; then
    sudo docker build -f $FILE-predict -t ${3%%:*}-predict:${3##*:} .;
fi
