xref: /aosp_15_r20/external/sqlite/common-functions.sh (revision a3141fd39888aecc864dfb08485df64ff6c387f9)
1#!/bin/bash
2#
3# Copyright (C) 2024 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# Some common functions used by the source-update scripts.
18#
19
20die() {
21    echo "$script_name: $*"
22    exit 1
23}
24
25echo_and_exec() {
26    echo "  Running: $@"
27    "$@"
28}
29
30validate_year() {
31  local year=$1
32  if [[ "$year" =~ ^2[0-9][0-9][0-9]$ ]]; then
33    return 0;
34  else
35    return 1;
36  fi
37}
38
39# This function converts a release string like "3.42.0" to the canonical 7-digit
40# format used by sqlite.org for downloads: "3420000".  A hypothetical release
41# number of 3.45.6 is converted to "3450600".  A hypothetical release number of
42# 3.45.17 is converted to "3451700".  The last two digits are assumed to be
43# "00" for now, as there are no known counter-examples.
44function normalize_release {
45  local version=$1
46  local -a fields
47  fields=($(echo "$version" | sed 's/\./ /g'))
48  if [[ ${#fields[*]} -lt 2 || ${#fields[*]} -gt 4 ]]; then
49    echo "cannot parse version: $version"
50    return 1
51  fi
52  if [[ ${#fields[*]} -eq 2 ]]; then fields+=(0); fi
53  if [[ ${#fields[*]} -eq 3 ]]; then fields+=(0); fi
54  printf "%d%02d%02d%02d" ${fields[*]}
55  return 0
56}
57
58function prettify_release {
59  local version=$1
60  local patch=$((version % 100))
61  version=$((version / 100))
62  local minor=$((version % 100))
63  version=$((version / 100))
64  local major=$((version % 100))
65  version=$((version / 100))
66  # version now contains the generation number.
67  printf "%d.%d.%d" $version $major $minor
68}
69