#!/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=" \n" NORMAL_SUFFIXES_STRING_ARRAY_HEADER=" \n" WILD_CARD_SUFFIXES_STRING_ARRAY_HEADER=" \n" STRING_ARRAY_FOOTER=" \n" STRING_RESOURCE_FOOTER="\n" STRING_RESOURCE_HEADER="\n\n\n" # Setup the header of the file. newFileContents=$STRING_RESOURCE_HEADER exceptionSuffixes=() normalSuffixes=() wildcardSuffixes=() addItemToContents() { newFileContents+=" $1\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"