#!/bin/sh
# vim: ts=4 sw=4 et ft=sh
#---------------------------------------------------------------------------#
# Copyright (C) 2020 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#
#
# This script constructs (or reconstructs) the contents of
# compiler/optimization_options.m. The initial and final parts
# are hand_written, in make_optimization_options_{start,end}
# respectively, while the middle is generated by
# make_optimization_options_middle from make_optimization_options_db.
#
# make_optimization_options_middle also reconstructs the contents of
# compiler/options.m. Specifically, it rewrites the part between two markers,
# INCLUDE_HANDLER_FILE_START and INCLUDE_HANDLER_FILE_END, which contains
# the code handling the bool_special, int_special etc optimization options.
#

if test -f ../compiler/optimization_options.m
then
    chmod u+w ../compiler/optimization_options.m
fi

/bin/rm .options_init .options_final handler_file > /dev/null 2>&1

(
cat make_optimization_options_start;
awk -f make_optimization_options_middle make_optimization_options_db;
cat make_optimization_options_end
) > ../compiler/optimization_options.m

chmod a-w ../compiler/optimization_options.m

sed -e '1,/INCLUDE_HANDLER_FILE_START/w .options_init' \
    < ../compiler/options.m > /dev/null 2>&1
sed -e '/INCLUDE_HANDLER_FILE_END/,$w .options_final' \
    < ../compiler/options.m > /dev/null 2>&1
# The invocation of make_optimization_options_middle above
# creates handler_file
cat .options_init handler_file .options_final > .new_options.m
mv -f .new_options.m ../compiler/options.m
/bin/rm .options_init .options_final handler_file > /dev/null 2>&1
