#!/bin/bash

set -e

pdiff () {
  local one="$1";
  local two="$2";
  local pipe="$3";
  shift 3;
  diff "$@" <(
    cat "$one" \
    | bash -c "$pipe"
  ) <(
    cat "$two" \
    | bash -c "$pipe"
  )
}

unset commit_message
if [ $# -eq 1 ]; then
  if [ "x$1" = 'x-a' ]; then
    commit_message=$(
      unset pkgname pkgbase epoch pkgver pkgrel
      eval "$(
        git archive HEAD -- PKGBUILD \
        | tar -Ox \
        | grep '^\(pkgname\|pkgbase\|epoch\|pkgver\|pkgrel\)=[^(]\+$'
      )"
      printf '%s: %s%s-%s -> ' \
        "${pkgbase}${pkgbase:-${pkgname}}" \
        "${epoch:+${epoch}:}" \
        "${pkgver}" \
        "${pkgrel}"
      unset epoch pkgver pkgrel
      eval "$(
        grep '^\(epoch\|pkgver\|pkgrel\)=' PKGBUILD
      )"
      printf '%s%s-%s' \
        "${epoch:+${epoch}:}" \
        "${pkgver}" \
        "${pkgrel}"
    )
  else
    commit_message="$1"
  fi
elif [ $# -ne 0 ]; then
  >&2 echo 'usage: commit-package [commit-message]'
  >&2 echo '  without a commit message, it will only be added for commit'
  exit 1
fi

if [ -d '.git' ] || [ -f '.git' ]; then
  if ! git remote \
    | grep -qxF aur \
    || ! git remote \
    | grep -qxF ecknernet; then
    >&2 echo 'I cannot find the necessary remotes: aur and ecknernet'
    exit 1
  fi
  if ! git branch --format='%(refname:lstrip=-1)' \
    | grep -qxF master \
    || ! git branch --format='%(refname:lstrip=-1)' \
    | grep -qxF archlinuxewe; then
    >&2 echo 'I cannot find the necessary branches: master and archlinuxewe'
    exit 1
  fi
  if ! git merge-base --is-ancestor master archlinuxewe; then
    >&2 echo 'master is not an ancestor of archlinuxewe'
    exit 1
  fi
  git checkout archlinuxewe
  git pull ecknernet --ff-only
  makepkg --printsrcinfo > .SRCINFO
  git commit 'PKGBUILD' '.SRCINFO' -m "${commit_message:-rebuild}"
  git checkout master
  git pull --ff-only
  diff=$(
    git diff archlinuxewe{^,} --name-status \
    | cut -f2 \
    | grep -vxF .SRCINFO \
    | grep -vxF PKGBUILD \
    | xargs -r git diff archlinuxewe{^,} --
  )
  if [ -n "${diff}" ]; then
    printf '%s\n' "${diff}" \
    | git apply
  fi
  pdiff <(
    git archive archlinuxewe^ -- PKGBUILD
  ) <(
    git archive archlinuxewe -- PKGBUILD
  ) '
    tar -Ox \
    | sed "
      /^_pinned_dependencies=(/,/)/d
      /^\(\s*'"'"'python\(>=\|<\)[0-9.]\+'"'"'\s*\)$/{
        N
        s/^\(\s*'"'"'python\)\(>=\|<\)[0-9.]\+'"'"'\s*\n\s*'"'"'python\(>=\|<\)[0-9.]\+\('"'"'\s*)\?\s*\)$/\1\4/
        t
        s/^\(\s*'"'"'python\)\(>=\|<\)[0-9.]\+\('"'"'\s*)\?\s*\)$/\1\3/
      }
    "
  ' -u \
  | sed '
    s@^\(---\|+++\) /dev/fd/\S\+\s\+@\1 a/PKGBUILD @
  ' \
  | git apply
  makepkg --printsrcinfo > .SRCINFO
  git commit 'PKGBUILD' '.SRCINFO' -m "${commit_message:-rebuild}"
  git push aur
  git push ecknernet
  git checkout archlinuxewe
  git rebase master
  git push ecknernet --force
  paket=$(basename "$(pwd)")
  cd ..
  if [ -n "${commit_message}" ]; then
    git commit "${paket}" -m "${commit_message}"
  else
    git add "${paket}"
  fi
else
  if [ -n "${commit_message}" ]; then
    git commit 'PKGBUILD' -m "${commit_message}"
  else
    git add 'PKGBUILD'
  fi
fi