#!/bin/sh -e
# vim:set ts=2 sw=2 sts=2 ai si et:
MSGCAT="/usr/bin/msgcat"
MSGATTRIB="/usr/bin/msgattrib"

# merge older PO file in HEAD with newer PO in TREEISH (keep fuzzy are taken from older PO)
merge1po () {
  # $1: PO file name
  # $2: treeish
  if [ -z "$1" ]; then
    # no PO
    echo "E: must specify PO"
    help
    exit 126
  fi
  if [ "$1" = "${1%.po}" ]; then
    # not PO file
    return
  fi
  if [ -z "$2" ]; then
    # no TREEISH
    echo "E: must specify TREEISH for merge1po() for $1"
    help
    exit 127
  fi
  PO="$1"
  LOCAL_TREEISH="$2"
  rm -f ${PO}_base0
  rm -f ${PO}_base1
  rm -f ${PO}_base2
  git checkout HEAD ${PO}
  ${MSGATTRIB} --no-obsolete -o ${PO}_base1 ${PO}
  git checkout ${LOCAL_TREEISH} ${PO}
  ${MSGATTRIB} --translated --no-fuzzy --no-obsolete -o ${PO}_base0 ${PO}
  rm -f ${PO}
  ${MSGCAT} -o ${PO} --use-first ${PO}_base0 ${PO}_base1
}
help () {
  echo "SYNTAX:"
  echo "  ${0##*/} PO TREEISH"
  echo ""
  echo "DESCRIPTION"
  echo "    Replace msgstr of PO in the current directory with"
  echo "    only no-fuzzy msgstr of PO in TREEISH"
  echo ""
  echo "    Both sides should be updated to the common English content."
  echo ""
}

merge1po ${1} ${2}
