#!/bin/bash

WFCWorkDir=/opt/wfc
WFCBinDir=${WFCWorkDir}/bin
DockerDir=${WFCWorkDir}/docker
DockerCompose=docker-compose
PIDFile=/run/wfccontrol.pid

Jars="/opt/wfc/docker/wfc/auth/jar/wfc-auth.jar 
      /opt/wfc/docker/wfc/gateway/jar/wfc-gateway.jar 
      /opt/wfc/docker/wfc/modules/system/jar/wfc-modules-system.jar 
      /opt/wfc/docker/wfc/modules/user/jar/wfc-modules-user.jar 
      /opt/wfc/docker/wfc/modules/file/jar/wfc-modules-file.jar     
      /opt/wfc/docker/wfc/modules/job/jar/wfc-modules-job.jar 
      /opt/wfc/docker/wfc/modules/payment/jar/wfc-modules-payment.jar
     "

case "$1" in
    start)
        cd ${DockerDir}
        if [ -z "$2" ]; then
            ${DockerCompose} up -d 
            echo $! > ${PIDFile}
        else 
            ${DockerCompose} up -d $2
        fi
        ;;
    stop)
        cd ${DockerDir}
        if [ -z "$2" ]; then
            ${DockerCompose} stop
            if [ -f ${PIDFile} ]; then
                rm ${PIDFile}
            fi
        else 
            ${DockerCompose} stop $2
        fi
        ;;
    restart)
        $0 stop $2
        sleep 1        
        $0 start $2
        ;;    
    status)
        cd ${DockerDir}
        if [ -z "$2" ]; then
            ${DockerCompose} ps
        else 
            ${DockerCompose} ps $2
        fi        
        ;;	
    version)
        cd ${WFCBinDir}
        # java printJarVer ${Jars}
        for jar in ${Jars}; do
            jar_name=$(basename "$jar" .jar)
            version=$(unzip -p "$jar" META-INF/MANIFEST.MF | grep 'Implementation-Version' | awk '{print $2}')
            echo "$jar_name version: $version"
        done
        ;;
    *)
        echo "WANFi Control and Billing System Service ... "
        echo "Usage: $0 start|stop|restart|status|version"
        exit 1
        ;;
esac
