#!/bin/bash
# default config should always work for localhost

SCRIPT_DIR=$(dirname "$0")
TEMPLATE="$1"

# make sure TEMPLATE doesn't end with a /
TEMPLATE=$(basename "$TEMPLATE")

function die() {
    echo "$1"
    exit 1
}

function title() {
    echo "----------------------------------------"
    echo "$1"
    echo "----------------------------------------"
}

if [ ! -d "$SCRIPT_DIR/$TEMPLATE" ]; then
    echo "Local Template $TEMPLATE does not exist"
    exit 1
fi

# for now, we need to build and locally install to test the template. Check if it's up to date.

title "Checking template $TEMPLATE"


HASH1=$(ds hash "$SCRIPT_DIR/$TEMPLATE")
HASH2=$(ds hash "/opt/dropshell/templates/$TEMPLATE")

if [ "$HASH1" != "$HASH2" ]; then
    echo "Template $TEMPLATE is out of date"
    echo "Need to run build.sh, and install locally."
    exit 1
fi

SERVICE_NAME="test-$TEMPLATE"

title "Creating service"
ds create-service localhost $TEMPLATE $SERVICE_NAME || die "Failed to create service"

title "Installing service"
ds install localhost $SERVICE_NAME || die "Failed to install service"

title "Stopping service"
ds stop localhost $SERVICE_NAME || die "Failed to stop service"

title "Starting service"
ds start localhost $SERVICE_NAME || die "Failed to start service"

title "Listing services"
ds list localhost || die "Failed to list services"

title "Backing up service"
ds backup localhost $SERVICE_NAME || die "Failed to backup service"

title "Restoring service"
ds restore localhost $SERVICE_NAME || die "Failed to restore service"

title "Checking status"
ds status localhost $SERVICE_NAME || die "Failed to check status"

title "Nuking service"
ds nuke localhost $SERVICE_NAME || die "Failed to nuke service"

title "Listing services"
ds list localhost || die "Failed to list services"



