#!/bin/bash

#
# ciaover
#
# Script to report versions 
#
#  Copyright (C) 2020-2023 Smithsonian Astrophysical Observatory
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


LOC_ASCDS_INSTALL=${ASCDS_INSTALL}
CALDB_VER="docs/chandra/caldb_version/caldb_version.fits"

retn()
{
    let n=${1}
    shift
    while (( $n > 1 ))
    do
	let n=$n-1
	shift
    done
    echo ${1}
}

cv_read_fits()
{
# First try to use dmtlist (if available) then try dmlist
# (if installed) else use fold to read the fits file

    if [ -f "${ASCDS_INSTALL}/test/bin/datamodel/dmtlist" ] ; then
	cv_ret="`${ASCDS_INSTALL}/test/bin/datamodel/dmtlist ${1}'[cols CALDB_VER]' data,clean 2>/dev/null | tail -1`"
    elif [ -f "${ASCDS_INSTALL}/bin/dmlist" ] ; then
	cv_ret="`${ASCDS_INSTALL}/bin/dmlist ${1}'[cols CALDB_VER]' data,clean 2> /dev/null | tail -1`"
    else
	cv_ret="`strings ${1} | fold -80 | grep REVISION= | cut -d\' -f2`"
    fi

# check for the string "error" in the output as something might of gone wrong
# then fail to fold

    if [ "x`echo ${cv_ret} | grep error`" != "x" ] || [ "x${cv_ret}" == "x" ] ; then
	cv_ret="`strings ${1} | fold -80 | grep REVISION= | cut -d\' -f2`"
    fi
    echo ${cv_ret}
}

listcaldb()
{
    cv_calver=""
    if [ -f "${CALDB}/${CALDB_VER}" ] ; then
	cv_calver="`cv_read_fits ${CALDB}/${CALDB_VER}`"
    elif [ -f "${ASCDS_INSTALL}/CALDB/${CALDB_VER}" ] ; then
	cv_calver="`cv_read_fits ${ASCDS_INSTALL}/CALDB/${CALDB_VER}`"
    else
	cv_calver="Not installed"
    fi
    echo "${cv_calver}"
}

segpad()
{
    case ${1} in
	ciao )
	    \echo "CIAO        "
	    ;;
	contrib )
	    \echo "Contrib     "
	    ;;
	*)
	    \echo ;;
    esac
}

printlong()
{
    if [ -f "${LOC_ASCDS_INSTALL}/VERSION_${1}" ] ; then
	vline="`\cat ${LOC_ASCDS_INSTALL}/VERSION_${1}`"
	part1="`retn 1 ${vline}`"
	part2="`retn 2 ${vline}`"
	part2_trim="`echo ${part2} | sed 'y/./ /'`"
	\echo "  `segpad ${part1}`: Package release `retn 3 ${part2_trim}`  `retn 3 ${vline}` `retn 4 ${vline}` `retn 5 ${vline}` `retn 6 ${vline}`"
    else
	\echo "  `segpad ${1}`: Not Installed"
    fi
}

printcontrib()
{
    if [ -f "${LOC_ASCDS_INSTALL}/VERSION.CIAO_scripts" ] ; then
	vline="`\cat ${LOC_ASCDS_INSTALL}/VERSION.CIAO_scripts`"
	part1="`retn 1 ${vline}`"
	part2="`retn 2 ${vline}`"
	part2_trim="`echo ${part2} | sed 'y/./ /'`"
	\echo "  `segpad contrib`: Package release `retn 3 ${part2_trim}`  `retn 3 ${vline}` `retn 4 ${vline}` `retn 5 ${vline}` `retn 6 ${vline}`"
    else
	\echo "  `segpad contrib`: Not Installed"
    fi
}

printshort()
{
    if [ -f "${LOC_ASCDS_INSTALL}/VERSION_${1}" ] ; then
	vxline="`\cat ${LOC_ASCDS_INSTALL}/VERSION_${1}`"
	part2="`retn 2 ${vxline}`"
	part2_trim="`echo ${part2} | sed 'y/./ /'`"
	\echo -n ".`retn 3 ${part2_trim}`"
    else
	\echo -n ".-"
    fi
}


action="x"
while [ "x${1}" != "x" ] ; do
    case ${1} in
	-s ) action="${1}" ;;
	-v ) action="${1}" ;;
	-h )
	    \echo "${0} -h -s -v -t <location of ciao>"
	    \echo "     -h Prints this message"
	    \echo "     -s short (default)"
	    \echo "     -v verbose"
	    \echo "     -t <location of ciao> Report on another copy of ciao"
	    ;;
	-t )
	    shift
	    if [ "x${1}" != "x" ] ; then
		LOC_ASCDS_INSTALL=${1}
	    fi ;;
	* ) : ;;
    esac
    shift
done

if [ -f "${LOC_ASCDS_INSTALL}/VERSION" ] ; then
    case "x${action}" in
	x-v )
# print long report

	    \echo "The current environment is configured for:"
	    \echo -n "  CIAO        : "
	    \cat ${LOC_ASCDS_INSTALL}/VERSION
	    \echo "`printcontrib`"
	    \echo "  bindir      : ${LOC_ASCDS_INSTALL}/bin"
	    if [ "${CIAO_PYTHON}" != "CIAO" ] && [ "x${CIAO_PYTHON}" != "x" ] ; then
		pythonpath=${CIAO_PYTHON}
	    else
		pythonpath="${LOC_ASCDS_INSTALL}/bin"
	    fi
	    \echo "  Python path : ${pythonpath}"
	    \echo "  CALDB       : `listcaldb`"
	    \echo
	    if [ -f "${LOC_ASCDS_INSTALL}/ciao-type" ] ; then
		\echo "CIAO Installation: `cat ${LOC_ASCDS_INSTALL}/ciao-type`"
	    elif [ -f "${LOC_ASCDS_INSTALL}/bin/ciao-type" ] ; then
		\export ASCDS_INSTALL="${LOC_ASCDS_INSTALL}"
		\echo "CIAO Installation: `${LOC_ASCDS_INSTALL}/bin/ciao-type`"
	    fi
	    \echo "System information:"
	    \uname -a
	    ;;
	x-s )

# print short report

	    \echo -n " "
	    \cat  ${LOC_ASCDS_INSTALL}/VERSION
	    ;;
	* )
	    \cat ${LOC_ASCDS_INSTALL}/VERSION
	    \echo "  bindir      : $LOC_ASCDS_INSTALL/bin"
	    \echo "  CALDB       : `listcaldb`"
	    ;;
    esac
else
    echo "VERSION File missing!"
fi
