#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with this
# work for additional information regarding copyright ownership. The ASF
# licenses this file to You under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

# This script is used for starting and stopping HugeGraphServer easily.
# We could copy this file under '/usr/bin' to use it globally

# Note: user must set a absolute path below
INSTALL_DIR=
SERVER_PORT=

INSTALL_DIR=${INSTALL_DIR:?"Please open the script then set variable 'INSTALL_DIR' manually"}
SERVER_PORT=${SERVER_PORT:?"Please open the script then set variable 'SERVER_PORT' manually"}

BIN_DIR=$INSTALL_DIR/bin
SERVER_URL="http://localhost:${SERVER_PORT}"
DETECT_URL="$SERVER_URL/versions"
EXIT=1

# Start the HugeGraphServer
start() {
    # Verify if the service is running
    if get_status; then
        echo "The graph server is already running"
        exit 0
    else
        # Run the service
        $BIN_DIR/start-hugegraph.sh

        # sleep time before the service verification
        #sleep 10

        # Verify if the service is running
        if get_status; then
            echo "Graph server was successfully started"
            exit 0
        else
            echo "Failed to start graph server"
            exit 1
        fi
    fi
}

# Stop the MATH
stop() {
    # Verify if the service is running
    if get_status; then
        # Stop the service
        $BIN_DIR/stop-hugegraph.sh

        # Verify if the service is running
        if get_status; then
            echo "Failed to stop service"
        else
            echo "Service was successfully stopped"
        fi
    else
        echo "The service is already stopped"
    fi

    if [[ $EXIT -eq 1 ]]; then
        exit 0
    fi
}

# Verify the status of HugeGraphServer
status() {
    if get_status; then
        echo "Service is running"
        exit 0
    else
        echo "Service is stopped"
        exit 1
    fi
}

# Get status of HugeGraphServer to ensure it is alive
get_status() {
    HTTP_CODE=$(curl -I -s -w "%{http_code}" -o /dev/null $DETECT_URL)
    if [ "$HTTP_CODE" = 200 ]; then
        return 0
    else
        return 1
    fi
}

# Main logic
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart|reload|rs)
        EXIT=0
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start, stop, status, restart|reload|rs}"
        exit 1
esac
exit 0
