# poor mans readlink. Works on linux and mac
function readlink_f() {
   (oldpwd=$PWD;
   cd $1 && echo `pwd`)
}


# conditional debugging
function logmsg {
   if [ ! -z "$DEBUG_SCRIPT" ];
   then
      echo $*
   fi
}

function readConfig {
   # precedence in config lookup is current directory
   # then installation directory. Load the configuration
   # that is found first
   if [ ! -z $1 ] ;
   then
      if [ -f "$1" ] ;
      then
         CONFIG=$1
      fi
      if [ -d "$1" ] ;
      then
         CONFIG=$1/image-launch.conf
      fi
   else
      if [ -f "$PWD/image-launch.conf" ] ;
      then
         CONFIG="$PWD/image-launch.conf"
      else
         echo "cannot find image-launch.conf"
         exit 1
      fi
   fi


   logmsg "CONFIG = " $CONFIG

   source $CONFIG
   CONFIG_DIR=$(readlink_f  $(dirname $CONFIG))
}

function resolvePidFile {
   # pid file wasn't set manually so we first try to read it from the
   # configuration and if it's not there we create own just for our
   # own purpose
   if [ "$PIDFILE" == "" ] ;
   then
      PIDFILE="$CONFIG_DIR/image-launch.pid"
   fi
}
