#!/bin/sh
#
#  $Id$
#
#  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
#  project.
#  
#  Copyright (C) 1998-2024 OpenLink Software
#  
#  This project 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; only version 2 of the License, dated June 1991.
#  
#  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 St, Fifth Floor, Boston, MA 02110-1301 USA
#  

#DEBUG
#set -x
#DEBUG

# ----------------------------------------------------------------------
#  Fix issues with LOCALE
# ----------------------------------------------------------------------
LANG=C
LC_ALL=POSIX
export LANG LC_ALL

# ----------------------------------------------------------------------
#  Color settings
# ----------------------------------------------------------------------
B=`tput bold 2>/dev/null`
N=`tput sgr0 2>/dev/null`

# ----------------------------------------------------------------------
#  Globals
# ----------------------------------------------------------------------
OUTPUT_FILE=""
OUTPUT_ARG=""
EXTRA_ARGS=""
ERROR="${B}** ERROR **${N}"
WARNING="${B}* WARNING *${N}"
USAGE=0
USE_STDOUT=0


# ----------------------------------------------------------------------
#  Parse command line
# ----------------------------------------------------------------------
while  test "x$1" != "x"
do
   case "$1" in
	--stdout)
 	  USE_STDOUT=1
	  ;;

       -o)	
          shift
	  OUTPUT_FILE="$1"
	  ;;
	
	-\?)
	  USAGE=1
	  ;;

	*)
	  EXTRA_ARGS="$EXTRA_ARGS $1"
	  ;;
   esac

   shift
done


if test "$USAGE" -eq 1 || test -z "$EXTRA_ARGS"
then
    cat <<-EOF_USAGE

	Usage: $0 [--stdout] prog -o outputfile [args]...

	This script tries to runs a command and optionally pipe standard
	output to a file.

	If the program does not exist but the output file already exists,
	the user is warned that any new data is not parsed and the output
	file is not overwritten. The timestamp of the output file is
	changed to now, so the next make command will continue.

	If the output file does not exist, or the program fails, the
	user gets an appropriate error message.
	
EOF_USAGE
    exit 1
fi


# ----------------------------------------------------------------------
#  Reset arguments
# ----------------------------------------------------------------------
set - $EXTRA_ARGS
PROG=$1
shift

case "$PROG" in
  gawk|*/gawk)
    USE_STDOUT=1
    ;;
esac

VERSION=`$PROG --version 2>/dev/null | head -1`

if test -z "$VERSION"
then
    if test -f "$OUTPUT_FILE"
    then
	#
	#  Program not found, but output file already exists
	#
        cat >&2 <<-EOF_WARNING

	${WARNING}: You should have the ${B}${PROG}${N} program installed on your system.

	Please check the ${B}INSTALL${N} and ${B}README${N} files for package 
	dependencies, recommended versions and operating system dependent 
	instructions to build and install Virtuoso on your system.

	However since the file ${B}${OUTPUT_FILE}${N} exists on your system 
	we will try to use it, but all changes you made to the underlying
	source files will be ignored.

	Please rerun 'make'

EOF_WARNING
	
	#
	# Fix timestamp so hopefully make will continue next time
	#
	touch -c "$OUTPUT_FILE"

	# 
	#  Abort make, so user is forced to read the above error message
	#
	exit 1

    else
	#
	#  Program not found, and output file does not exist
	#
	cat >&2 <<-EOF_MISSING

	${ERROR}: You must have the ${B}${PROG}${N} program installed on your system.

	Please check the ${B}INSTALL${N} and ${B}README${N} files for package 
	dependencies, recommended versions and operating system dependent 
	instructions to build and install Virtuoso on your system.
	         
EOF_MISSING
	
	#
	#  Abort make
	#
	exit 1
    fi
fi


# ----------------------------------------------------------------------
#  Generate some output
# ----------------------------------------------------------------------

if test -z "$OUTPUT_FILE"
then
  echo "* Running $PROG using $VERSION"
else
  echo "* Generating $OUTPUT_FILE using $VERSION"
  test $USE_STDOUT -eq 0 && OUTPUT_ARG="-o $OUTPUT_FILE"
fi

if test $USE_STDOUT -eq 0 || test -z "$OUTPUT_FILE"
then
  $PROG $OUTPUT_ARG $*
  STATUS=$?
else
  $PROG $* > "tmp.$OUTPUT_FILE"
  STATUS=$?
  if test $STATUS -eq 0 && test -s "tmp.$OUTPUT_FILE"
  then
      mv "tmp.$OUTPUT_FILE" "$OUTPUT_FILE"
  fi
fi

if test $STATUS -gt 0
then
    cat >&2 <<-EOF_FAILED

	${ERROR}: Code generation failed while running the following command:

	  $PROG $*

	Please fix the above error(s) before continuing.

	Please check the ${B}INSTALL${N} and ${B}README${N} files for package 
	dependencies, recommended versions and operating system dependent 
	instructions to build and install Virtuoso on your system.

EOF_FAILED
    
    #
    #  Abort make
    #
    rm -f "tmp.$OUTPUT_FILE"
    exit 1
fi


# ----------------------------------------------------------------------
#  All done
# ----------------------------------------------------------------------
exit 0
