#! /bin/sh
#
# Generate a list of authors from the git repository, it will only
# actually do anything if a .git file exists. Run like this:
#
#    ./genauthors top/src/dir
#
# Note that some authors might have changed their email addresses over
# the course of their contributions to Gnuastro. Fortunately Git has a
# great tool for that: the .mailmap file. It has already been included
# in the source code and if any author changes their email address or
# would want their name to be printed differently here, please use
# that file. See the git-shortlog manpage for a complete explanation
# of all the possible ways to do this.
#
# Original author:
#     Mohammad Akhlaghi <akhlaghi@gnu.org>
# Contributing author(s):
# Copyright (C) 2015, 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/>.





# Initial settings:
set -o nounset                  # Stop if a variable is not set.
set -o errexit                  # Stop if a program returns false.





# Only do the job if a .git directory exists (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
    echo "There is no Git repository in the source directory."
    echo "AUTHORS cannot be generated."
    exit 0
fi





# Print a status report, since this will be run along with the large
# number of bootstrap operations, it is best to tell the users since
# it might take a few seconds.
echo "Generating AUTHORS from the version controlled source..."





# Make sure the .mailmap file is present in this directory, so Git can fix
# the different email addresses and names of one person. Note that while
# this is in the top source directory, it is possible for the source and
# build directories to be different, and we have to be prepared for that.
if [ ! -f .mailmap ]; then
    ln -s $1/.mailmap .mailmap
fi





# Set the version number. Note that this script is also run at the start of
# the bootstrapping process. At that point we don't have the `.version'
# file, so we will just rely on `.git describe'. Later during `make', this
# scripot will be run again to set it using `git-version-gen'.
if [ -f "$1/.version" ]; then
    gnuastroversion="Gnuastro "$(cat "$1/.version")
else
    gnuastroversion=$(git --git-dir=$1/.git describe)
fi





# Print the top of the AUTHORS file.
outauthors="$1/AUTHORS"
echo "GNU Astronomy Utilities authors
===============================

List of authors (and their relative contribution) for this version of GNU
Astronomy Utilities (Gnuastro). The relative contribution is measured by
the number of commits made in the version controlled history.

Generated for $gnuastroversion.

The format for each author is: [Number of commits] [Name] (email)


Aggregate list of all authors
-----------------------------

List of all authors irrespective of which part of Gnuastro they contributed
to with their email address, the rows are ordered alphabetically." > \
"$outauthors"
# This extra line is to have a blank line between the explanations and the
# list.
echo "" >> "$outauthors"


# Generate the aggregate list
git --git-dir=$1/.git shortlog --summary --email --no-merges >> "$outauthors"


# For each utility, print the list of authors:
echo "

Separated by part of Gnuastro
-----------------------------

Authors listed under the respective part of Gnuastro: utility, libraries,
or documentation. Note that a single commit might include multiple parts of
Gnuastro (especially in the libraries and documentation). The authors in
each part are sorted by the number of commits, see the aggregate list for
emails." >> "$outauthors"

# A blank line to separate the list below from the explanations.
echo "" >> "$outauthors"


# Go over each part of Gnuastro and add the authors:
for util in src/* "lib/" "doc/"; do

    # Set the name of the utility
    if   [ $util = src/arithmetic  ]; then name=Arithmetic
    elif [ $util = src/convertt    ]; then name=ConvertType
    elif [ $util = src/convolve    ]; then name=Convolve
    elif [ $util = src/cosmiccal   ]; then name=CosmicCalculator
    elif [ $util = src/header      ]; then name=Header
    elif [ $util = src/imgcrop     ]; then name=ImageCrop
    elif [ $util = src/imgstat     ]; then name=ImageStatistics
    elif [ $util = src/imgwarp     ]; then name=ImageWarp
    elif [ $util = src/mkcatalog   ]; then name=MakeCatalog
    elif [ $util = src/mknoise     ]; then name=MakeNoise
    elif [ $util = src/mkprof      ]; then name=MakeProfile
    elif [ $util = src/noisechisel ]; then name=NoiseChisel
    elif [ $util = src/subtractsky ]; then name=SubtractSky
    elif [ $util = lib/            ]; then name=Libraries
    elif [ $util = doc/            ]; then name=Documentation
    else echo; echo; echo "genauthors.sh: $util not recognized!"; exit 1;
    fi

    # Fill in the authors of each part:
    echo $name >> "$outauthors"
    git --git-dir=$1/.git shortlog --numbered --summary     \
        --no-merges -- $util >> "$outauthors"
    echo "" >> "$outauthors"
done
