#!/bin/bash

PX_DOCKER_IMAGE=portworx/px-enterprise:2.XX.X
STORE_VERSION=px-storev2

function usage()
{
echo "Install Portworx as a witness node"
echo ""
echo "./install-witness.sh"
echo "--help"
echo "--cluster-id=$CLUSTER_ID"
echo "--etcd=$ETCD"
echo "--docker-image=$PX_DOCKER_IMAGE"
echo "--store-version=$STORE_VERSION"
echo "--ca=<file>     (only supply when using secure etcd)"
echo "--cert=<file>   (only supply when using secure etcd)"
echo "--key=<file>    (only supply when using secure etcd)"
echo
echo -e "\t* external etcd (--etcd) format: etcd:<end-point-URL:port>,etcd<end-point-URL:port>,..."
echo -e "\n\t--store-version=<version>  Specify the datastore to use (optional, default: px-storev2)"
echo -e "\n\t--ca=<file>                Specify location of CA file for ETCD authentication"
echo -e "\t--cert=<file>              Specify location of certificate for ETCD authentication"
echo -e "\t--key=<file>               Specify location of certificate key for ETCD authentication"
}

while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
--help)
usage
exit
;;
--cluster-id)
CLUSTER_ID=$VALUE
;;
--docker-image)
PX_DOCKER_IMAGE=$VALUE
;;
--etcd)
ETCD=$VALUE
;;
--store-version)
STORE_VERSION=$VALUE
;;
--ca)
CAFILE=$VALUE
;;
--cert)
CERTFILE=$VALUE
;;
--key)
KEYFILE=$VALUE
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
# Once PX is up and running place the witness node in maintenance.
esac
shift
done

if [ -z "${CLUSTER_ID}" -o -z "${PX_DOCKER_IMAGE}" -o -z "${ETCD}" ]; then
    echo "ERROR: requires parameter missing"
    usage
    exit 1
fi

# check store version
if [ "${STORE_VERSION}" != "px-storev1" -a "${STORE_VERSION}" != "px-storev2" ]; then
    echo "WARNING: invalid store version \"${STORE_VERSION}\". Valid values are 'px-storev1' or 'px-storev2'."
    usage
    exit 1
fi

# check certificate files if provided
if [ -n "${CAFILE}" -o -n "${CERTFILE}" -o -n "${KEYFILE}" ]; then
    if [ -z "${CAFILE}" -o -z "${CERTFILE}" -o -z "${KEYFILE}" ]; then
	echo "ERROR: missing certificate parameter. All certificate file locations must be provided."
	usage
	exit 1
    fi

    if [ ! -s "${CAFILE}" -o ! -s "${CERTFILE}" -o ! -s "${KEYFILE}" ]; then
	echo "ERROR: A certificate file provided is inaccessible, does not exist or is empty." && exit 1
    fi

    CERTPARAM="-ca ${CAFILE} -cert ${CERTFILE} -key ${KEYFILE}"
fi

# check etcd format and proto
ETCD_ERR=0
for endpt in ${ETCD//,/ }; do
    ETCD_PROTO="${endpt:5:5}"

    if [ "${ETCD_PROTO}" != "http:" -a "${ETCD_PROTO}" != "https" ]; then
	echo "Error: Invalid etcd end point format: ${endpt}.  Should be 'etcd:<end-point-URL:port>'."
	echo "For example: 'etcd:https://etcd1.example.com:3128,etcd:https://etcd2.example.com...'"
	ETCD_ERR=1
    else
	PROTO_ERR=0
	if [ -n "${CERTPARAM}" ]; then
	    [ "${ETCD_PROTO}" != "https" ] && EPROTO="https" && PROTO_ERR=1
	elif [ "${ETCD_PROTO}" != "http:" ]; then
	    EPROTO="http" && PROTO_ERR=1
	fi
	[ ${PROTO_ERR} -eq 1 ] && echo "Error: Invalid etcd end point proto: ${endpt}.  Should be '${EPROTO}." && ETCD_ERR=1
    fi
done
[ ${ETCD_ERR} -eq 1 ] && exit 1

sudo docker run --entrypoint /runc-entry-point.sh \
--rm -i --privileged=true \
-v /opt/pwx:/opt/pwx -v /etc/pwx:/etc/pwx \
$PX_DOCKER_IMAGE

if [ "${STORE_VERSION}" == "px-storev2" ]; then
    sudo /opt/pwx/bin/px-runc install -k $ETCD -c $CLUSTER_ID --cluster_domain witness -z ${CERTPARAM} -T ${STORE_VERSION} -rt_opts small_conf=1
else
    sudo /opt/pwx/bin/px-runc install -k $ETCD -c $CLUSTER_ID --cluster_domain witness -z ${CERTPARAM}
fi

sudo systemctl daemon-reload
sudo systemctl enable portworx
sudo systemctl start portworx

echo "Use the following command to monitor the status of the witness node:"
echo "\"watch -n 1 --color /opt/pwx/bin/pxctl --color status\""
