#!/bin/bash
#
# Update libphonenumber for PHP's Metadata with the one shipped in the latest
# version of upstream.
#
# Usage:
#   update-php-giggsey-libphonenumber
#

set -e

YELLOW='\033[0;33m'
GREEN='\033[0;32m'
RESET='\033[0m'

if [ "$(id -u)" -ne 0 ]; then
    echo -e "${YELLOW}Please run as root.${RESET}"
    exit
fi

# set -x

print_current_metadata() {
  CURRENT_METADATA="$(tail -1 /var/lib/php-giggsey-libphonenumber/enabled/METADATA-VERSION.txt)"
  echo -e "Current METADATA version: ${GREEN}$CURRENT_METADATA (from $(readlink "/var/lib/php-giggsey-libphonenumber/enabled" | cut -d / -f 2-))${RESET}"
}

print_current_metadata

TARBALL_URL=$(curl --silent https://api.github.com/repos/giggsey/libphonenumber-for-php/releases/latest | \
    grep '"tarball_url":' | \
    sed -E 's/.*"([^"]+)".*/\1/')

if [ -z "$TARBALL_URL" ]; then
  echo -e "${YELLOW}Couldn't get upstream tarball URL. Skipping upstream download.${RESET}"
else
  TARBALL_NAME=$(basename "$TARBALL_URL")

  echo "Latest upstream METADATA version: v$TARBALL_NAME"

  CACHE_DIR=/var/cache/php-giggsey-libphonenumber
  mkdir -p "$CACHE_DIR"

  echo -ne "${YELLOW}Do you want to download the tarball from $TARBALL_URL [y/n]?${RESET} " | fmt
  read -r
  if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
    echo -n "Downloading tarball for $TARBALL_NAME (from $TARBALL_URL)... " | fmt
    if curl --silent -L "$TARBALL_URL" -o "$CACHE_DIR/$TARBALL_NAME"; then
      echo "done"

      TMP_DIR=$(mktemp -t -d php-giggsey-libphonenumber.XXXX) || exit 1
      trap "rm -r -- '$TMP_DIR'" EXIT

      echo -n "Unpacking tarball for $TARBALL_NAME... "
      tar -xf "$CACHE_DIR/$TARBALL_NAME" -C "$TMP_DIR" --strip-components=1
      echo "done"

      UPSTREAM_METADATA="$(tail -1 "$TMP_DIR/METADATA-VERSION.txt")"

      DATA_BASE_DIR="/var/lib/php-giggsey-libphonenumber/available/upstream-$UPSTREAM_METADATA"
      mkdir -p "$DATA_BASE_DIR"

      echo -n "Copying new metadata to $DATA_BASE_DIR... "
      for dir in src/data/ src/carrier/data/ src/geocoding/data/ src/timezone/data/ ; do
          if [ -e "$DATA_BASE_DIR/$dir" ]; then
              rm -r "${DATA_BASE_DIR:?}/$dir"
          fi
          mkdir -p "$DATA_BASE_DIR/$dir"
          cp -a "$TMP_DIR/$dir" "$(dirname "$DATA_BASE_DIR/$dir")"
      done
      cp "$TMP_DIR/METADATA-VERSION.txt" "$DATA_BASE_DIR"
      echo "done"
    else
      echo -e "${YELLOW}failed${RESET}"
    fi
    rm -r "$TMP_DIR"
    trap - EXIT
    rm "$CACHE_DIR/$TARBALL_NAME"
  fi
fi

print_select_dir() {
  echo
  echo "Currently selected metadata directory is:"
  echo " - $(readlink "/var/lib/php-giggsey-libphonenumber/enabled" | cut -d / -f 2-)"
  echo
  echo "List of available metadata directory(ies):"
  ls -1 "/var/lib/php-giggsey-libphonenumber/available/" | sort -V | sed -e "s/^/ - /"
  echo
  echo -en "${YELLOW}Write here the directory to use:${RESET} "
  read -r REPLY_DIR
}

while [ -z "$REPLY_DIR" ] || [ ! -d "/var/lib/php-giggsey-libphonenumber/available/$REPLY_DIR" ]; do
  print_select_dir
done

if [ ! "$REPLY_DIR" = "debian" ]; then
echo
  fmt << EOF
Selected metadata is taken from upstream. Please be aware that:

- only the upstream metadata is updated (the library code is not updated)

- in case upstream also updated the code of the library, using this metadata may not
work anymore with the code shipped in the debian package if the library has changed the way it accesses
and uses the metadata.

- This may be a security risk in case the upstream repository was compromised.

- There is no support from Debian security team for such a setup.
EOF
   echo -en "${YELLOW}Do you still want to continue [y/n]?${RESET} "
   read -r REPLY_NOT_DEBIAN
fi

if [ "$REPLY_DIR" = "debian" ] || [ "$REPLY_NOT_DEBIAN" = "y" ] || [ "$REPLY_NOT_DEBIAN" = "Y" ]; then
  rm /var/lib/php-giggsey-libphonenumber/enabled
  ln -rsf "/var/lib/php-giggsey-libphonenumber/available/$REPLY_DIR" /var/lib/php-giggsey-libphonenumber/enabled
else
  echo -e "${YELLOW}No change made.${RESET} Run the script again to use another directory."
fi

print_current_metadata
