#!/bin/sh
#
# po4aman-display-po permits to see how a manpage translated with po4a
# will be displayed, without needing to uncompress the original manpage
# and call po4a-translate.
#

OPTIONS="";

function usage() {
  echo "Usage: $0 -p PO_FILE [-m MASTER_FILE] [-o PO4A_OPT]";
  return 0;
}

while getopts m:p:ho: option
do
  case $option in
    m)
      MASTER=$OPTARG;
      ;;
    p)
      PO=$OPTARG;
      ;;
    o)
      OPTIONS="-o $OPTARG $OPTIONS";
      ;;
    h)
      usage;
      exit 0;
      ;;
    [?])
      usage 1>&2;
      exit 1;
      ;;
  esac
done

if [ -z "$MASTER" ] && [ -n "$PO" ]
then
  echo "No manpage specified."
  echo "Trying to find the manpage according to a line reference in the PO."
  file=$(grep -m 1 "^#:" $PO | sed -e 's/^.* \(.*\):.*$/\1/')
  file=$(basename $file)
  ext=""
  echo Looking for manpage: $file
  MASTER=$(man -w $file)
  if [ $? != "0" ]
  then
    ext=$(echo $file | sed -e 's/^.*\.\([1-9].*\)$/\1/' -e 's/\..*//')
    file=$(echo $file | sed -e 's/\.[1-9].*//')
    echo looking for manpage $file in section $ext
    MASTER=$(man -w $ext $file)
    if [ $? != "0" ]
    then
      file=$(echo $file | sed -e 's/\..*//')
      echo looking for manpage $file in section $ext
      MASTER=$(man -w $ext $file)
      if [ $? != "0" ]
      then
        echo No manpage found for $PO
        echo you must provide the manpage with the -m option
        MASTER=""
      fi
    fi
  fi
  if [ -n "$MASTER" ]
  then
    echo Using: $MASTER as the original manpage
  fi
fi

# checking mandatory options
if [ -z $MASTER ];
then
  usage 1>&2;
  exit 1;
fi;
if [ -z $PO ];
then
  usage 1>&2;
  exit 1;
fi;

# checking files
if [ ! -e $MASTER ];
then
  echo "Error: could not find master file: $MASTER" 1>&2;
  exit 1;
fi;
if [ ! -e $PO ];
then
  echo "Error: could not find po file: $PO" 1>&2;
  exit 1;
fi;


if [ "${MASTER%.gz}" = "$MASTER" ];
then
  MAINNAME=$MASTER;
else
  MAINNAME=`mktemp`;
  trap "rm $MAINNAME" EXIT SIGINT;
  gunzip -c $MASTER > $MAINNAME;
fi;

CHARSET_MASTER=`file -i $MAINNAME | cut -d "=" -f 2`;
CHARSET_PO=`file -i $PO | cut -d "=" -f 2`;

po4a-translate -f man -k 0 -m $MAINNAME -M $CHARSET_MASTER \
  -p $PO $OPTIONS| iconv -f $CHARSET_PO -t // | man -l -
