set -e

output_changes () {
	if test -n "$1"; then
		export LC_TIME="$1"
	fi

	AM_PM=`locale am_pm`
	D_T_FMT=`locale d_t_fmt`
	D_FMT=`locale d_fmt`
	T_FMT=`locale t_fmt`
	T_FMT_AMPM=`locale t_fmt_ampm`
	MON=`locale mon`
	ABMON=`locale abmon`
	DAY=`locale day`
	ABDAY=`locale abday`

	AM_STR="`echo $AM_PM | cut -d\; -f1`"
	PM_STR="`echo $AM_PM | cut -d\; -f2`"
	for i in `seq 1 7`; do
		export MON_${i}="`echo $MON | cut -d\; -f${i}`"
		export ABMON_${i}="`echo $ABMON | cut -d\; -f${i}`"
	done
	for i in `seq 1 7`; do
		export DAY_${i}="`echo $DAY | cut -d\; -f${i}`"
		export ABDAY_${i}="`echo $ABDAY | cut -d\; -f${i}`"
	done

	cat <<-EOF
		msgid ""
		msgstr "Content-Type: text/plain; charset=UTF-8\n"

		msgctxt "GDateTime"
		msgid "AM"
		msgstr "${AM_STR}"

		msgctxt "GDateTime"
		msgid "PM"
		msgstr "${PM_STR}"

		msgctxt "GDateTime"
		msgid "%a %b %e %H:%M:%S %Y"
		msgstr "${D_T_FMT}"

		msgctxt "GDateTime"
		msgid "%m/%d/%y"
		msgstr "${D_FMT}"

		msgctxt "GDateTime"
		msgid "%H:%M:%S"
		msgstr "${T_FMT}"

		msgctxt "GDateTime"
		msgid "%I:%M:%S %p"
		msgstr "${T_FMT_AMPM}"

		msgctxt "full month name"
		msgid "January"
		msgstr "${MON_1}"

		msgctxt "full month name"
		msgid "February"
		msgstr "${MON_2}"

		msgctxt "full month name"
		msgid "March"
		msgstr "${MON_3}"

		msgctxt "full month name"
		msgid "April"
		msgstr "${MON_4}"

		msgctxt "full month name"
		msgid "May"
		msgstr "${MON_5}"

		msgctxt "full month name"
		msgid "June"
		msgstr "${MON_6}"

		msgctxt "full month name"
		msgid "July"
		msgstr "${MON_7}"

		msgctxt "full month name"
		msgid "August"
		msgstr "${MON_8}"

		msgctxt "full month name"
		msgid "September"
		msgstr "${MON_9}"

		msgctxt "full month name"
		msgid "October"
		msgstr "${MON_10}"

		msgctxt "full month name"
		msgid "November"
		msgstr "${MON_11}"

		msgctxt "full month name"
		msgid "December"
		msgstr "${MON_12}"

		msgctxt "abbreviated month name"
		msgid "Jan"
		msgstr "${ABMON_1}"

		msgctxt "abbreviated month name"
		msgid "Feb"
		msgstr "${ABMON_2}"

		msgctxt "abbreviated month name"
		msgid "Mar"
		msgstr "${ABMON_3}"

		msgctxt "abbreviated month name"
		msgid "Apr"
		msgstr "${ABMON_4}"

		msgctxt "abbreviated month name"
		msgid "May"
		msgstr "${ABMON_5}"

		msgctxt "abbreviated month name"
		msgid "Jun"
		msgstr "${ABMON_6}"

		msgctxt "abbreviated month name"
		msgid "Jul"
		msgstr "${ABMON_7}"

		msgctxt "abbreviated month name"
		msgid "Aug"
		msgstr "${ABMON_8}"

		msgctxt "abbreviated month name"
		msgid "Sep"
		msgstr "${ABMON_9}"

		msgctxt "abbreviated month name"
		msgid "Oct"
		msgstr "${ABMON_10}"

		msgctxt "abbreviated month name"
		msgid "Nov"
		msgstr "${ABMON_11}"

		msgctxt "abbreviated month name"
		msgid "Dec"
		msgstr "${ABMON_12}"

		msgctxt "full weekday name"
		msgid "Monday"
		msgstr "${DAY_2}"

		msgctxt "full weekday name"
		msgid "Tuesday"
		msgstr "${DAY_3}"

		msgctxt "full weekday name"
		msgid "Wednesday"
		msgstr "${DAY_4}"

		msgctxt "full weekday name"
		msgid "Thursday"
		msgstr "${DAY_5}"

		msgctxt "full weekday name"
		msgid "Friday"
		msgstr "${DAY_6}"

		msgctxt "full weekday name"
		msgid "Saturday"
		msgstr "${DAY_7}"

		msgctxt "full weekday name"
		msgid "Sunday"
		msgstr "${DAY_1}"

		msgctxt "abbreviated weekday name"
		msgid "Mon"
		msgstr "${ABDAY_2}"

		msgctxt "abbreviated weekday name"
		msgid "Tue"
		msgstr "${ABDAY_3}"

		msgctxt "abbreviated weekday name"
		msgid "Wed"
		msgstr "${ABDAY_4}"

		msgctxt "abbreviated weekday name"
		msgid "Thu"
		msgstr "${ABDAY_5}"

		msgctxt "abbreviated weekday name"
		msgid "Fri"
		msgstr "${ABDAY_6}"

		msgctxt "abbreviated weekday name"
		msgid "Sat"
		msgstr "${ABDAY_7}"

		msgctxt "abbreviated weekday name"
		msgid "Sun"
		msgstr "${ABDAY_1}"
	EOF
}

