#! /bin/sh
#
# Update/create authors for the documentation. But only when there is a Git
# repository. When there is no $(top_srcdir)/.git directory, rely on the
# existing authors list. If the authors.texi file has for some reason been
# deleted from the non version controlled source, then the book will not be
# made (Texinfo will complain about a missing authors.texi).
#
# Call like this:
#     genauthors TOP_SRCDIR
#
# Original author:
#     Mohammad Akhlaghi <mohammad@akhlaghi.org>
# Contributing author(s):
#     Mosè Giordano <mose@gnu.org>
# Copyright (C) 2016-2025 Free Software Foundation, Inc.
#
# Gnuastro 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.
#
# Gnuastro 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 Gnuastro. If not, see <http://www.gnu.org/licenses/>.


# Status report
echo "Generating authors list for documentation."


# Only do the job if a .git directory exists in the top source directory
# (recall that this script is also present in the tar-ball with no .git
# directory and might be run from there)
if [ -d $1/.git ]; then

    # We will need to import the '.mailmap' file from the source directory
    # temporarily to correct the changing emails (see the comments in
    # '.mailmap'). Note that this script is run from within the 'doc/'
    # directory. The original '.mailmap' is in the 'TOP_SRCDIR', so even
    # when the source and build directories are the same, there is no
    # problem.
    #
    # But in case '.mailmap' already exists (for example the script is run
    # in the top source directory not from the 'doc' directory, or if a
    # symbolic link was already created), we won't do any copying.
    if [ -e .mailmap ]; then keepmailmap=1;
    else                     keepmailmap=0; cp $1/.mailmap .mailmap;
    fi

    # Do NOT test if authors.texi is newer than ../.git.  In some cases the
    # list of authors is created empty when running make in top directory
    # (in particular "make -jN" with N > 1), so authors.texi needs to be
    # recreated anyway.
    git --git-dir=$1/.git shortlog --numbered --summary --email --no-merges \
        | sed -e 's/</ /' \
              -e 's/>/ /' \
              -e 's/@/@@/' \
              -e "s/è/@\`e/" \
              -e "s/é/@\'e/" \
              -e "s/ç/@,{c}/" \
        | awk '{for(i=2;i<NF;++i) printf("%s ", $i); \
                printf("(%s, %s)@*\n", $NF, $1)}' \
        > $1/doc/authors.texi

    # Clean up (if necessary)
    if [ $keepmailmap = 0 ]; then rm .mailmap; fi

    # Check if the authors.texi file was actually written:
    if [ ! -s $1/doc/authors.texi ]; then
        echo "authors.texi is empty!"
        exit 1
    fi
else
    echo "No Git repository detected, leaving $1/doc/authors.texi unchanged."
fi
