1#!/usr/bin/env bash
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Creates fake test data files by taking the files in $1 and turning them into ones
18# that appear to be ones from IANA release $2. The resulting files are placed beneath $3.
19
20REFERENCE_FILES_DIR=$1
21OUTPUT_IANA_RULES_VERSION=$2
22TEST_DATA_OUTPUT_DIR=$3
23
24# Fail on error
25set -e
26
27TZDATA_FILE=iana/tzdata
28TZDATA_LICENSE_FILE=iana/NOTICE
29ICU_FILE=icu_overlay/icu_tzdata.dat
30ICU_LICENSE_FILE=icu_overlay/LICENSE
31TZLOOKUP_FILE=android/tzlookup.xml
32TELEPHONYLOOKUP_FILE=android/telephonylookup.xml
33
34INPUT_TZDATA_FILE=${REFERENCE_FILES_DIR}/${TZDATA_FILE}
35INPUT_ICU_FILE=${REFERENCE_FILES_DIR}/${ICU_FILE}
36INPUT_TZLOOKUP_FILE=${REFERENCE_FILES_DIR}/${TZLOOKUP_FILE}
37INPUT_TELEPHONYLOOKUP_FILE=${REFERENCE_FILES_DIR}/${TELEPHONYLOOKUP_FILE}
38
39TZHEADER=$(head -n1 ${INPUT_TZDATA_FILE} | cut -c1-11)
40INPUT_IANA_RULES_VERSION=${TZHEADER:6}
41
42OUTPUT_TZDATA_FILE=${TEST_DATA_OUTPUT_DIR}/${TZDATA_FILE}
43OUTPUT_ICU_FILE=${TEST_DATA_OUTPUT_DIR}/${ICU_FILE}
44OUTPUT_TZLOOKUP_FILE=${TEST_DATA_OUTPUT_DIR}/${TZLOOKUP_FILE}
45OUTPUT_TELEPHONYLOOKUP_FILE=${TEST_DATA_OUTPUT_DIR}/${TELEPHONYLOOKUP_FILE}
46
47mkdir -p ${TEST_DATA_OUTPUT_DIR}
48mkdir -p ${TEST_DATA_OUTPUT_DIR}/iana
49mkdir -p ${TEST_DATA_OUTPUT_DIR}/icu_overlay
50mkdir -p ${TEST_DATA_OUTPUT_DIR}/android
51
52# Create a new tzdata file.
53sed "1s/^tzdata${INPUT_IANA_RULES_VERSION}/tzdata${OUTPUT_IANA_RULES_VERSION}/" ${INPUT_TZDATA_FILE} > ${OUTPUT_TZDATA_FILE}
54
55# Create new ICU file.
56SEARCH=$(echo ${INPUT_IANA_RULES_VERSION} | sed "s/\(.\)/\1\\\x00/g")
57REPLACE=$(echo ${OUTPUT_IANA_RULES_VERSION} | sed "s/\(.\)/\1\\\x00/g")
58sed "s/$SEARCH/$REPLACE/" ${INPUT_ICU_FILE} > ${OUTPUT_ICU_FILE}
59
60# Copy the tzlookup.xml but update the ianaversion= attribute.
61sed "s/${INPUT_IANA_RULES_VERSION}/${OUTPUT_IANA_RULES_VERSION}/" ${INPUT_TZLOOKUP_FILE} > ${OUTPUT_TZLOOKUP_FILE}
62
63# Copy the telephonylookup.xml file. No updates are required.
64cp ${INPUT_TELEPHONYLOOKUP_FILE} ${OUTPUT_TELEPHONYLOOKUP_FILE}
65
66# Copy license files
67cp ${REFERENCE_FILES_DIR}/${TZDATA_LICENSE_FILE} ${TEST_DATA_OUTPUT_DIR}/${TZDATA_LICENSE_FILE}
68cp ${REFERENCE_FILES_DIR}/${ICU_LICENSE_FILE} ${TEST_DATA_OUTPUT_DIR}/${ICU_LICENSE_FILE}
69
70echo Transformed input files with version ${INPUT_IANA_RULES_VERSION} to ${OUTPUT_IANA_RULES_VERSION} in ${TEST_DATA_OUTPUT_DIR}
71
72