do_update () {
	echo -n "Updating $1 (from $2) "
	sed -e '/^$/,$d' $1 > header
	output_changes $2 > changes
	msgcat --use-first header changes $1 | msgmerge - glib20.pot > merged
	mv merged $1
	rm -f header changes merged
}


do_update af.po af_ZA.utf8
do_update am.po am_ET.utf8
# which country to use for arabic?
do_update as.po as_IN.utf8
do_update ast.po ast_ES.utf8
do_update az.po az_AZ.utf8
do_update be@latin.po be_BY.utf8@latin
do_update be.po be_BY.utf8
do_update bg.po bg_BG.utf8
do_update bn_IN.po bn_IN.utf8
do_update bn.po bn_BD.utf8
do_update bs.po bs_BA.utf8
do_update ca.po ca_ES.utf8
do_update ca@valencia.po ca_ES.utf8
do_update cs.po cs_CZ.utf8
do_update cy.po cy_GB.utf8
do_update da.po da_DK.utf8
do_update de.po de_DE.utf8      # one of many choices...
do_update dz.po dz_BT.utf8
do_update el.po el_GR.utf8
do_update en_CA.po en_CA.utf8
do_update en_GB.po en_GB.utf8
# shavian?
# esperanto?
do_update es.po es_ES.utf8      # one of many choices...
do_update et.po et_EE.utf8
do_update eu.po eu_ES.utf8
do_update fa.po fa_IR.utf8
do_update fi.po fi_FI.utf8
do_update fr.po fr_FR.utf8      # one of many choices...
do_update ga.po ga_IE.utf8
do_update gl.po gl_ES.utf8
do_update gu.po gu_IN.utf8
do_update he.po he_IL.utf8
do_update hi.po hi_IN.utf8
do_update hr.po hr_HR.utf8
do_update hu.po hu_HU.utf8
do_update hy.po hy_AM.utf8
do_update id.po id_ID.utf8
do_update is.po is_IS.utf8
do_update it.po it_IT.utf8
do_update ja.po ja_JP.utf8
do_update ka.po ka_GE.utf8
do_update kk.po kk_KZ.utf8
do_update kn.po kn_IN.utf8
do_update ko.po ko_KR.utf8
do_update ku.po ku_TR.utf8
do_update lt.po lt_LT.utf8
do_update lv.po lv_LV.utf8
do_update mai.po mai_IN.utf8
do_update mg.po mg_MG.utf8
do_update mk.po mk_MK.utf8
do_update ml.po ml_IN.utf8
do_update mn.po mn_MN.utf8
do_update mr.po mr_IN.utf8
do_update ms.po ms_MY.utf8
do_update nb.po nb_NO.utf8
do_update nds.po nds_DE.utf8
do_update ne.po ne_NP.utf8
do_update nl.po nl_NL.utf8
do_update nn.po nn_NO.utf8
do_update oc.po oc_FR.utf8
do_update or.po or_IN.utf8
do_update pa.po pa_IN.utf8      # also spoken in pakistan...
do_update pl.po pl_PL.utf8
do_update ps.po ps_AF.utf8
do_update pt_BR.po pt_BR.utf8
do_update pt.po pt_PT.utf8
do_update ro.po ro_RO.utf8
do_update ru.po ru_RU.utf8
do_update rw.po rw_RW.utf8
do_update si.po si_LK.utf8
do_update sk.po sk_SK.utf8
do_update sl.po sl_SI.utf8
do_update sq.po sq_AL.utf8
do_update sr@ije.po sr_RS.utf8
do_update sr@latin.po sr_RS.utf8@latin
do_update sr.po sr_RS.utf8
do_update sv.po sv_SE.utf8      # also spoken in finland...
do_update ta.po ta_IN.utf8
do_update te.po te_IN.utf8
do_update th.po th_TH.utf8
do_update tl.po tl_PH.utf8
do_update tr.po tr_TR.utf8
do_update tt.po tt_RU.utf8
do_update ug.po ug_CN.utf8
do_update uk.po uk_UA.utf8
do_update vi.po vi_VN.utf8
do_update wa.po wa_BE.utf8
do_update xh.po xh_ZA.utf8
do_update yi.po yi_US.utf8
do_update zh_CN.po zh_CN.utf8
do_update zh_HK.po zh_HK.utf8
do_update zh_TW.po zh_TW.utf8
