#!/usr/bin/env bash

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

function __usage {
    cat <<-END >&2
    USAGE: $0 [-f JBOSS_FUSE_HOME] [-d JBOSS_FUSE_DATA] [-p JBOSS_FUSE_PIDFILE] [-n JBOSS_FUSE_SERVICE_NAME] [-e JBOSS_FUSE_ENV, ...]
            -f JBOSS_FUSE_HOME          # JBoss Fuse installation path
            -d JBOSS_FUSE_DATA          # JBoss Fuse data path (default to ${JBOSS_FUSE_HOME}/data)
            -c JBOSS_FUSE_CONF          # JBoss Fuse configiration file
            -p JBOSS_FUSE_PIDFILE       # JBoss Fuse pid path (default to ${JBOSS_FUSE_DATA}/$(JBOSS_FUSE_SERVICE_NAME).pid)
            -n JBOSS_FUSE_SERVICE_NAME  # JBoss Fuse service name (default fuse)
            -e JBOSS_FUSE_ENV           # JBoss Fuse environment variable (can be repeated)
            -u JBOSS_FUSE_SERVICE_USER  # JBoss Fuse user
            -l JBOSS_FUSE_CONSOLE_LOG   # JBoss Fuse console log
            -h                          # this usage message
END
    exit
}

CONF_TEMPLATE="fuse-service-template.conf"
SYSTEMD_TEMPLATE="fuse-service-template.systemd"
INIT_REDHAT_TEMPLATE="fuse-service-template.init-redhat"
INIT_DEBIAN_TEMPLATE="fuse-service-template.init-debian"
SOLARIS_SMF_TEMPLATE="fuse-service-template.solaris-smf"

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


JBOSS_FUSE_ENV=()

while getopts f:d:c:p:n:u:e:l:h opt
do
    case $opt in
    f)  export JBOSS_FUSE_HOME=$OPTARG ;;
    d)  export JBOSS_FUSE_DATA=$OPTARG ;;    
    c)  export JBOSS_FUSE_CONF=$OPTARG ;;    
    p)  export JBOSS_FUSE_PIDFILE=$OPTARG ;;
    n)  export JBOSS_FUSE_SERVICE_NAME=$OPTARG ;;
    u)  export JBOSS_FUSE_SERVICE_USER=$OPTARG ;;
    l)  export JBOSS_FUSE_SERVICE_LOG=$OPTARG ;;
    e)  JBOSS_FUSE_ENV+=($OPTARG) ;;
    h|?) usage ;;
    esac
done

shift $(( $OPTIND - 1 ))

if [[ ! $JBOSS_FUSE_HOME ]]; then 
    echo "Warning, JBOSS_FUSE_HOME is required"
    usage
fi

if [[ ! $JBOSS_FUSE_DATA ]]; then 
    export JBOSS_FUSE_DATA=${JBOSS_FUSE_HOME}/data
fi

if [[ ! $JBOSS_FUSE_CONF ]]; then 
    export JBOSS_FUSE_CONF=${JBOSS_FUSE_HOME}/etc/${JBOSS_FUSE_SERVICE_NAME}.conf
fi

if [[ ! $JBOSS_FUSE_SERVICE_NAME ]]; then 
    export JBOSS_FUSE_SERVICE_NAME="fuse"
fi

if [[ ! $JBOSS_FUSE_PIDFILE ]]; then 
    export JBOSS_FUSE_PIDFILE=${JBOSS_FUSE_DATA}/${JBOSS_FUSE_SERVICE_NAME}.pid
fi

if [[ ! $JBOSS_FUSE_SERVICE_LOG ]]; then 
    export JBOSS_FUSE_SERVICE_LOG=${JBOSS_FUSE_DATA}/log/${JBOSS_FUSE_SERVICE_NAME}-console.log
fi

if [[ ! $JBOSS_FUSE_SERVICE_USER ]]; then 
    export JBOSS_FUSE_SERVICE_USER="root"
fi

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

function linux_service {
    perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < $1 > $2

    cat > ${JBOSS_FUSE_CONF} <<EOF
#
# JBoss Fuse
#
EOF

    perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < ${CONF_TEMPLATE} >> ${JBOSS_FUSE_CONF}

    cat >> ${JBOSS_FUSE_CONF} <<EOF
#
# User
#
EOF

    for var in "${JBOSS_FUSE_ENV[@]}"; do
      echo "${var}" >> ${JBOSS_FUSE_CONF}
    done
}

function solaris_service {
    perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < $1 > $2

    #svcbundle \
    #    -o ${JBOSS_FUSE_SERVICE_NAME}.xml \
    #    -s bundle-type=manifest \
    #    -s service-name=application/${JBOSS_FUSE_SERVICE_NAME} \
    #    -s model=daemon \
    #    -s start-method="${JBOSS_FUSE_HOME}/bin/fuse server &amp;" \
    #    -s stop-method=="${JBOSS_FUSE_HOME}/bin/stop"
}

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

case $(uname | tr [:upper:] [:lower:]) in
    sunos)
        # smc vs initv
        solaris_service $SOLARIS_SMF_TEMPLATE ${PWD}/${JBOSS_FUSE_SERVICE_NAME}.xml
        ;;
    linux)
        if [ -d /run/systemd/system ]; then
            linux_service $SYSTEMD_TEMPLATE ${PWD}/${JBOSS_FUSE_SERVICE_NAME}.service
        elif [ -f /etc/redhat-release ]; then
            linux_service $INIT_REDHAT_TEMPLATE {PWD}/${JBOSS_FUSE_SERVICE_NAME}
        elif [ -f /etc/debian-release ]; then
            linux_service $INIT_DEBIAN_TEMPLATE ${PWD}/${JBOSS_FUSE_SERVICE_NAME}
        fi
        ;;
esac
