1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 2000-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                 Glenn Fowler <gsf@research.att.com>                  #
18#                                                                      #
19########################################################################
20: message catalog administration
21
22command=msgadmin
23
24case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in
250123)	ARGV0="-a $command"
26	USAGE=$'
27[-?
28@(#)$Id: msgadmin (AT&T Labs Research) 2001-06-08 $
29]
30'$USAGE_LICENSE$'
31[+NAME?'$command$' - message catalog file administration]
32[+DESCRIPTION?\b'$command$'\b administers message catalog files. If no \afile\a
33	operands are specified then all message files in the local
34	\b$INSTALLROOT\b source tree are operated on. Exactly one of
35	\b--generate\b, \b--remove\b, \b--translate\b, or \b--verify\b
36	must be specified.]
37[D:debug?Passed to \btranslate\b(1).]
38[a:all?Passed to \btranslate\b(1).]
39[c:cache?Passed to \btranslate\b(1).]
40[d:dialect?Operate on the dialects in the \b,\b separated \adialect\a list.
41	\b-\b means all dialects supported by \btranslate\b(1).]:[dialect:=-]
42[f:force?Force binary catalog generation even when the current binary is newer
43	than the source.]
44[g:generate?Generate and install \bgencat\b(1) binary message catalogs.]
45[l:list?List each installed message catalog name paired with its input source.]
46[n:show?Show commands but do not execute.]
47[o:omit?Omit \btranslate\b(1) methods matching the \bksh\b(1)
48	\apattern\a.]:[pattern]
49[r:remove?Remove all translated message files and work directories.]
50[s:share?Generate and install \bmsggen\b(1) machine independent binary
51	message catalogs.]
52[t:translate?Translate using \btranslate\b(1).]
53[v:verify?Verify that translated message files satisfy \bgencat\b(1) syntax.]
54
55[ file ... ]
56
57[+SEE ALSO?\bgencat\b(1), \bksh\b(1), \bmsggen\b(1), \btranslate\b(1)]
58'
59	;;
60*)	ARGV0=""
61	USAGE="Dcd:gno:rstv [ file ... ]"
62	;;
63esac
64
65usage()
66{
67	OPTIND=0
68	getopts $ARGV0 "$USAGE" OPT '-?'
69	exit 2
70}
71
72messages()
73{
74	if	[[ $PACKAGEROOT && -d $PACKAGEROOT ]]
75	then	MSGROOT=$PACKAGEROOT
76	else	MSGROOT=$HOME
77	fi
78	set -- $MSGROOT/arch/*/src/cmd/INIT/INIT.msg
79	[[ -f $1 ]] || { print -u2 $"$command: INIT.msg: not found"; exit 1; }
80	MSGROOT=${1%/src/cmd/INIT/INIT.msg}
81	grep -l '^1' $MSGROOT/src/@(cmd|lib)/*/*.msg
82}
83
84integer n
85typeset all cache dialect=- exec force omit op show verbose
86typeset dir=$INSTALLROOT gen=gencat
87
88while	getopts $ARGV0 "$USAGE" OPT
89do	case $OPT in
90	D)	debug=-D ;;
91	a)	all=-a ;;
92	c)	cache=-c ;;
93	d)	dialect=$OPTARG ;;
94	f)	force=1 ;;
95	g)	op=generate ;;
96	l)	op=list ;;
97	n)	exec=print show=-n ;;
98	o)	omit="-o $OPTARG" ;;
99	r)	op=remove ;;
100	s)	gen=msggen dir=$dir/share ;;
101	t)	op=translate ;;
102	v)	op=verify ;;
103	*)	usage ;;
104	esac
105done
106shift $OPTIND-1
107
108[[ $INSTALLROOT ]] || { print -u2 $"$command: INSTALLROOT not defined"; exit 1; }
109
110case $op in
111
112generate)
113	dir=$dir/lib/locale
114	[[ -d $dir ]] || { print -u2 $"$command: $dir: not found"; exit 1; }
115	(( ! $# )) && set -- C $(ls *-*.msg 2>/dev/null | sed 's,.*-\(.*\)\.msg,\1,' | sort -u)
116	owd=$PWD
117	for locale
118	do	case $locale in
119		C)	set -- $(messages) ;;
120		*)	set -- *-$locale.msg ;;
121		esac
122		if	[[ ! -f $1 ]]
123		then	print -u2 "$command: $locale: no message files"
124		else	nwd=$dir/$locale/LC_MESSAGES
125			[[ -d $nwd ]] || $exec mkdir -p $nwd || exit
126			[[ -d $nwd ]] && { cd $nwd || exit; }
127			for file
128			do	case $file in
129				/*)	name=${file##*/}
130					name=${name%*.msg}
131					;;
132				*)	name=${file%-$locale.msg}
133					file=$owd/$file
134					;;
135				esac
136				if	[[ $force || ! $name -nt $file ]]
137				then	print -u2 $locale $name:
138					$exec rm -f $name $name.*
139					$exec $gen $name $file
140				fi
141			done
142			cd $owd
143		fi
144	done
145	;;
146
147list)	messages | sed 's,^.*/\(.*\)\.msg$,\1 &,'
148	;;
149
150remove)	(( !$# )) && set -- *.msg translate.tmp
151	$exec rm -rf "$@"
152	;;
153
154translate)
155	(( !$# )) && set -- $(messages)
156	translate -lmv $all $cache $debug $omit $show $dialect "$@"
157	;;
158
159verify)	(( ! $# )) && set -- *.msg
160	for file
161	do	n=0
162		while	read -r num txt
163		do	if	[[ $num == +([0-9]) ]]
164			then	((n++))
165				if	[[ $n != $num ]]
166				then	if	(( n == $num-1 ))
167					then	print -u2 "$file: [$n] missing"
168					else	print -u2 "$file: [$n-$(($num-1))] missing"
169					fi
170					n=$num
171				fi
172			fi
173		done < $file
174	done
175	;;
176
177*)	usage
178	;;
179
180esac
181