#!/usr/bin/env bash

#export get_ip_address
. /opt/adb/openshift/get_ip_address

# Prepare, configure and start OpenShift

set -o pipefail
set -o nounset

export ORIGIN_DIR="/var/lib/openshift"
export OPENSHIFT_DIR=${ORIGIN_DIR}/openshift.local.config/master
export KUBECONFIG=${OPENSHIFT_DIR}/admin.kubeconfig
eval "OPENSHIFT_SUBDOMAIN=${OPENSHIFT_SUBDOMAIN}"

# Get machine IP address
ip=$(get_ip_address)


########################################################################
# Helper function to start OpenShift as container
#
# All passed paramters are passed through ti
########################################################################
start_openshift() {
        docker run $1 --name "openshift" --privileged --net=host --pid=host \
        -w ${ORIGIN_DIR} \
        -e "KUBECONFIG=${OPENSHIFT_DIR}/admin.kubeconfig" \
        -v /:/rootfs:ro \
        -v /var/run:/var/run:rw \
        -v /sys:/sys:ro \
        -v /var/lib/docker:/var/lib/docker:rw \
        -v ${ORIGIN_DIR}/openshift.local.volumes:${ORIGIN_DIR}/openshift.local.volumes:z \
        -v ${ORIGIN_DIR}/openshift.local.config:${ORIGIN_DIR}/openshift.local.config:z \
        -v ${ORIGIN_DIR}/openshift.local.etcd:${ORIGIN_DIR}/openshift.local.etcd:z \
        $IMAGE start "${@:2}"
}

########################################################################
# Helper function to remove existing openshift container
########################################################################
rm_openshift_container() {
    if docker inspect openshift &>/dev/null; then
        docker rm -f -v openshift > /dev/null 2>&1
    fi
}

########################################################################
# Helper function to wait for OpenShift config file generation
########################################################################
wait_for_config_files() {
  echo "[INFO] Waiting for OpenShift config files to be created"
  for i in {1..6}; do
    if [ ! -f ${1} ] || [ ! -f ${2} ]; then
      echo "[INFO] ..."
      sleep 5
    else
      break
    fi
  done
  if [ ! -f ${1} ] || [ ! -f ${2} ]; then
    >&2 echo "[ERROR] Unable to create OpenShift config files"
    docker logs openshift
    exit 1
  fi
}

########################################################################
# Main
########################################################################

# First start OpenShift to just write the config files
master_config=${OPENSHIFT_DIR}/master-config.yaml
node_config=${ORIGIN_DIR}/openshift.local.config/node-$(hostname)/node-config.yaml

if [ ! -f $master_config ]; then

    # Prepare directories for bind-mounting
    dirs=(openshift.local.volumes openshift.local.config openshift.local.etcd)
    for d in ${dirs[@]}; do
            mkdir -p ${ORIGIN_DIR}/${d} && chcon -Rt svirt_sandbox_file_t ${ORIGIN_DIR}/${d}
    done

    echo "[INFO] Preparing OpenShift config"
    start_openshift -d --write-config=${ORIGIN_DIR}/openshift.local.config > /dev/null 2>&1
    wait_for_config_files ${master_config} ${node_config}

    echo "Create openshift-dev and admin User"
    htpasswd -b -c ${OPENSHIFT_DIR}/user.htpasswd openshift-dev devel
    htpasswd -b ${OPENSHIFT_DIR}/user.htpasswd admin admin

    # Now we need to make some adjustments to the config
    DIR_EXPAND="\/var\/lib\/openshift\/openshift.local.config\/master"
    echo "[INFO] Configuring OpenShift via ${master_config}"
    sed -i.orig -e "s/\(.*subdomain:\).*/\1 $OPENSHIFT_SUBDOMAIN/" ${master_config} \
    -e "s/\(.*masterPublicURL:\).*/\1 https:\/\/$ip:8443/g" \
    -e "s/\(.*publicURL:\).*/\1 https:\/\/$ip:8443\/console\//g" \
    -e "s/\(.*assetPublicURL:\).*/\1 https:\/\/$ip:8443\/console\//g" \
    -e "s/\(.*\)anypassword/\1 htpasswd/" \
    -e "s/\(.*\)kind: AllowAllPasswordIdentityProvider/\1kind: HTPasswdPasswordIdentityProvider\n\1file: ${DIR_EXPAND}\/user.htpasswd/" \
    -e "/corsAllowedOrigins:/,/- localhost/c\corsAllowedOrigins:\n- .*"

    # Remove the container
    rm_openshift_container
fi

# Now we start the server pointing to the prepared config files
echo "[INFO] Starting OpenShift server"
start_openshift '' --master-config="${master_config}" --node-config="${node_config}"
