#!/usr/bin/tcsh -f

#
# fsr-checkxopts
#
# USAGE: fsr-checkxopts xoptsfile
#
# Checks the xoptsfile to make sure that there are no errors in it.
#
# Original Author: Doug Greve
#
# Copyright © 2021 The General Hospital Corporation (Boston, MA) "MGH"
#
# Terms and conditions for use, reproduction, distribution and contribution
# are found in the 'FreeSurfer Software License Agreement' contained
# in the file 'LICENSE' found in the FreeSurfer distribution, and here:
#
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
#
# Reporting: freesurfer@nmr.mgh.harvard.edu
#
#

set VERSION = 'fsr-checkxopts mockbuild-local';

if($#argv < 1) exit 0;
set xoptsfile = $argv[1];

if(! -e $xoptsfile) then
  echo "ERROR: cannot find $xoptsfile"
  exit 1;
endif

# Get the number of commands in the xopts file
set n1 = `grep -v \# $xoptsfile | awk '{print $1}' | sort | wc -l`;

# Get the number of unique commands in the xopts file
set n2 = `grep -v \# $xoptsfile | awk '{print $1}' | sort | uniq | wc -l`;

# If they are not the same, then it means that there is a replication
if($n1 != $n2) then
  echo "ERROR: multiple occurrences of a command in expert options file $xoptsfile"
  exit 1;
endif

exit 0
