# This is an -*- sh -*- script.  No shebang is necessary.
# Copyright (C) 2020 Luca Saiu
# Updated in 2021 by Luca Saiu
# Written by Luca Saiu

# This file is part of GNU Jitter.

# GNU Jitter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# GNU Jitter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GNU Jitter.  If not, see <http://www.gnu.org/licenses/>.


# Introduction and rationale.
# ################################################################

# I want my portable shell scripts to start with an appropriate shebang line,
# referring the shell that JITTER_SHEBANG expands to; the value for the
# substitution JITTER_SHEBANG is computed by configure and then replaced by
# config.status .
# The problem with this design is that I cannot just write a shebang line
# using @JITTER_SHEBANG@ at the beginning of my source scripts ending in
# .in.m4sh : such a line would not reproduced in the output because of how
# AS_INIT works: everything in the source before the AS_INIT call (in
# m4-utility/jitter-script.m4sh , included in my script sources via m4_include
# ) expands to nothing in the output .in files.
#
# In order to work around this issue here I am prepending a comment to what
# autom4te produces, the first line being of course the approprieate shebang
# as per @JITTER_SHEBANG@ ; config.status will later substitute @JITTER_SHEBANG@
# with the correct value.
# Down below in the expansion there will still be the default shebang produced
# by autom4te , but that will be ignored as an ordinary comment.


# Command line.
# ################################################################

# Fail fatally because of a command-line syntax error.
syntax_error ()
{
  cat <<EOF
$0: syntax error
Try '$0 --help' for more information.
EOF
  exit 1
}

# One argument.  If the argument is --help or --version handle it and
# exit.  Otherwise do nothing.
handle_gnu_option ()
{
  if test "$1" = '--help'; then
    cat <<EOF
Usage: $0 FILE|OPTION
Amend a shell script template (ending in .in , and generated from an .in.m4sh
source) by prepending a shebang line using @JITTER_SHEBANG@ .

Options:
  --help                       print this help
  --version                    show the version number

Report bugs to <@JITTER_PACKAGE_BUGREPORT@>.
@JITTER_PACKAGE_NAME@ home page: <@JITTER_PACKAGE_URL@>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
EOF
    exit 0
  elif test "$1" = '--version'; then
    cat <<EOF
$0 script from @JITTER_PACKAGE_NAME@ @JITTER_PACKAGE_VERSION@
Copyright (C) 2020-2021 Luca Saiu.
GNU Jitter comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Jitter under the terms of the GNU General
Public License, version 3 or any later version published by the Free Software
Foundation.  For more information see the file named COPYING in the source
distribution.

Written by Luca Saiu <http://ageinghacker.net>.
EOF
    exit 0
  fi
}

# Handle the command line.
if test "$#" != 1; then
  syntax_error
fi
file="$1"
handle_gnu_option ${file}

# If the script is still running at this point then ${file} is in fact a file
# to process.


# Work.
# ################################################################

mv "${file}" "${file}.tmp" \
  && echo '@JITTER_SHEBANG@' \
       > "${file}" \
  && echo "# This machine-generated portable shell script is part of GNU Jitter." \
       >> "${file}" \
  && echo "# The human-written source file for this script is ${file}.m4sh ." \
       >> "${file}" \
  && echo "# The initial comments in the source are not reproduced here; see" \
       >> "${file}" \
  && echo "# the source file for copyright and license notices." \
       >> "${file}" \
  && echo '#' \
       >> "${file}" \
  && echo '# The shebang line below is, of course, ignored.  See the comments' \
       >> "${file}" \
  && echo '# in amend-autom4te-output , distributed along with the Jitter' \
       >> "${file}" \
  && echo '# sources, for an explanation.' \
       >> "${file}" \
  && echo '#' \
       >> "${file}" \
  && echo '# What follows is generated by autom4te from an M4sh source.' \
       >> "${file}" \
  && echo '' \
       >> "${file}" \
  && cat "${file}.tmp" >> "${file}" \
  && rm "${file}.tmp" \
  && echo "...Done, amending the shebang line in $file ."
