#!/bin/bash
set -e

pkg=orthanc-wsi
CUR_DIR=`pwd`
service_name=orthanc
orthanc_server="http://localhost:8042"

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp ${CUR_DIR}/debian-tests-data/* -a "${AUTOPKGTEST_TMP}"

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


# Start the orthanc service
/etc/init.d/$service_name start

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


# Transcode data and push to server
OrthancWSIDicomizer CMU-1-Small-Region.tiff --orthanc=$orthanc_server


if [ $? -eq 0 ]; then
    echo "TIFF transcoded and pushed to Orthanc server"
fi