#!/bin/bash

BRANCH=${1:-master}

GIT_USER="https://raw.githubusercontent.com/adriansev"
REPO="alicexrd"
LOCATION="${GIT_USER}/${REPO}"


echo "This script will deploy ALICE XRootD settings for the current user"
echo "All previous template configs will be overwritten"
echo -ne "\nSpecify branch/tag; if not used default=master\n>>>>>   ${BRANCH}   <<<<<\n\n"
echo "Press ctrl-c for interrupting; waiting 10 seconds :"

for (( i=1 ; i<12 ; i++)) ; do sleep 1 ; echo -ne "." ; done
echo

CURL='curl -fkLsS -o'

XRDCTLDIR=${XRDCTLDIR:=~/alicexrd}
XRDCONFDIR=${XRDCONFDIR:=~/alicexrdconf}
XRDRUNDIR=${XRDRUNDIR:=~/alicexrdrun}

# create directory for logs and other files
mkdir -p ${XRDCTLDIR} ${XRDRUNDIR} ${XRDCONFDIR}

# Script for starting services
${CURL} ${XRDCTLDIR}/xrdctl          ${LOCATION}/${BRANCH}/xrdctl        && chmod +x ${XRDCTLDIR}/xrdctl
${CURL} ${XRDCTLDIR}/xrd_mk_config   ${LOCATION}/${BRANCH}/xrd_mk_config && chmod +x ${XRDCTLDIR}/xrd_mk_config

# Main configurations
${CURL} ${XRDCONFDIR}/system.cnf_CHANGEME  ${LOCATION}/${BRANCH}/alicexrdconf/system.cnf_CHANGEME

# Common configuration template
${CURL} ${XRDCONFDIR}/xrootd.xrootd.cf.tmp ${LOCATION}/${BRANCH}/alicexrdconf/xrootd.xrootd.cf.tmp