#!/bin/bash
set -euo pipefail

# Get the absolute path of the current working directory
CURRENT_DIR=$(pwd)

# Create or clean directories
rm -rf "$CURRENT_DIR/download"
mkdir -p "$CURRENT_DIR/download"

rm -rf "$CURRENT_DIR/zoneinfo"
mkdir -p "$CURRENT_DIR/zoneinfo"

rm -rf "$CURRENT_DIR/timezones"
mkdir -p "$CURRENT_DIR/timezones"

# Change to the download directory
cd "$CURRENT_DIR/download" || exit 1

# Download the timezone data over HTTPS
TZDATA_VERSION="${VERSION:-${TZDATA_VERSION:-}}"
TZDATA_BASE_URL="https://data.iana.org/time-zones"
VERIFY_TZDATA_SIGNATURE="${VERIFY_TZDATA_SIGNATURE:-true}"
TZDATA_GPG_KEY="${TZDATA_GPG_KEY:-ED97E90E62AA7E34}"

if [ -z "$TZDATA_VERSION" ]; then
    TZDATA_VERSION="$(wget -qO- "${TZDATA_BASE_URL}/tzdb/version")"
fi

TZDATA_URL="${TZDATA_BASE_URL}/releases/tzdata${TZDATA_VERSION}.tar.gz"
TZDATA_SIG_URL="${TZDATA_URL}.asc"

echo "Downloading timezone data from ${TZDATA_URL}"
wget -O tzdata-latest.tar.gz "$TZDATA_URL"
if [ "$VERIFY_TZDATA_SIGNATURE" = "true" ]; then
    echo "Downloading timezone data signature from ${TZDATA_SIG_URL}"
    wget -O tzdata-latest.tar.gz.asc "$TZDATA_SIG_URL"

    export GNUPGHOME
    GNUPGHOME="$(mktemp -d)"
    chmod 700 "$GNUPGHOME"
    trap 'rm -rf "$GNUPGHOME"' EXIT

    gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$TZDATA_GPG_KEY"
    gpg --batch --verify tzdata-latest.tar.gz.asc tzdata-latest.tar.gz
    rm tzdata-latest.tar.gz.asc
fi

# Extract the timezone data archive
tar zxvf tzdata-latest.tar.gz
rm tzdata-latest.tar.gz

# Compile timezone data files in the correct order backward
for file in africa antarctica asia australasia europe northamerica southamerica etcetera backward; do
    INPUT_FILE="$CURRENT_DIR/download/$file"
    if [ -f "$INPUT_FILE" ]; then
        echo "Compiling $file with zic..."
        zic -d "$CURRENT_DIR/zoneinfo" "$INPUT_FILE"
    else
        echo "Warning: Expected timezone data file '$file' not found."
    fi
done

# Remove existing posixinfo file
rm -f "$CURRENT_DIR/posixinfo"

# Generate the posixinfo file
cd "$CURRENT_DIR/zoneinfo" || exit 1
for i in $(find . -type f); do
    echo -n "${i#./} " >> "$CURRENT_DIR/posixinfo"  # Strip the leading './' from the path
    tail -1 "$i" >> "$CURRENT_DIR/posixinfo"
done

# Extract timezones from zone1970.tab and create .lst files
cd "$CURRENT_DIR" || exit 1
ZONE_FILE="$CURRENT_DIR/download/zone1970.tab"

if [ -f "$ZONE_FILE" ]; then
    echo "Processing available timezones from zone1970.tab..."

    # Create dictionaries to store regions and their subregions
    declare -A REGIONS
    declare -A SUBREGIONS

    # Read the zone1970.tab file
    while read -r line; do
        # Skip comments
        [[ "$line" =~ ^# ]] && continue

        # Extract timezone (3rd column in zone1970.tab)
        TZ=$(echo "$line" | awk '{print $3}')

        # Split into components
        IFS="/" read -r REGION SUBREGION EXTRA <<< "$TZ"

        # Populate dictionaries
        if [ -z "$SUBREGION" ]; then
            # No subregion means top-level region only
            REGIONS["$REGION"]+=""
        else
            # We have at least one subregion
            REGIONS["$REGION"]+="$SUBREGION,"
            # If there's an extra level, it's a sub-subregion
            if [ -n "$EXTRA" ]; then
                SUBREGIONS["$SUBREGION"]+="$EXTRA,"
            fi
        fi
    done < "$ZONE_FILE"

    # Write the main .lst file for regions
    {
        REGION_COUNT=0
        REGION_TOTAL=${#REGIONS[@]}
        for REGION in "${!REGIONS[@]}"; do
            REGION_COUNT=$((REGION_COUNT + 1))
            REGION_SUBREGIONS_CLEAN=$(echo "${REGIONS[$REGION]}" | sed 's/,$//')
            SUBREGIONS_LIST=$(echo "$REGION_SUBREGIONS_CLEAN" | tr ',' '\n' | awk 'NF' | sort | uniq)
            REGION_SUBREGION_COUNT=$(printf '%s\n' "$SUBREGIONS_LIST" | awk 'NF { count++ } END { print count + 0 }')

            echo "$REGION:$REGION_SUBREGION_COUNT;"

            # Create region .lst files
            REGION_FILE="$CURRENT_DIR/timezones/${REGION}.lst"

            # Add subregions to each region file
            for SUB in $SUBREGIONS_LIST; do
                if [ -n "$SUB" ]; then
                    CURRENT_SUBREGIONS="${SUBREGIONS[$SUB]-}"
                    SUB_CLEAN=$(echo "$CURRENT_SUBREGIONS" | sed 's/,$//')
                    SUBSUBREGIONS=$(echo "$SUB_CLEAN" | tr ',' '\n' | awk 'NF' | sort | uniq)
                    SUB_COUNT=$(printf '%s\n' "$SUBSUBREGIONS" | awk 'NF { count++ } END { print count + 0 }')

                    echo "$SUB:$SUB_COUNT;" >> "$REGION_FILE"

                    # Create subregion .lst files for sub-sub-regions if they exist
                    if [ -n "$CURRENT_SUBREGIONS" ]; then
                        SUBREGION_FILE="$CURRENT_DIR/timezones/${SUB}.lst"
                        for SUBSUB in $SUBSUBREGIONS; do
                            if [ -n "$SUBSUB" ]; then
                                echo "$SUBSUB:0;" >> "$SUBREGION_FILE"
                            fi
                        done
                    fi
                else
                    echo "$SUB:0;" >> "$REGION_FILE"
                fi
            done
        done
    } > "$CURRENT_DIR/timezones/regions.lst"
fi

echo "Timezone files created in $CURRENT_DIR/timezones"
