#!/bin/bash

if [ `id -u` -ne 0 ]; then
  >&2 echo "Error: You must be root to do this!"
  exit 255
fi

export TMPFILE=/tmp/ioc-build.release
if [ -e $TMPFILE ]; then
  /bin/rm -r $TMPFILE
fi
if ls /opt/epics/ioc-build/*.release > /dev/null 2>&1; then
  for FILE in /opt/epics/ioc-build/*.release; do
    cat $FILE >> $TMPFILE
  done
else
  touch $TMPFILE
fi

cd /opt/epics/iocs/pandadcs
if [ "$1" = "--debug" ]; then
  /usr/bin/make rebuild
   RETURNCODE=$?
else
  /usr/bin/make rebuild > /dev/null 2>&1
   RETURNCODE=$?
   if [ $RETURNCODE -ne 0 ]; then
     >&2 echo "Building the pandadcs-IOC has failed. Run \"pandadcs-buildioc --debug\" to see details of the make process."
   fi
fi
/bin/rm -r $TMPFILE
exit $RETURNCODE
