#!/bin/bash

set -e


pkg=orthanc
service_name=$pkg

if [ "$AUTOPKGTEST_TMP" = "" ]; then
	# create the tmp directory
	AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXX`
	
	# clean the directory if any of these signal is detected
	trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi

cd  $AUTOPKGTEST_TMP


# Check if user 'orthanc' exists
if getent passwd orthanc > /dev/null 2>&1; then
    echo "User 'orthanc' exists."
else
	adduser --system --group orthanc
    echo "User 'orthanc' added."
fi


/etc/init.d/$service_name start

if [ $? -eq 0 ]; then
    echo "Orthanc is active."
else
    echo "Orthanc is not active."
fi

URL="http://localhost:8042/app/explorer.html"

status_code=$(curl -s -o /dev/null -w "%{http_code}" "$URL")

# Check the status code
if [ "$status_code" -eq 200 ]; then
    echo "Request successful (HTTP status code 200)"
    exit 0
else
    echo "Request failed with HTTP status code $status_code"
    exit 1
fi






