#!/bin/bash

if [ "$(which alba_blissrc 2>/dev/null)" ] ; then
  echo "loading .rc file ..."
  . alba_blissrc
fi

PTAROOT=$(python -c "import imp;print(imp.find_module('PyTangoArchiving')[1])")

case $1 in
 --help|help)
  echo "Usage:"
  echo "  ctarchiving --help"
  echo "  ctarchiving --load <filename.csv> <hdb/tdb> [force] ; adds attributes to archiving "
  echo "  ctarchiving --check <filename.csv> <hdb/tdb> [force] ; checks if attributes from .csv are archived"
  echo "  ctarchiving --parse <filename.csv>"
  echo '   ctarchiving --export [--resolution=X(s)] [--hdb] [--tdb] [--modes] [attributes] ["Y-m-d H:M"] ["Y-m-d H:M"] filename.csv'
  echo "  ctarchiving --search <device/attributes> ; searchs for matching archived attributes, returns configuration"
  echo "  ctarchiving --gui <device/attributes> ; shows Archiving GUI, TaurusFinder"
  echo "  ctarchiving [attributes] ; shows Archiving GUI, TaurusFinder"
  ;;
 --load|load)
 shift 1
 python $PTAROOT/files.py load $*
  ;;
 --check|check)
 shift 1
 python $PTAROOT/files.py check $*
  ;;
 --parse|parse)
  python -c "import PyTangoArchiving as pta;print('\n');print('\n'.join(map(str,pta.files.ParseCSV('$2',log=True).items())))"
  ;;
 --export|export)
  shift 1
  archiving2csv $*
  ;;
 --search|search)
  python -c "import fandango as F,PyTangoArchiving as pta;pta.api('hdb'),pta.api('tdb');print('\n');print('\n'.join(sorted(str((a,pta.api(s)[a])) for s in ('hdb','tdb') for a in pta.api(s) if F.matchCl('$2',a))))"
  ;;
 --search2|search2)
  python -c "import fandango as F,PyTangoArchiving as pta;pta.Reader();print('\n');print('\n'.join(sorted(str((a,pta.Reader().get_attribute_modes(a))) for a in pta.Reader().get_attributes(active=1) if F.matchCl('$2',a))))"
  ;;
 --gui|gui)
  shift 1
  ctarchiving $*
  ;;  
 *)
  cd $PTAROOT/widget
  python ArchivingBrowser.py $*
  ;;
esac

