#!/bin/bash



superset_status(){
    result=`ps -ef | awk '/gunicorn/ && !/awk/{print $2}' | wc -l`

    if [[ $result -eq 0 ]]; then
        return 0
    else
     return 1
    fi
    }

superset_start(){
    source ~/.bashrc
    superset_status >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        conda activate superset ; gunicorn --workers 10 --timeout 120 --bind superset:8787  "superset.app:create_app()" --daemon

    else
        echo ""

    echo " -------------------------- SuperSet is Running Now -------------------------"
    fi
}


superset_stop(){
    superset_status >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        echo ""

        echo "############################# SuperSet is not Running #############################"

        echo ""
    else
         ps -ef | awk '/gunicorn/ && !/awk/{print $2}'| xargs kill -9

    fi
}



case $1 in
    start )
        echo ""

        echo "************************** Start SuperSet **************************"

        echo ""
        superset_start
    ;;

    stop )
        echo ""

        echo "~~~~~~~~~~~~~~~~~~~~~~~~~ Stop SuperSet ~~~~~~~~~~~~~~~~~~~~~~~~~"

        echo ""
        superset_stop
    ;;

    restart )
        echo ""

        echo "*~*~*~*~*~*~*~*~*~*~*~*~*~ Restart SuperSet *~*~*~*~*~*~*~*~*~*~*~*~*~"

        echo ""

        superset_stop
        echo ""
        superset_start
    ;;

    status )
        superset_status>/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            echo ""
            echo "~~~~~~~~~~~~~~~~~~~~~~~~~ SuperSet is  not Running ~~~~~~~~~~~~~~~~~~~~~~~~~"
            echo ""
        else
            echo ""
            echo "************************** SuperSet is Running Now **************************"
            echo ""
        fi
     ;;

    * )
        echo "||||||||||||||||||||||||||||| start | stop | status | restart |||||||||||||||||||||||||||||"

esac