#!/bin/sh

#-----------------------------------------------------------------------
# The following function renders a commented configuration-variable
# definition to the standard output.
#
# Inputs:
#  $1     The name of the variable.
#  $2     The default value to give the variable if not defined in the
#         original configuration file.
#  $3     The multi-line comment that should precede the variable
#         definition.
#-----------------------------------------------------------------------
output_variable() {

# Write the comment.

  echo "$3"
  echo ' '

# If a definition for the variable exists in the original file,
# use that definition in the output file. Otherwise adopt the default
# specified in $2.

  if egrep -s -e "^ *$1" $input; then
    egrep -e "^ *$1" $input
  else
    echo "   $1=\"$2\"" 
  fi
}

# Rewrite each of the existing system configuration files, one at a
# time.

for input in `find sys_* -name '*.conf' -print`; do

# Compose a new output file name so that the input file
# doesn't get clobbered until we have had a chance to check for
# unresolved differences.

  output="${input}_new"
  echo ''
  echo ---------------
  echo Creating $output

# Discard any old reconfiguration.

  rm -f $output

# Write the new configuration file. Note that the open-parenthesis on the
# next line is matched by a close-parenthesis near the end of this script.
# This is used so that standard output of all the enclosed commands can
# be redirected in one go to the output file. Without the parenthesis
# we would have to append the output of each command individually. That
# would be much slower.

