#!  /bin/bash
#
#  $Source: /cvs/G/EPICS/App/scripts/showtags,v $
#  $Revision: 1.15 $   $Date: 2015/02/04 16:49:28 $
#----------------------------------------------------
#         Check the command line
#
  opts=$(getopt -n showtags ahvs $*)
  if [ $? != 0 ]; then echo Try: -h; exit; fi
#
#         Command line is OK, so set the args and continue.
#
  set -- $opts

  asciiMonth=0

  while [ $# != 0 ]; do
    case "$1" in
      -h | -\? | --help)
        echo "usage: showtags [-a] [-s] [-h] [-v]"
        echo " Find CVS tags in this directory and subdirectories."
        echo " If there is a connection to psip0 (Oracle 9) you get date and time info too."
        echo " Specify -a to get months displayed as abbreviated text string."
        echo " Specify -s to get the tagged status of files in the directory."
        exit 0
        ;;
      -v | --version)
        echo 'Original Author: Dirk Zimoch'
        echo 'Last Change by $Author: zimoch $'
        echo '$Revision: 1.15 $   $Date: 2015/02/04 16:49:28 $'
        echo '$Source: /cvs/G/EPICS/App/scripts/showtags,v $'
        exit 0
        ;;
      -a)
        # Display months as abbreviated month strings, not as numbers.
        asciiMonth=1
        ;;
      -s)
        # Show the tagged status of the directory.
        tagStatus
        exit $?
        ;;
      --)
        ;;
      *)
        echo "Invalid argument $1. Try: -h"
        exit 1
        ;;
    esac
    shift
  done
  trap "stty echo" EXIT SIGTERM SIGKILL

  TAGS=$(cvs status -v 2>/dev/null | awk '
    /\(revision:/   {tag[$1]=1}
    END             {for (i in tag) { print i}}
    ')

function jointags()
{
    echo -n \'$1\'
    shift
    while [ $# != 0 ]
    do
        echo -ne ",\n'$1'"
        shift
    done
    echo
}

TAGLIST=$(jointags $TAGS)

function selectNumericMonth () {
  ivsql  --separator " " "SELECT DISTINCT TagName AS 'Tag', TagDate AS 'Date' FROM CvsTags WHERE TagName IN ($TAGLIST) ORDER BY Date" \
    | awk '{printf "%-40s  %s  %s\n", $1, $2, $3}'
}

function selectAsciiMonth () {
  ivsql  --separator " " "SELECT DISTINCT FORMAT(TagDate, 'dd-MMM-yyyy  HH:MM ') AS 'Date' , TagName AS 'Tag' FROM CvsTags WHERE TagName IN ($TAGLIST) ORDER BY Date" \

}

if [ $asciiMonth = 0 ]; then
  DATABASEINFO=$(selectNumericMonth)
else
  DATABASEINFO=$(selectAsciiMonth)
fi

echo "$DATABASEINFO"
for TAG in $TAGS
do
    echo "$DATABASEINFO" | grep -q $TAG || echo $TAG
done


