>From 8f9ffd332bcfc2e2ef9b09e363b5b17510dd7bdd Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sun, 3 May 2015 23:44:05 -0700
Subject: [PROPOSED PATCH 2/2] Better fix for tzselect in non-UTF-8 locales

* NEWS: Document this.
* tzselect.ksh (TZ_COUNTRY_TABLE, TZ_ZONE_TABLE):
If the current locale is not UTF-8, convert these tables
to the current locale's encoding before using them.
That's better than switching ths ehell to UTF-8, which
may not work if the current terminal doesn't support it.
Problem reported by Random832 in:
http://mm.icann.org/pipermail/tz/2015-May/022260.html
---
 NEWS         |  3 ++-
 tzselect.ksh | 27 +++++++++++++++------------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index b77cd03..fc7773d 100644
--- a/NEWS
+++ b/NEWS
@@ -9,7 +9,8 @@ Unreleased, experimental changes
 
   Changes affecting code
 
-    tzselect aligns UTF-8 columns better, if a UTF-8 locale is available.
+    When displaying data, tzselect converts it to the current locale's
+    encoding if the iconv command works.  (Problem reported by random832.)
 
 
 Release 2015d - 2015-04-24 08:09:46 -0700
diff --git a/tzselect.ksh b/tzselect.ksh
index 8589a72..0d46ed7 100644
--- a/tzselect.ksh
+++ b/tzselect.ksh
@@ -50,18 +50,6 @@ say() {
 	exit 1
 }
 
-# Use a UTF-8 locale if available, as the data contain UTF-8,
-# and the shell aligns columns better that way.
-# Check the UTF-8 of U+12345 CUNEIFORM SIGN URU TIMES KI.
-utf8_locale='BEGIN { u12345 = "\360\222\215\205"; exit length(u12345) != 1 }'
-$AWK "$utf8_locale" ||
-    for locale in en_US.utf8 en_US.UTF-8 C.utf8; do
-	(LC_ALL=$locale $AWK "$utf8_locale") 2>/dev/null && {
-	    export LC_ALL=$locale
-	    break
-	}
-    done
-
 coord=
 location_limit=10
 zonetabtype=zone1970
@@ -195,6 +183,21 @@ do
 	}
 done
 
+# If the current locale does not support UTF-8, convert data to current
+# locale's format if possible, as the shell aligns columns better that way.
+# Check the UTF-8 of U+12345 CUNEIFORM SIGN URU TIMES KI.
+! $AWK 'BEGIN { u12345 = "\360\222\215\205"; exit length(u12345) != 1 }' &&
+    { tmp=`(mktemp -d) 2>/dev/null` || {
+	tmp=${TMPDIR-/tmp}/tzselect.$$ &&
+	(umask 77 && mkdir -- "$tmp")
+    };} &&
+    trap 'status=$?; rm -fr -- "$tmp"; exit $status' 0 1 12 13 15 &&
+    (iconv -f UTF-8 -t //TRANSLIT <"$TZ_COUNTRY_TABLE" >$tmp/iso3166.tab) \
+        2>/dev/null &&
+    TZ_COUNTRY_TABLE=$tmp/iso3166.tab &&
+    iconv -f UTF-8 -t //TRANSLIT <"$TZ_ZONE_TABLE" >$tmp/$zonetabtype.tab &&
+    TZ_ZONE_TABLE=$tmp/$zonetabtype.tab
+
 newline='
 '
 IFS=$newline
-- 
2.1.4

