#!/bin/bash
#
# Version 1.02, Time-stamp: <08/09/2009, 15:44:46>
# Fanel Donea, fdonea AT users DOT sourceforge DOT net
# http://versions.sourceforge.net
#
# Usage:
# versions [[case_insensitive_strings] ...]
#
# Quick way to list _personally relevant_ versions of software on a computer.
# If 'substring' is given output is limited to lines containing it.

# If called with one (string) argument, run this very script
# with no arguments but grep for the string and exit.

# De facut:
#    - LOOP OVER ARGS!!!
#    - de mutat functia in alt fisier?
#    - de mutat fiecare componenta in fisiere separate: check_gcc, check_perl etc
#    - LOOP OVER check_* FILES FOUND IN THE DIRECTORY 'selected' !!!


if [ "$#" -eq 1 ]
then
   $0 | grep -i $1
   exit
fi

n_args="$#"
egrep_string=""
counter=0
for arg in "$@"
do
   counter=`expr $counter + 1`
   echo "arg $counter=$arg"

   egrep_string="${egrep_string}${arg}"

   if [ $counter -lt "$n_args" ]
   then
      egrep_string="${egrep_string}|"
   fi
done

#echo egrep_string = $egrep_string


#$0 | egrep -i "$egrep_string"


# Some variables
not_found="*** Not found. ***"
out=/tmp/output_stdout$$
err=/tmp/output_stderr$$


# Function that tries to run a command that may or may not be installed.
function try_to_run
{
   command_title=$1       # e.g. GNU gcc
   command_version=$2     # e.g. gcc -v, ant -version etc
   command_extract=$3     # e.g. head -1 $out, etc

   echo -e -n "$command_title"
   # Run command, trap error, redirect output to files
   installed="yes"
   trap 'installed="no"' ERR
   $command_version 1> $out 2> $err
   # Reset to default.
   trap ERR

   if [ "$installed" == "yes" ]
   then
      /bin/bash -c "$command_extract"
   else
      echo "$not_found"
   fi

}


# Some preliminary info.
echo -e "Host name:\t`hostname -f`"
# Linux only
echo -e -n "CPU:\t\t"
grep "model name" /proc/cpuinfo | awk -F: '{print $2}' | sed -e 's/ \(.*\)/\1/' | head -1
# head -1 aded for multiproc machines; CPUs assumed identical.
# if running Red Hat
if [ -f /etc/redhat-release ]
then
   echo -e -n "RH version:\t"
   cat /etc/redhat-release
else
   echo -e -n "/etc/issue:\t"
   cat /etc/issue | tr -d "\n"
   echo
fi
echo -e -n "Kernel:\t\t"
uname -r




try_to_run "GNU gcc:\t"     "gcc -v" "tail -1 $err | sed -e 's/gcc version \(.*\)/\1/'"
try_to_run "Fortran 77:\t"  "f77 -v" "tail -1 $err"
try_to_run "PostgreSQL:\t"  "psql --version" "head -1 $out"
try_to_run "gfortran:\t"    "gfortran -v" "tail -1 $err | sed -e 's/gcc version \(.*\)/\1/'"
try_to_run "Java (Sun)\t"   "java -version" "head -1 $err"
try_to_run "Perl:\t\t"      "perl -v" "head -2 $out | tail -1"
try_to_run "Ant:\t\t"       "ant -version" "cat $out"
try_to_run "MySQL:\t\t"     "mysql -V" "cat $out | sed -e 's/\(.*\), \(.*\)/\1/'"
try_to_run "OpenSSL:\t"     "openssl version" "cat $out"
try_to_run "Make:\t\t"      "make -v" "head -1 $out"
try_to_run "Autoconf:\t"    "autoconf --version" "head -1 $out"
try_to_run "Automake:\t"    "automake --version" "head -1 $out"
try_to_run "Libtool:\t"     "libtool --version"  "head -1 $out | cut -c -55"  # sed later!!
try_to_run "Python:\t\t"    "python -V" "cat $err"
try_to_run "rsync:\t\t"     "rsync --version" "head -1 $out"
try_to_run "Mozilla:\t"     "mozilla -v" "cat $out | sed -e 's/\(.*\), Copyright\(.*\)/\1/'"
try_to_run "Firefox:\t"     "firefox -v" "cat $out | sed -e 's/\(.*\), Copyright\(.*\)/\1/'"
try_to_run "pkg-config\t"   "pkg-config --version" "cat $out"
try_to_run "Emacs\t\t"      "emacs --version" "head -1 $out"
try_to_run "mc\t\t"         "mc -V" "head -1 $out"
try_to_run "Dia\t\t"        "dia -v" "cat $out"
try_to_run "LaTeX\t\t"      "latex -v" "head -1 $out"
try_to_run "GnuCash\t\t"    "gnucash -v" "head -1 $out"
try_to_run "Fvwm\t\t"       "fvwm -version" "head -1 $out"
try_to_run "Lynx\t\t"       "lynx -version" "head -1 $out"
try_to_run "cdrecord\t"     "cdrecord -version" "head -1 $out | cut -c -38"
try_to_run "gnuplot\t\t"    "gnuplot -V" "cat $out"
try_to_run "Apache httpd\t" "httpd -v" "head -1 $out"
try_to_run "Acroread\t"     "acroread -v" "cat $out"
try_to_run "ImageMagick\t"  "display -version" "head -1 $out | cut -c -50"
try_to_run "CATALINA_HOME\t"  "printenv CATALINA_HOME" "cat $out"
try_to_run "Tomcat:\t\t"   "/bin/sh $CATALINA_HOME/bin/version.sh" "grep 'Server version:'  $out"
#NOT USED try_to_run "Tomcat:\t\t"  "echo CATALINA_HOME = `printenv CATALINA_HOME`" "cat $out"
try_to_run "GridSphere:\t" "cat $CATALINA_HOME/webapps/gridsphere/WEB-INF/gridsphere.properties" "grep gridsphere.release $out"
try_to_run "Maven\t\t"   "mvn --version" "head -1 $out"




############## TRY NOT TO READ WHAT'S BELOW ################################3
# TO DO

# tcl/tk!!!

#htmldoc --help | head -1
#HTMLDOC Version 1.8.9 Copyright 1997-2000 Easy Software Products, All Rights Reserved.

# This produces 'not found'!!! FIX!!!!!
#try_to_run "Rxvt\t\t"       "rxvt -h" "cat $out"
#try_to_run "Rxvt\t\t"       "rxvt -h 2>&1" "cat $out"


# this works, but how can we use or improve 'try+_to_run' ???
echo -e -n "Rxvt\t\t"
result=`rxvt -h  2>&1 | grep Usage`
if [ $? -eq "0" ]
then
   echo $result
else
   echo "$not_found"
fi

# Delete temporary files.
/bin/rm $out $err
