#!/bin/bash

# Get configuration and addons from firefox-solydxk-adjustments if not already configured or upgraded.

if [ $EUID == 0 ]; then
  echo "You cannot run this script as root."
  exit
fi

if [ "$(pidof firefox)" ] || [ "$(pidof thunderbird)" ]; then
  echo "Firefox and/or Thunderbird are running."
  echo "Please, close these programs before you continue."
  exit
fi

SKIPCNFS='extensions.enabled_addons browser.newtabpage.pinned'

FFUSRS=$(find $HOME/.mozilla/firefox -name "prefs.js" -not -path "*/extensions/*")
FFSKEL=$(find /etc/skel/.mozilla/firefox -name "prefs.js" -not -path "*/extensions/*")
TBUSR=$(find $HOME/.thunderbird -name "prefs.js" -not -path "*/extensions/*")
TBSKEL=$(find /etc/skel/.thunderbird -name "prefs.js" -not -path "*/extensions/*")
FFEXTSKEL=$(find /etc/skel/.mozilla/firefox -name "extensions")
FFEXTUSR=$(find $HOME/.mozilla/firefox -name "extensions")
TBEXTSKEL=$(find /etc/skel/.thunderbird -name "extensions")
TBEXTUSR=$(find $HOME/.thunderbird -name "extensions")

# Firefox
if [ -e "$FFSKEL" ]; then
  # Loop all profiles for this user
  for FFUSR in $FFUSRS; do
    echo
    echo "Synchronize Firefox configuration: $FFUSR"
    # Read prefs.js line by line
    while IFS='' read -r LINE || [[ -n "$LINE" ]]; do
      # Get only lines that begin with user_pref
      UP=$(echo $LINE | egrep -o '^user_pref.*;')
      # Get the configuration variable name
      CNF=$(echo $UP | grep -oP '"[^"]+"' | tr -d '"' | head -n 1)
      if [ "$CNF" != '' ] ; then
        # Check for configuration variable to be skipped
        SKIP=false
        for SC in $SKIPCNFS; do
          if [ "$SC" == "$CNF" ]; then
            SKIP=true
          fi
        done
        # Check if configuration already exists in user prefs.js file
        if ! $SKIP && ! grep -Fq "$CNF" "$FFUSR"; then
          echo "  + $UP"
          # Add user_pref to user prefs.js
          echo "$UP" >> "$FFUSR"
        fi
      fi
    done < "$FFSKEL"
  done
fi

if [ -e "$FFEXTUSR" ] && [ -e "$FFEXTSKEL" ]; then
    echo
    echo "Upgrade Firefox addons"
    # Loop through all available addons
    for ITEM in $(ls "$FFEXTSKEL"); do
      # Check if the user has the addon installed
      if [ -e "$FFEXTUSR/$ITEM" ]; then
        echo "  + $ITEM"
        # Copy the addon to the user extensions directory
        cp -r "$FFEXTSKEL/$ITEM" "$FFEXTUSR/$ITEM"
      fi
    done
fi

# Thunderbird
if [ -e "$TBSKEL" ]; then
  # Loop all profiles for this user
  for TBUSR in $TBUSRS; do
    echo
    echo "Synchronize Thunderbird configuration: $TBUSR"
    # Read prefs.js line by line
    while IFS='' read -r LINE || [[ -n "$LINE" ]]; do
      # Get only lines that begin with user_pref
      UP=$(echo $LINE | egrep -o '^user_pref.*;')
      # Get the configuration variable name
      CNF=$(echo $UP | grep -oP '"[^"]+"' | tr -d '"' | head -n 1)
      if [ "$CNF" != '' ] ; then
        # Check for configuration variable to be skipped
        SKIP=false
        for SC in $SKIPCNFS; do
          if [ "$SC" == "$CNF" ]; then
            SKIP=true
          fi
        done
        # Check if configuration already exists in user prefs.js file
        if ! $SKIP && ! grep -Fq "$CNF" "$TBUSR"; then
          echo "  + $UP"
          # Add user_pref to user prefs.js
          echo "$UP" >> "$TBUSR"
        fi
      fi
    done < "$TBSKEL"
  done
fi

if [ -e "$TBEXTUSR" ] && [ -e "$TBEXTSKEL" ]; then
  echo
  echo "Upgrade Thunderbird addons"
  # Loop through all available addons
  for ITEM in $(ls "$TBEXTSKEL"); do
    # Check if the user has the addon installed
    if [ -e "$TBEXTUSR/$ITEM" ]; then
        echo "  + $ITEM"
        # Copy the addon to the user extensions directory
        cp -r "$TBEXTSKEL/$ITEM" "$TBEXTUSR/$ITEM"
    fi
    done
fi