(

#-----------------------------------------------------------------------
# Preserve any system-specific comments that are found at the start of
# the original configuration file.
#-----------------------------------------------------------------------

awk '{if($0 ~ /^[ \t]*#/ || $0 ~ /^[ \t]*$/) {print $0} else {exit}}' $input

# Write the configuration variable descriptions and assignments.

#-----------------------------------------------------------------------
# XINCL
#-----------------------------------------------------------------------
output_variable XINCL "" '
# Optional: Needed by XWDRIV (/xwindow and /xserve) and
#           X2DRIV (/xdisp and /figdisp).
# The arguments needed by the C compiler to locate X-window include files.' 

#-----------------------------------------------------------------------
# MOTIF_INCL
#-----------------------------------------------------------------------
output_variable MOTIF_INCL "$XINCL" '
# Optional: Needed by XMDRIV (/xmotif).
# The arguments needed by the C compiler to locate Motif, Xt and
# X-window include files.'

#-----------------------------------------------------------------------
# TKDRIV
#-----------------------------------------------------------------------
output_variable  TK_INCL "-I/usr/local/include $XINCL" '
# Optional: Needed by TKDRIV (/xtk).
# The arguments needed by the C compiler to locate Tcl, Tk and
# X-window include files.'

#-----------------------------------------------------------------------
# RV_INCL
#-----------------------------------------------------------------------
output_variable RV_INCL "" '
# Optional: Needed by RVDRIV (/xrv).
# The arguments needed by the C compiler to locate Rivet, Tcl, Tk and
# X-window include files.'

#-----------------------------------------------------------------------
# FCOMPL
#-----------------------------------------------------------------------
output_variable FCOMPL "" '
# Mandatory.
# The FORTRAN compiler to use.'

#-----------------------------------------------------------------------
# FFLAGC
#-----------------------------------------------------------------------
output_variable FFLAGC "" '
# Mandatory.
# The FORTRAN compiler flags to use when compiling the pgplot library.
# (NB. makemake prepends -c to $FFLAGC where needed)'

#-----------------------------------------------------------------------
# FFLAGD
#-----------------------------------------------------------------------
output_variable FFLAGD "" '
# Mandatory.
# The FORTRAN compiler flags to use when compiling fortran demo programs.
# This may need to include a flag to tell the compiler not to treat
# backslash characters as C-style escape sequences'

#-----------------------------------------------------------------------
# CCOMPL
#-----------------------------------------------------------------------
output_variable CCOMPL "" '
# Mandatory.
# The C compiler to use.'

#-----------------------------------------------------------------------
# CFLAGC
#-----------------------------------------------------------------------
output_variable CFLAGC "" '
# Mandatory.
# The C compiler flags to use when compiling the pgplot library.'

#-----------------------------------------------------------------------
# CFLAGD
#-----------------------------------------------------------------------
output_variable CFLAGD "" '
# Mandatory.
# The C compiler flags to use when compiling C demo programs.'

#-----------------------------------------------------------------------
# PGBIND_FLAGS
#-----------------------------------------------------------------------
output_variable PGBIND_FLAGS "" '
# Optional: Only needed if the cpgplot library is to be compiled.
# The flags to use when running pgbind to create the C pgplot wrapper
# library. (See pgplot/cpg/pgbind.usage)'

#-----------------------------------------------------------------------
# LIBS
#-----------------------------------------------------------------------
output_variable LIBS "" '
# Mandatory.
# The library-specification flags to use when linking normal pgplot
# demo programs.'

#-----------------------------------------------------------------------
# XMDRIV
#-----------------------------------------------------------------------
output_variable MOTIF_LIBS "-lXm -lXt $LIBS" '
# Optional: Needed by XMDRIV (/xmotif).
# The library-specification flags to use when linking motif
# demo programs.'

#-----------------------------------------------------------------------
# TKDRIV
#-----------------------------------------------------------------------
output_variable TK_LIBS "-L/usr/local/lib -ltk -ltcl $LIBS -ldl" '
# Optional: Needed by TKDRIV (/xtk).
# The library-specification flags to use when linking Tk demo programs.
# Note that you may need to append version numbers to -ltk and -ltcl.'

#-----------------------------------------------------------------------
# RANLIB
#-----------------------------------------------------------------------
output_variable RANLIB ":" '
# Mandatory.
# On systems that have a ranlib utility, put "ranlib" here. On other
# systems put ":" here (Colon is the Bourne-shell do-nothing command).'

#-----------------------------------------------------------------------
# SHARED_LIB
#-----------------------------------------------------------------------
output_variable SHARED_LIB "" '
# Optional: Needed on systems that support shared libraries.
# The name to give the shared pgplot library.'

#-----------------------------------------------------------------------
# SHARED_LD
#-----------------------------------------------------------------------
output_variable SHARED_LD "" '
# Optional: Needed if SHARED_LIB is set.
# How to create a shared library from a trailing list of object files.'

#-----------------------------------------------------------------------
# SHARED_LIB_LIBS
#-----------------------------------------------------------------------
output_variable SHARED_LIB_LIBS "" '
# Optional:
# On systems such as Solaris 2.x, that allow specification of the
# libraries that a shared library needs to be linked with when a
# program that uses it is run, this variable should contain the
# library-specification flags used to specify these libraries to
# $SHARED_LD'

#-----------------------------------------------------------------------
# MCOMPL
#-----------------------------------------------------------------------
output_variable MCOMPL "" '
# Optional:
# Compiler name used on Next systems to compile objective-C files.'

#-----------------------------------------------------------------------
# MFLAGC
#-----------------------------------------------------------------------
output_variable MFLAGC "" '
# Optional:
# Compiler flags used with MCOMPL when compiling objective-C files.'

#-----------------------------------------------------------------------
# SYSDIR
#-----------------------------------------------------------------------
output_variable SYSDIR '$SYSDIR' '
# Optional: (Actually mandatory, but already defined by makemake).
# Where to look for any system-specific versions of the files in
# pgplot/sys. Before evaluating this script, makemake sets SYSDIR to
# /wherever/pgplot/sys_$OS, where $OS is the operating-system name
# given by the second command-line argument of makemake. If the
# present configuration is one of many for this OS, and it needs
# different modifications to files in pgplot/sys than the other
# configurations, then you should create a subdirectory of SYSDIR,
# place the modified files in it and change the following line to
# $SYSDIR="$SYSDIR/subdirectory_name".'

#-----------------------------------------------------------------------
# Redirect the output of the above commands to the new configuration file.

) > $output

# Check for unexpected differences between the old and new
# configuration files.

  if [ `diff $input $output | egrep '^<' | wc -l` -gt 0 ]; then
    echo "*** The following lines of $input were not found at the"
    echo "*** equivalent locations in $output"
    diff $input $output | egrep '^<'
  fi
  echo ---------------
done
