#!/bin/bash

if [ -z "$EPICS_BASE" -o -z "$EPICS_HOST_ARCH" ]; then
  if ls /etc/profile.d/epics-*.sh > /dev/null 2>&1; then
    for ENVFILE in /etc/profile.d/epics-*.sh; do
      source $ENVFILE
    done
  else
    echo "Cannot determine EPICS_BASE and EPICS_HOST_ARCH!"
    echo "Your environment is broken, make sure that the packages"
    echo "\"epics-base\" and \"epics-ioc-pandadcs\" are installed properly!"
    exit 1
  fi
fi

EPICS_EXE=/opt/epics/iocs/pandadcs/bin/$EPICS_HOST_ARCH/pandadcs
IOC_SCRIPT=/opt/epics/iocs/pandadcs/iocBoot/iocpandadcs/start.script

if [ ! -e $EPICS_EXE ]; then
  echo "Cannot find PANDA DCS executable $EPICS_EXE!"
  exit 2
fi

if [ ! -e $IOC_SCRIPT ]; then
  echo "Cannot find IOC startup script $IOC_SCRIPT!"
  exit 3
fi

$EPICS_EXE $IOC_SCRIPT
