#!/bin/bash

# This script is a variant from fandango.scripts.DynamicDS
# srubio@cells.es, 2016

DS=PyAlarm
INSTANCE=$1

DSPATH=$(python -c "import imp;print(imp.find_module('panic')[1])" 2>/dev/null)/ds
DSPATH=$(readlink -f $DSPATH)

echo "Launching $DSPATH/$DS $INSTANCE"

# TODO: if it is mandatory to be in the module path 
cd ${DSPATH}

if [ $(which screen 2>/dev/null) ] ; then
 if [ ! "$(echo $* | grep attach)" ] ; then
  echo "run detached"
  CMD="screen -dm -S $DS-$INSTANCE "
 else
  CMD="screen -S $DS-$INSTANCE "
 fi
else
  CMD=""
fi

CMD="${CMD} python ${DSPATH}/$DS.py $INSTANCE $2"
echo $CMD
$CMD
