#!/bin/sh

set -e

UPDATENOTIFIERDIR=/var/lib/update-notifier/user.d
MOZ_LIBDIR=/usr/lib/floorp
MOZ_APP_NAME=floorp
MOZ_PKG_NAME=floorp
MOZ_ADDONDIR=/usr/lib/floorp-addons

ESC=$(printf '\033')
echo "----------------------------------------"
echo "${ESC}[0;36mThanks for contributing to Floorp! ${ESC}[0m"
echo ""
echo "Hojo Hinata"
echo "	Twitter:@Hinata_Hojo"
echo ""
echo "Thank you so much!"
echo "----------------------------------------"

# Move a conffile without triggering a dpkg question
finish_mv_conffile() {
    local OLDCONFFILE="$1"
    local NEWCONFFILE="$2"

    rm -f $OLDCONFFILE.dpkg-remove

    [ -e "$OLDCONFFILE" ] || return 0

    echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
    mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
    mv -f "$OLDCONFFILE" "$NEWCONFFILE"
}

finish_rm_conffile() {
    local CONFFILE="$1"

    if [ -e "$CONFFILE.dpkg-backup" ]; then
        mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
    fi
    if [ -e "$CONFFILE.dpkg-remove" ]; then
        echo "Removing obsolete conffile $CONFFILE ..."
        rm -f "$CONFFILE.dpkg-remove"
    fi
}

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ] ; then
    update-alternatives --install /usr/bin/gnome-www-browser \
        gnome-www-browser /usr/bin/$MOZ_APP_NAME 40

    update-alternatives --install /usr/bin/x-www-browser \
        x-www-browser /usr/bin/$MOZ_APP_NAME 40
fi

if [ "$1" = "configure" ] ; then
    #
    # AppArmor
    #
    APP_PROFILE="/etc/apparmor.d/usr.bin.$MOZ_PKG_NAME"
    if [ -f "$APP_PROFILE" ]; then
        if [ -e /etc/lsb-release ] ; then
            ubuntu_version=`grep ^DISTRIB_RELEASE /etc/lsb-release | cut -d= -f2 | cut -d '.' -f-2 | tr -d '.'`
        else
            ubuntu_version=`lsb_release -rs | cut -d '.' -f-2 | tr -d '.'`
        fi
        # Setup the extra include files for Ubuntu 10.10 and higher
        # Add the local/ include
        LOCAL_APP_PROFILE="/etc/apparmor.d/local/usr.bin.$MOZ_PKG_NAME"
        test -e "$LOCAL_APP_PROFILE" || {
            tmp=`mktemp`
            cat <<EOM > "$tmp"
# Site-specific additions and overrides for usr.bin.firefox.
# For more details, please see /etc/apparmor.d/local/README.
EOM
            mkdir `dirname $LOCAL_APP_PROFILE` 2>/dev/null || true
            mv -f "$tmp" "$LOCAL_APP_PROFILE"
            chmod 644 "$LOCAL_APP_PROFILE"
        }

        # Add the addons include
        ADDONS_APP_PROFILE="/etc/apparmor.d/abstractions/ubuntu-browsers.d/$MOZ_PKG_NAME"
        test -e "$ADDONS_APP_PROFILE" || {
            tmp=`mktemp`
            cat <<EOM > "$tmp"
# This file is updated by 'aa-update-browser' and may be overwritten on
# upgrades.
#
# For site-specific adjustments, please see /etc/apparmor.d/local/<binary>

#include <abstractions/ubuntu-browsers.d/plugins-common>
#include <abstractions/ubuntu-browsers.d/mailto>
#include <abstractions/ubuntu-browsers.d/multimedia>
#include <abstractions/ubuntu-browsers.d/productivity>
#include <abstractions/ubuntu-browsers.d/java>
#include <abstractions/ubuntu-browsers.d/kde>
#include <abstractions/ubuntu-browsers.d/text-editors>
#include <abstractions/ubuntu-browsers.d/ubuntu-integration>
#include <abstractions/ubuntu-browsers.d/user-files>
EOM
            mkdir -p `dirname $ADDONS_APP_PROFILE` 2>/dev/null || true
            mv -f "$tmp" "$ADDONS_APP_PROFILE"
            chmod 644 "$ADDONS_APP_PROFILE"
        }

        # Reload AppArmor profile
        DISABLE_APP_PROFILE="/etc/apparmor.d/disable/usr.bin.$MOZ_PKG_NAME"
        if [ ! -f "$DISABLE_APP_PROFILE" ] && aa-status --enabled 2>/dev/null; then
            apparmor_parser -r -T -W "$APP_PROFILE" || true
        fi
    fi
    #
    # End AppArmor
    #

    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/bookmarks.html"
    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/localstore.rdf"
    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/mimeTypes.rdf"
    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/prefs.js"
    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/chrome/userChrome-example.css"
    finish_rm_conffile "/etc/${MOZ_PKG_NAME}/profile/chrome/userContent-example.css"

    finish_mv_conffile "/etc/${MOZ_PKG_NAME}/pref/floorp.js" "/etc/${MOZ_PKG_NAME}/syspref.js"
fi

if [ -d /var/run ] ; then
    touch /var/run/$MOZ_APP_NAME-restart-required
fi
curl -sS --compressed -o /etc/apt/sources.list.d/Floorp.list "https://ppa.ablaze.one/Floorp.list"
ESC=$(printf '\033')
echo "----------------------------------------"
echo "${ESC}[0;32mFloorp was successfully installed! ${ESC}[0m"
echo "----------------------------------------"
echo "Donate"
echo ""
echo "The Floorp project is accepting donations."
echo "If you would like to maintain the website or contribute"
echo "to the developers,please donate via Github Sponsors!"
echo ""
printf 'url:\e]8;;https://github.com/sponsors/Ablaze-MIRAI\e\\https://github.com/sponsors/Ablaze-MIRAI\e]8;;\e\\\n'
echo "----------------------------------------"
echo "${ESC}[1;33mPlease restart all running instances of $MOZ_APP_NAME, or you will experience problems.${ESC}[0m"
