1#!/usr/bin/env bash
2
3# Copyright 2020 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
17set -e
18SCRIPT_PATH=$(realpath $0)
19
20CURL_CMD=curl
21SERVER_BASE_URL=https://download.geonames.org/export/dump/
22FILES=( \
23  cities1000.zip \
24  cities15000.zip \
25  cities500.zip \
26  cities5000.zip \
27  readme.txt \
28  )
29
30LOCAL_ANDROID_DIR=$(dirname ${SCRIPT_PATH})
31LOCAL_ROOT_DIR=$(dirname ${LOCAL_ANDROID_DIR})
32LOCAL_DOWNLOAD_DIR=${LOCAL_ROOT_DIR}/export/dump
33LOCAL_VERSION_INFO_DIR=${LOCAL_ANDROID_DIR}/version_info
34METADATA_FILE=${LOCAL_ROOT_DIR}/METADATA
35
36echo Removing old downloaded files and metadata...
37rm ${LOCAL_DOWNLOAD_DIR}/*
38rm ${LOCAL_VERSION_INFO_DIR}/*
39
40# Start the METADATA file
41cat << EOF > ${METADATA_FILE}
42// Generated file DO NOT EDIT
43// Run android/download_latest.sh
44name: "geonames"
45description:
46    "Assorted files downloaded from https://www.geonames.org/"
47
48third_party {
49  url {
50    type: HOMEPAGE
51    value: "https://www.geonames.org/"
52  }
53EOF
54
55DOWNLOAD_DATE=$(date --iso-8601)
56for FILE in ${FILES[@]}; do
57  URL=${SERVER_BASE_URL}${FILE}
58  HEADERS_FILE=${LOCAL_VERSION_INFO_DIR}/${FILE}.headers
59  RELATIVE_HEADERS_FILE=$(realpath --relative-to ${LOCAL_ROOT_DIR} ${HEADERS_FILE})
60
61  echo Downloading ${FILE}...
62  ${CURL_CMD} ${URL} \
63      --output ${LOCAL_DOWNLOAD_DIR}/${FILE} \
64      --dump-header ${HEADERS_FILE}
65
66  SHA=$(sha1sum ${LOCAL_DOWNLOAD_DIR}/${FILE} | awk '{ print $1}')
67
68  # Add a METADATA file URL entry for every file we download.
69  cat << EOF >> ${METADATA_FILE}
70  url {
71    type: OTHER
72    value: "Downloaded from ${URL} on ${DOWNLOAD_DATE}, SHA1=${SHA}, see ${RELATIVE_HEADERS_FILE}"
73  }
74EOF
75
76done
77
78FILE_LAST_MODIFIED_HEADER=$(grep -i 'Last-Modified' ${LOCAL_VERSION_INFO_DIR}/${FILES[0]}.headers)
79LAST_MODIFIED_DATE_TIME=$(echo ${FILE_LAST_MODIFIED_HEADER} | sed 's/Last-Modified: //')
80PROTO_LAST_MODIFIED_DATE=$(date --date="${LAST_MODIFIED_DATE_TIME}" +'{ year: '%Y' month: '%-m' day: '%-d' }')
81
82# Finish the METADATA file
83cat << EOF >> ${METADATA_FILE}
84  version: "As of ${DOWNLOAD_DATE}"
85  last_upgrade_date ${PROTO_LAST_MODIFIED_DATE}
86  license_type: NOTICE
87}
88EOF
89