summaryrefslogtreecommitdiffstats
path: root/scripts/po4a-display-man
blob: 76dfe7b989e7e3b926ff170f6cc8f1062b70887c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/sh
#
# po4a-display-man shows a translated manpage, recompiling everything on the fly with po4a.
# Translators can see the effects of their work without reinstalling everything.
#

OPTIONS=""

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

error () {
  echo "Error: $1" 1>&2
  exit 1
}

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

if [ -z "$PO" ]
then
  usage 1>&2
  exit 1
fi

if [ ! -f "$PO" ]
then
  error "could not find PO file: $PO"
fi

if [ -z "$MASTER" ]
then
  echo "No manpage specified."
  echo "Trying to find the manpage according to a line reference in the PO."
  MASTER=$(grep -m 1 "^#:" $PO | sed -e 's/^.* \(.*\):.*$/\1/')
fi

find_man()
{
  section="$1"
  man="$2"
  echo -n "Looking for manpage $file"
  [ -n "$section" ] && echo " in section $section" || echo ""
  MAN_NUMBER=`man --all --where --locale=C $section "$man" 2> /dev/null | wc -l`

  if [ "$MAN_NUMBER" = "0" ]
  then
    return 1
  elif [ "$MAN_NUMBER" != "1" ]; then
    MAN_NUMBER=`man --all --where --locale=C $section "$man" 2> /dev/null | \
                grep "\/$man\.$section\(.gz\)\?$" | wc -l`
    if [ "$MAN_NUMBER" = "1" ]
    then
      MASTER=`man --where --locale=C "$section" "$man" | grep "\/$man\.$section\(.gz\)\?$"`
      echo "Multiple manpages in section $section: $(man -aw -L C $section "$man"), only one exactly matches the section."
    else
      error "Too many possible manpages: $(man -aw -L C $section "$man"), you must specify the manpage or the section"
    fi
  else
    MASTER=`man --where --locale=C "$section" "$man"`
  fi

  return 0
}

if [ ! -f "$MASTER" ]
then
  echo "Can't find the master man page $MASTER"
  file=$(basename "$MASTER")
  MASTER=""
  section=""
  if ! find_man "" "$file"
  then
    # Maybe $file contains a section
    # (in the form of file.section.extension)
    section=$(echo "$file" | sed -ne 's/^.*\.\([1-9].*\)$/\1/; s/\..*//; p')
    file=$(echo "$file" | sed -e 's/\.[1-9].*//')
    if ! find_man "$section" "$file"
    then
      # Maybe there is an extension at the end of the manpage (e.g. .man)
      file=$(echo "$file" | sed -e 's/\..*//')
      if ! find_man "$section" "$file"
      then
        echo "No manpage found"
        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

# checking files
if [ ! -e "$MASTER" ]
then
  error "could not find master file: $MASTER"
fi


if [ "${MASTER%.gz}" = "$MASTER" ]
then
  MAINNAME="$MASTER"
else
  MAINNAME=`mktemp`
  trap "rm \"$MAINNAME\"" EXIT INT
  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 -

# TODO: man may need some options (e.g. -L)
#       as we cannot detect what options are needed, they will have to be
#       specified on the po4a-display-man command line.