#!/bin/ksh
#$Id$
#---------------------------------------------------------------------
# @(#)Rebuild IOIPSL domains
#---------------------------------------------------------------------
#-
#set -xv
#-
# Extract the calling sequence for the script (d_n/b_n)
#-
d_n=$(dirname $0); b_n=$(basename $0);
#-
# Retrieving and validation of the options
#-
r_v='silencious'; r_f='noforce'; r_o="";
while getopts :hvfo: V
 do
  case $V in
   (h) echo '';
       echo '"'${b_n}'"';
       echo '  rebuild a model_file from several input files.';
       echo 'Each input file contains the model_data for a domain.';
       echo 'Usage :';
       echo ${b_n} '[-h]';
       echo ${b_n} '[-v] [-f] -o output_file_name input_file_names';
       echo ' -h : this help';
       echo ' -v : verbose mode';
       echo ' -f : executing mode';
       echo '  (execute the program even if the number of input files';
       echo '   is not equal to the total number of domains)';
       echo '';
       exit 0;;
   (v) r_v='verbose';;
   (f) r_f='force';;
   (o) r_o=${OPTARG};;
   (:)  echo ${b_n}" : option $OPTARG : missing value" 1>&2;
        exit 2;;
   (\?) echo ${b_n}" : option $OPTARG : not supported" 1>&2;
        exit 2;;
  esac
 done
shift $(($OPTIND-1));
#-
# Validate the number of arguments
#-
if   [ ${#} -lt 1 ]; then
  echo ${b_n}' : Too few arguments have been specified. (Use -h)' 1>&2;
  exit 3;
fi
#-
# Check for the output file name
#-
if [ '\?'${r_o} = '\?' ]; then
  echo ' ' 1>&2;
  echo ${b_n}' : output_file_name not specified. (Use -h)' 1>&2;
  echo '          "rebuilt_file.nc" should be created.' 1>&2;
  echo ' ' 1>&2;
  r_o='rebuilt_file.nc'
fi;
#-
# Validate the names of the input files
#-
qi=0;
for i in $*;
 do ((qi=qi+1));
  [ ${qi} -le ${#} ] && [ ! -f ${i} ] && \
    { echo "${i} unreachable ..."; exit 3;}
 done
#-
# Create the information file for the program
#-
echo ${r_v} > tmp.$$;
echo ${r_f} >> tmp.$$;
((qi=$#+1));
echo ${qi} >> tmp.$$;
for i in $*;
 do echo ${i} >> tmp.$$;
 done
echo ${r_o} >> tmp.$$;
#-
# Create the output file
#-
${d_n}/flio_rbld < tmp.$$
#-
# Clear
#-
rm -f tmp.$$
#-
# End
#-
exit 0;
