#!/bin/bash

# 20220315 bkw: SBo pre-commit hook, wrapper for sbolint.
# 20260919 bkw: no code changes; update install comments.
# 20261130 bkw: add check for md5sums in the .info file.

# TODO:
# - rewrite in perl.

# Installation:

# This file is part of sbo-maintainer-tools, which you can get
# from SBo:
# https://slackbuilds.org/repository/15.0/system/sbo-maintainer-tools/

# Copy this file to <gitdir>/.git/hooks, mode 0755 (or anyway, make it
# executable).

# Usage:

# Just do your usual "git commit". When you do, sbolint will run on
# the build you're updating. If it finds any issues, it will cause the
# commit to abort, so you can fix whatever's wrong and try the commit
# again.

# Since sbolint isn't perfect, you can skip the check for any commit
# by running e.g:

#  SBOLINT=no git commit <arguments>

# This script also checks the .info file, to see whether VERSION changed.
# If it has, it checks MD5SUM and/or MD5SUM_x86_64 to see whether they've
# changed. If they haven't, the commit is aborted. This is to guard
# against the common mistake of updating a build's version but forgetting
# to update the md5sums in the .info file. If you are 100% sure your
# commit is correct, you can skip the md5sum check:

#  MD5OK=yes git commit <arguments>

# Notes:

# This really uses bash features, please don't try changing the top
# line to #!/bin/sh, it'll fail (even if /bin/sh is a symlink to
# /bin/bash).

# There should normally only be one changed build per commit, but this
# rule may get broken when the repo's frozen pending a new Slackware
# release. So use a loop.

#######################################################################
set -e
exec 1>&2

check_info_file() {
  local file="$1" # this is the modified version
  local origfile="$1".old.$$ # this will be the original
  local cksum

  # could be an array but I refuse to use bash's hideous array syntax:
  local errmsg

  # if the file was removed, don't try to check it.
  [ -e "$file" ] || return 0

  # reconstitute the file as it was before this commit
  cp "$file" "$origfile"
  git diff --cached "$file" | patch -s -R -p3 "$origfile"

  # if the file just added, $origfile won't exist after patching.
  [ -e "$origfile" ] || return 0

  source "$origfile"
  oldver="$VERSION"
  oldmd5="$MD5SUM"
  oldmd5_64="$MD5SUM_x86_64"
  rm -f "$origfile"

  source "$file"
  if [ "$VERSION" != "$oldver" ]; then
    if [ -n "$oldmd5" -a -n "$MD5SUM" -a "$oldmd5" = "$MD5SUM" ]; then
      errmsg+="    VERSION was updated but MD5SUM was not!\n"
    fi
    if [ -n "$oldmd5_64" -a -n "$MD5SUM_x86_64" -a "$oldmd5_64" = "$MD5SUM_x86_64" ]; then
      errmsg+="    VERSION was updated but MD5SUM_x86_64 was not!\n"
    fi
  fi

  if [ -z "$MD5SUM" -a -z "$MD5SUM_x86_64" ]; then
    errmsg+="     MD5SUM and MD5SUM_x86_64 are both blank!\n"
  else
    # no quotes: we want to break on whitespace-separated words here.
    for cksum in $MD5SUM $MD5SUM_x86_64; do
      [[ $cksum =~ ^[a-z0-9]{32}$ ]] || errmsg+="    Invalid md5sum: $cksum\n"
    done
  fi

  if [ -n "$errmsg" ]; then
    echo -e "*** $file:\n$errmsg"
    cat <<EOF
Commit aborted. Either update the checksum(s) and retry the commit, or
if you're 100% sure it's correct, rerun the commit with MD5OK=yes in
the environment.
EOF
    exit 1
  fi
}

# Note to self: Don't get tempted to add too many checks here. Anything
# that could possibly be checked by sbolint, should be. The md5sum
# check can't go in sbolint because it has to be able to look back in
# the git history.

# The fugly grep regex below matches only the actual .info files, named
# category/prgnam/prgnam.info (and not other files named .info).
if [ "${MD5OK:-no}" != "yes" ]; then
	while read filename; do
		check_info_file "$filename"
	done < <(git diff --cached --name-only | grep '^[^/]*/\([^/]*\)/\1.info' || exit 0)
fi

if [ "${SBOLINT:-yes}" = "yes" ]; then
	sbolintfailed=""
	if ! which sbolint &>/dev/null; then
		echo "WARNING: can't find sbolint in PATH, no linting will be done"
	else
		while read build; do
			# If there's no slack-desc or README, assume the build was removed.
			# The directory still might exist after a "git rm -rf" because it
			# might contain untracked files (e.g. the source tarball).
			if [ -e "$build/slack-desc" -o -e "$build/README" ]; then
				sbolint "$build" || sbolintfailed=1
			fi
		done < <(git diff --cached --name-only | cut -d/ -f1,2 | sort -u)
	fi
	if [ -n "$sbolintfailed" ]; then
		echo "*** sbolint failed, fix the errors or set SBOLINT=no"
		echo "*** in the environment to commit anyway."
		exit 1
	fi
fi

exit 0
