#! /bin/zsh -feu

# Dump the git history since the release which is `git-tagged', or `installed'
# not `available'

#  o  Git repository which carries a source debian package
# with
#  o  available(installed, reprepro?) debian package version.
#   grep-status   -F package   --regex '^make$'



# one day I started to use zsh modules (& read the last chapter of Zsh manual)
zmodload zsh/regex

. /usr/share/mmc-shell/mmc-functions.sh
colors

readonly GIT_DESCRIBE_TAG_ERX='^(.*)-([[:digit:]]+)-g([[:alnum:]]+)$'
# 1568-rc56
#     ^ must be there

verbose=n
# Binary package, somehow representing the source package in that Git repo:
# is this an array?
find_installed_version()
{
    # local
    # set -x
    readonly dir=$1
    readonly deb_pkg=($(deb-pkg-binary-packages $dir))
    # I could attempt all of the binary packages...
    deb-pkg-installed-version $deb_pkg[1] |tail -n 1
}

usage()
{
    setopt POSIX_ARGZERO
    cat <<EOF
usage: ${0##*/} [+-hv] [--] [dir]

-v verbose
dir ... by default "."

EOF
    # enters dir and compares its debian/changelog with what is installed (dpkg -l)
}

# fixme: must be invoked inside DIR.
while getopts :hv OPT; do
    case $OPT in
        h|+h)
            usage
            exit 0
            ;;
        v)
            verbose=y
            ;;
        *)
            usage >&2
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1

if [ $# -gt 0 ]; then
    DIR=$1
else
    DIR=.
fi

# so this is the `name'
readonly deb_src_pkg=$(deb-pkg-name $DIR/debian/control*)
readonly installed_version=$(find_installed_version $DIR)

# Calculate the last release (git-tagged): number, distance:
readonly git_describe_since_last_release=$(cd $DIR; git describe --tags HEAD 2>/dev/null)

# readonly
# fixme: this does not fail if not the last!
# git_distance= $(expr match $git_describe_since_last_release $DISTANCE_FROM_DESCRIBE_RX)
if [[ $git_describe_since_last_release -regex-match  $GIT_DESCRIBE_TAG_ERX ]]
then
    git_distance=$match[2]
    git_tag=$match[1]
    git_sha=$match[3]
else
    git_distance=0
    git_tag=$git_describe_since_last_release
    git_sha=""
    # fixme: if it's EXACTLY the tag!
    # die "cannot parse 'git describe' output: $git_describe_since_last_release"
fi

# the tag is distro/version-number
readonly version_from_tag=${git_tag#*/}

# mmc: wrong:
if [[ $verbose = y ]]
then
    print "$fg[red]Describe is $bold_color$git_describe_since_last_release$reset_color"
    print "$fg[red]Distance is $bold_color$git_distance"
    echo "tag is $bold_color $git_tag"
    cecho red "version is $version_from_tag"
fi

echo -ne "$bg_bold[green]$fg[red]$deb_src_pkg" $reset_color '\tis '
echoti setaf 5; echo -n $git_distance
# cecho cyan -n $git_distance
echoti setaf 7; echo " commits ahead of\t $git_tag"

if [ "$version_from_tag" != "$installed_version" ]; then
    cecho blue -e "\tdifferent versions: $version_from_tag vs $installed_version"
fi

# max 10...
git --no-pager log --max-count=10 --format='%C(green)%h %C(blue)%s' $git_tag..HEAD
# git log --oneline $git_tag..HEAD
echo $reset_color



if false; then
    if [ "$version_from_tag" != "$installed_version" ]; then
        cecho red -e $deb_src_pkg  '\t' $git_describe_since_last_release  '\t installed:' $installed_version
    else
        cecho reset -e $deb_src_pkg  '\t' $git_describe_since_last_release
        #   '\t installed:' $version
    fi
fi
