#! /bin/sh
#
#  Checks that the $1 options stored in the file $2 are exactly the same than
#  those expressed by the remaining arguments.
#
#  Up to six options may be verified.
#
#  É. Canot -- IPR/CNRS -- May 2013
#
#-----------------------------------------------------------------------

# needs at least 3 args: 1 nopt, 1 filename and 1 string
if [ $# -lt 3 ]; then
  echo
  echo "  usage: `basename $0` nopt file arg1 [arg2] ..."
  echo "         (where file contains old values of 'n' args)"
  echo
  exit 1
fi

NOPT=$1
# NOPT must be greater than (or equal to) 1
if [ $NOPT -lt 1 ]; then
  echo
  echo "  (`basename $0`:) nopt must be >= 1"
  echo
  exit 1
fi

MAX_OPTS=6

# NOPT must be less than (or equal to) MAX_OPTS
if [ $NOPT -gt $MAX_OPTS ]; then
  echo
  echo "  (`basename $0`:) nopt must be <= $MAX_OPTS"
  echo
  exit 1
fi

FILE=$2

ARG1=$3
ARG2=$4
ARG3=$5
ARG4=$6
ARG5=$7
ARG6=$8

# read FILE (if exist !)
if ! [ -f $FILE ]; then
  exit 0
fi
set `cat $FILE`

ARG1_OLD=$2
ARG2_OLD=$3
ARG3_OLD=$4
ARG4_OLD=$5
ARG5_OLD=$6
ARG6_OLD=$7

IFS="=" # new Internal Field Separator

set $ARG1_OLD
ARG_NAME_OLD=$1
set $ARG1
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG1" != "$ARG1_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG1_OLD but new: $ARG1"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi
if [ $NOPT -eq 1 ]; then
  exit 0
fi

set $ARG2_OLD
ARG_NAME_OLD=$1
set $ARG2
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG2" != "$ARG2_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG2_OLD but new: $ARG2"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi
if [ $NOPT -eq 2 ]; then
  exit 0
fi

set $ARG3_OLD
ARG_NAME_OLD=$1
set $ARG3
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG3" != "$ARG3_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG3_OLD but new: $ARG3"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi
if [ $NOPT -eq 3 ]; then
  exit 0
fi

set $ARG4_OLD
ARG_NAME_OLD=$1
set $ARG4
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG4" != "$ARG4_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG4_OLD but new: $ARG4"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi
if [ $NOPT -eq 4 ]; then
  exit 0
fi

set $ARG5_OLD
ARG_NAME_OLD=$1
set $ARG5
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG5" != "$ARG5_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG5_OLD but new: $ARG5"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi
if [ $NOPT -eq 5 ]; then
  exit 0
fi

set $ARG6_OLD
ARG_NAME_OLD=$1
set $ARG6
ARG_NAME=$1
if [ "$ARG_NAME" != "$ARG_NAME_OLD" ]; then
  echo
  echo "  ERROR: corrupted 'make_options' file?"
  echo "         (discrepancy at least for one make option)"
  echo "         old: $ARG_NAME_OLD but new: $ARG_NAME"
  echo
  echo "         you should type 'make clean' before any new make command."
  echo
  exit 1
fi
if [ "$ARG6" != "$ARG6_OLD" ]; then
  echo
  echo "  ERROR: you have changed one make option!"
  echo
  echo "         old: $ARG6_OLD but new: $ARG6"
  echo "         (verify by typing: make howmake)"
  echo
  echo "         you should type 'make clean' before,"
  echo "         or use old options by typing: \`cat make_options\`"
  echo
  exit 1
fi

# normal exit
exit 0
