#!/bin/bash

# This shell script is for parsing `public_suffix_list.dat` and writing its contents into a string
# array resource. We need these public suffixes for extracting a domain name from a URI. For more
# information about why that is, checkout the original PR description here:
# https://github.com/bitwarden/android/pull/842.
#
# For an updated `public_suffix_list.dat` file, go here: https://publicsuffix.org/list/index.html

RAW_PREFIXES_DATA_PATH="public_suffix_list.dat"
RESOURCE_OUTPUT_PATH="../app/src/main/res/values/public_suffix_list.xml"
EXCEPTION_SUFFIXES_STRING_ARRAY_HEADER="    <string-array name=\"exception_suffixes\">\n"
NORMAL_SUFFIXES_STRING_ARRAY_HEADER="    <string-array name=\"normal_suffixes\">\n"
WILD_CARD_SUFFIXES_STRING_ARRAY_HEADER="    <string-array name=\"wild_card_suffixes\">\n"
STRING_ARRAY_FOOTER="    </string-array>\n"
STRING_RESOURCE_FOOTER="</resources>\n"
STRING_RESOURCE_HEADER="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!--\n    A list of valid public domain suffixes. This file is generated by running\n    \`scripts/public_suffix_list_generator.sh\` \n-->\n<resources>\n"

# Setup the header of the file.
newFileContents=$STRING_RESOURCE_HEADER

exceptionSuffixes=()
normalSuffixes=()
wildcardSuffixes=()

addItemToContents() {
    newFileContents+="        <item>$1</item>\n"
}

while IFS= read -r string || [[ -n "$string" ]]; do
    if [[ -z "$string" || "$string" == "//"* ]]; then
        # Remove comments and empty lines
        continue
    elif [[ "$string" == "*"* ]]; then
        # Drop the "*."
        wildcardSuffixes+=("${string#??}")
    elif [[ "$string" == "!"* ]]; then
        # Drop the "!."
        exceptionSuffixes+=("${string#?}")
    else
        normalSuffixes+=("$string")
    fi
done < "$RAW_PREFIXES_DATA_PATH"

# Add all exception suffix items to the new file contents.
if [ ${#exceptionSuffixes[@]} -gt 0 ]; then
    newFileContents+=$EXCEPTION_SUFFIXES_STRING_ARRAY_HEADER
    for item in "${exceptionSuffixes[@]}"; do
        addItemToContents "$item"
    done
    newFileContents+=$STRING_ARRAY_FOOTER
fi

# Add all normal suffix items to the new file contents.
if [ ${#normalSuffixes[@]} -gt 0 ]; then
    newFileContents+=$NORMAL_SUFFIXES_STRING_ARRAY_HEADER
    for item in "${normalSuffixes[@]}"; do
        addItemToContents "$item"
    done
    newFileContents+=$STRING_ARRAY_FOOTER
fi

# Add all wild card suffix items to the new file contents.
if [ ${#wildcardSuffixes[@]} -gt 0 ]; then
    newFileContents+=$WILD_CARD_SUFFIXES_STRING_ARRAY_HEADER
    for item in "${wildcardSuffixes[@]}"; do
        addItemToContents "$item"
    done
    newFileContents+=$STRING_ARRAY_FOOTER
fi

# Setup the footer of the file.
newFileContents+=$STRING_RESOURCE_FOOTER

# Write the contents to the output file.
echo -e "$newFileContents" > "$RESOURCE_OUTPUT_PATH"
