#!/bin/bash
#
# Since: May, 2018
# Author: dongbo.xiao@oracle.com
# Description: script to build a Docker image for Apache HTTP Server. The install mode is "standalone" i.e. Apache is not managed by or registered to an Oracle WebLogic Server domain
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2016, 2020, Oracle and/or its affiliates.
#
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
#
usage() {
cat << EOF

Usage: buildDockerImage.sh -v [version] [-s]
Builds a Docker Image for Apache HTTP Server (standalone) .

Parameters:
   -v: Release version to build. Required. E.g 12.2.1.3.0 or 12.2.1.4.0 (default version : 12.2.1.3.0)
   -s: skips the MD5 check of packages

LICENSE Universal Permissive License v1.0

Copyright (c) 2016, 2020, Oracle and/or its affiliates.


EOF
exit 0
}


# Validate packages
checksumPackages() {
  echo "Checking if required packages are present and valid..."
   md5sum -c fmw_${VERSION}_wlsplugins_Disk1_1of1.zip.download
  if [ "$?" -ne 0 ]; then
    echo "MD5 for required packages to build this image did not match!"
    echo "Make sure to download missing files in folder dockerfiles. See *.download files for more information"
    exit $?
  fi
}


#Default value of Parameters 
VERSION="12.2.1.3.0"
SKIPMD5=0

while getopts "hsdgiv:" optname; do
  case "$optname" in
    "h")
      usage
      ;;
    "s")
      SKIPMD5=1
      ;;
    "v")
      VERSION="$OPTARG"
      ;;
	 *)
    # Should not occur
      echo "Unknown error while processing options inside buildDockerImage.sh"
      ;;
  esac
done


# Validate Versions, supported versions 12.2.1.3.0 and 12.2.1.4.0

versionOK=false
if [ "${VERSION}" = "NONE" ]; then
   VERSION="12.2.1.3.0"
   versionOK=true
else
  if [ ${VERSION} = 12.2.1.3.0 -o ${VERSION} = 12.2.1.4.0 ]; then
       versionOK=true
  fi
fi

# Confirm Versions Before starting the Build
if [ "${versionOK}" = "false" ]; then
  echo "ERROR: Incorrect version ${VERSION} specified"
  usage
fi

if [ ! "$SKIPMD5" -eq 1 ]; then
  checksumPackages
else
  echo "Skipped MD5 checksum."
fi

# Apache Image Name
IMAGE_NAME="oracle/apache:${VERSION%??}"

# Build  Arg Version
ARG_VERSION=" --build-arg VERSION=${VERSION}"

# Proxy settings
PROXY_SETTINGS=""
if [ "${http_proxy}" != "" ]; then
  PROXY_SETTINGS="$PROXY_SETTINGS --build-arg http_proxy=${http_proxy}"
fi

if [ "${https_proxy}" != "" ]; then
  PROXY_SETTINGS="$PROXY_SETTINGS --build-arg https_proxy=${https_proxy}"
fi

if [ "${ftp_proxy}" != "" ]; then
  PROXY_SETTINGS="$PROXY_SETTINGS --build-arg ftp_proxy=${ftp_proxy}"
fi

if [ "${no_proxy}" != "" ]; then
  PROXY_SETTINGS="$PROXY_SETTINGS --build-arg no_proxy=${no_proxy}"
fi

if [ "$PROXY_SETTINGS" != "" ]; then
  echo "Proxy settings were found and will be used during build."
fi

# ################## #
# BUILDING THE IMAGE #
# ################## #
echo "Building image '$IMAGE_NAME' ..."
echo "Proxy Settings '$PROXY_SETTINGS'"
echo "Build Arg Version '$ARG_VERSION'"
# BUILD THE IMAGE (replace all environment variables)
BUILD_START=$(date '+%s')
docker build  --force-rm=true --no-cache=true $PROXY_SETTINGS -t $IMAGE_NAME  $ARG_VERSION -f Dockerfile . || {
  echo "There was an error building the image."
  exit 1
}
BUILD_END=$(date '+%s')
BUILD_ELAPSED=`expr $BUILD_END - $BUILD_START`

echo ""

if [ $? -eq 0 ]; then
cat << EOF
  Apache Standalone Docker Image for version: $VERSION is ready to be used.

    --> $IMAGE_NAME

  Build completed in $BUILD_ELAPSED seconds.

EOF
else
  echo "Apache Docker Image was NOT successfully created. Check the output and correct any reported problems with the docker build operation."
fi
