#!/bin/sh

if [ "$(id -u)" != 0 ]; then
  echo "Must be root to set up Pale Moon"
  exit 1;
fi

if [ "$(cat /etc/debian_version | cut -d. -f1)" -ge 9 ]; then
  echo "Only Debian Wheezy/Jessie are supported at the moment"
  exit 1;
fi

case "$(uname -m)" in
#  armv6*) src="palemoon-armv6.tar.bz2";;
  armv7*) src="palemoon-armv7.tar.bz2";;
  *) echo "Architecture $(uname -m) is not supported"; exit 1;;
esac


if [ "$1" = remove ]; then
  echo "Removing Pale Moon..."
  rm /home/*/.local/share/applications/userapp-Pale\ Moon-*.desktop /home/*/.local/share/applications/mimeinfo.cache
  update-alternatives --remove gnome-www-browser /usr/bin/palemoon
  update-alternatives --remove x-www-browser /usr/bin/palemoon
  rm -rf /usr/bin/palemoon /opt/palemoon /usr/share/applications/palemoon.desktop /usr/share/icons/hicolor/*/apps/palemoon.png
  echo "Done."
  exit 0
fi

echo "Downloading Pale Moon..."
rm /tmp/palemoon-*.tar.bz2
if wget -P/tmp https://raspi.palemoon.org/latest/$src; then
  echo "Done."
else
  echo "Failed."
  exit 1
fi

rm -rf /opt/palemoon
tar -xvf /tmp/palemoon-*.tar.bz2 -C /opt
ln -s /opt/palemoon/palemoon /usr/bin/palemoon
ln -s /opt/palemoon/browser/chrome/icons/default/default16.png /usr/share/icons/hicolor/16x16/apps/palemoon.png
ln -s /opt/palemoon/browser/chrome/icons/default/default32.png /usr/share/icons/hicolor/32x32/apps/palemoon.png
ln -s /opt/palemoon/browser/chrome/icons/default/default48.png /usr/share/icons/hicolor/48x48/apps/palemoon.png
ln -s /opt/palemoon/browser/icons/mozicon128.png /usr/share/icons/hicolor/128x128/apps/palemoon.png
update-alternatives --install /usr/bin/gnome-www-browser gnome-www-browser /usr/bin/palemoon 100
update-alternatives --install /usr/bin/x-www-browser x-www-browser /usr/bin/palemoon 100

if [ -d /usr/share/hunspell ]; then
  echo "Pale Moon will use hunspell dictionaries"
  rm -r /opt/palemoon/dictionaries
  ln -s /usr/share/hunspell /opt/palemoon/dictionaries
fi

if [ -e /etc/alternatives/flash-mozilla.so ]; then
  echo "Your system has a configured flash player, Pale Moon will be using that"
  mkdir /opt/palemoon/plugins/
  ln -s /etc/alternatives/flash-mozilla.so /opt/palemoon/plugins/libflashplayer.so
elif [ -e /usr/lib/gnash/libgnashplugin.so ]; then
  echo "Your system has gnash installed, it will be used to render flash in Pale Moon"
  mkdir /opt/palemoon/plugins/
  ln -s /usr/lib/gnash/libgnashplugin.so /opt/palemoon/plugins/libflashplayer.so
else
  echo "You don't have flash installed. If you install it later, re-run this installer"
fi

cat >/usr/share/applications/palemoon.desktop <<EOF
[Desktop Entry]
Version=1.0
Name=Pale Moon Web Browser
Comment=Browse the World Wide Web
Keywords=Internet;WWW;Browser;Web;Explorer
Exec=palemoon %u
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=palemoon
Categories=Network;WebBrowser;Internet
MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;x-scheme-handler/chrome;video/webm;application/x-xpinstall;
StartupNotify=true
EOF
