#!/bin/sh
# Didier Spaier didieratslintdotfr 2021
# I wrote this script from scratch and put it in the public domain.
if [ ! $(id -u) -eq 0 ]; then
	echo "Please run this script as root."
	exit
fi
to_lower() {
echo "$1"|tr '[:upper:]' '[:lower:]'
}
chrome="  chrome   (Google Chrome web browser)\n"
firefox="  firefox  (Mozilla Firefox web browser)\n"
vivaldi="  vivaldi  (Vivaldi web browser)\n"
thunderbird="  thunderbird (Mozilla Thunderbird mail application)\n"

b1="chrome"
b2="firefox"
b3="vivaldi"
b4="thunderbird"
pkgname1="google-chrome"
pkgname2="mozilla-firefox"
pkgname3="vivaldi"
pkgname4="thunderbird"

browsers="$chrome$firefox$vivaldi$thunderbird"
usage() {
  echo "Usage: $0 <application>"
  echo "Install or uprade a web browser or mail application among:"
  printf %b "$browsers"
  echo
  echo "To know more, read /usr/doc/latest-browsers/README"
  echo
  exit
}
ARGUMENT=$(to_lower $1)
[ $# -ne 1 ] && usage
if \
[ ! "$ARGUMENT" = "$b1" ] && \
[ ! "$ARGUMENT" = "$b2" ] && \
[ ! "$ARGUMENT" = "$b3" ] && \
[ ! "$ARGUMENT" = "$b4" ]; then
	echo
	echo "$1 is not a supported web browser or mail application."
	usage
	exit
fi
case $ARGUMENT in
	chrome) pkgname="google-chrome";;
	firefox) pkgname="mozilla-firefox";config='~/.mozilla/firefox';;
	vivaldi) pkgname="vivaldi";;
	thunderbird) pkgname="mozilla-thunderbird";config='~/.thunderbird'
esac
if echo $pkgname|grep -q mozilla; then
	RUNNING=$(ps -eo cmd|grep "/${ARGUMENT}"|grep -v grep)
	if [ ! "$RUNNING" = "" ]; then
		printf %b "Please close $pkgname first\n"
		exit
	fi
	MAJVER=$(ls /var/log/packages/|grep ${pkgname}|sed "s/-[^-]*-[^-]*$//;s/.*-//;s/\..*//")
	if [ $MAJVER -lt 90 ]; then
		unset answered
		while [ ! "$answered" ]; do
			printf "Did you make a backup of $config? Please answer yes or no: "
			read backup
			if [ "$(to_lower $backup)" = "no" ]; then
				echo "Please make a backup then start this script again."
				exit
			fi
			if [ "$(to_lower $backup)" = "yes" ]; then
				answered="yes"
			fi
		done
	fi
fi
sh /usr/libexec/latest-browsers/latest-${ARGUMENT}.sh -i
pkg="$(find /tmp -name ${pkgname}*t?z -mmin -3)"
if [ ! "$pkg" = "" ]; then
	printf "Keep in /tmp the package that has been installed? [y/N]? "
	read KEEP
	[ ! "$(to_lower $KEEP)" = "y" ] && rm $pkg
else
	exit
fi
rm -rf /tmp/repackage-$pkgname
if echo $pkgname|grep -q mozilla && \
! grep EXCLUDE /etc/slapt-get/slapt-getrc|grep -q "$pkgname" ; then
	printf "Blacklist in /etc/slapt-get/slapt-getrc the package $pkgname? [Y/n]? "
	read BLACKLIST
	if [ ! "$(to_lower $BLACKLIST)" = "n" ]; then
		if grep EXCLUDE /etc/slapt-get/slapt-getrc|grep -q ,$ ; then
			sed "/EXCLUDE/\@\$@^${pkgname}@" /etc/slapt-get/slapt-getrc > bof
		else
			sed "/EXCLUDE/s@\$@,^${pkgname}@" /etc/slapt-get/slapt-getrc > bof
		fi
		mv bof /etc/slapt-get/slapt-getrc
	fi
fi
printf %b "All done.\n"
if echo $pkgname|grep -q mozilla; then
printf %b "
To avoid that $pkgname starts with an empty profile,
start it next time like this:
$ARGUMENT -P
then select the profile you used before upgrading and press Enter.\n"
fi




