#!/bin/bash

# Chromium Installer - A script to install the latest Chromium build for Linux
# AUTHOR: Alessandro Rinaldi <alerinaldi@linuxzogno.org>

# A script to download the last build of Chromium for Linux and install it automatically.

# This script hasn't got any type of license, I wrote it just for me and I publish it because I think it could be useful to someone.
# So, feel free to do ALL you want with it! ;)

echo "Chromium Installer - A script to install the latest Chromium build for Linux
AUTHOR: Alessandro Rinaldi <alerinaldi@linuxzogno.org>";
(
zenity;
if [ "$?" != "255" ]; then echo "ERROR: This script requires zenity! Please install it and try again"; exit 1; fi
if [ "`whoami`" != "root" ]; then gksu "$0 $@ `whoami`"; if [ "$?" = "127" ]; then zenity --error --text "ERROR: gksu not found, so the script must be manually executed by root!"; exit 1; else exit $?; fi; fi
cd ~;
rm -r ~/.chromium-temp;
mkdir ~/.chromium-temp;
cd ~/.chromium-temp;

# Download the latests build in the Linux trunk
readv="`cat /usr/lib/chromium-browser/VERSION`";
if [ "$readv" = "" ]; then
if [ -d "/usr/lib/chromium-browser" ]; then
instversion="Unknown";
else
instversion="None";
fi
else
instversion="$readv";
fi
arch="`uname -i`";
if [ "$arch" = "x86_64" ]; then
version=`wget -O- http://build.chromium.org/f/chromium/snapshots/Linux_x64/LATEST `;
else
version=`wget -O- http://build.chromium.org/f/chromium/snapshots/Linux/LATEST `;
fi
if [ "$version" = "" ]; then zenity --error --title "Chromium Installer" --text "ERROR: Couldn't connect to the Chromium server!"; exit 1; fi
if [ "$instversion" = "Unknown" ]; then
zenity --info --title "Chromium Installer" --text "WARNING: Chromium is already installed on your system, but it hasn't been installed by this script.
This script will automatically remove the previuos installation, but if you have installed it from a package manager, it may be automatically reinstalled at the next upgrade.
So, if you haven't installed Chromium manually, please remove it from your package manager before you continue!"; fi

zenity --question --title "Chromium Installer" --text "ARCHITECTURE: $arch
INSTALLED VERSION: $instversion
LATEST VERSION: $version

Press OK to install, or Cancel to abort";

answer=$?;
if [ $answer -eq 1 ]; then exit 1; fi
( echo "# Downloading build $version...";
if [ "$arch" = "x86_64" ]; then
wget http://build.chromium.org/f/chromium/snapshots/Linux_x64/$version/chrome-linux.zip -O chrome-linux.zip 2> fwget & echo "$!" > wgetpid;
else
wget http://build.chromium.org/f/chromium/snapshots/Linux/$version/chrome-linux.zip -O chrome-linux.zip 2> fwget & echo "$!" > wgetpid;
fi
while [ 1 ]; do
oldperc=$perc;
lul="`cat fwget | grep "%" | tail -n 1`";
if [ "$lul" = "" ]; then continue; fi
perc="`echo $lul | cut -d" " -f7`";
if [ "$perc" != "`echo -n $perc | grep %`" ]; then perc="100%"; fi
if [ "$perc" = "" ]; then perc="100%"; fi
perc="`echo $perc | cut -d"%" -f1`";
if [ "$perc" = "100" ]; then break; fi
if [ "$oldperc" != "$perc" ]; then
echo "# Downloading build $version... ($perc%)";
echo $perc;
if [ $? -gt 0 ]; then kill `cat wgetpid`; exit 1; fi
fi
sleep 0.3;
done
wget http://www.linuxzogno.org/chromium/chromium-browser.desktop -O chromium-browser.desktop;
wget http://www.linuxzogno.org/chromium/chromium-browser.png -O chromium-browser.png;

# Unpack it
echo "# Unpacking... ";
unzip chrome-linux.zip;

# Install it
echo "# Installing... ";
mv chrome-linux chromium-browser;
rm -r /usr/lib/chromium-browser;
mv chromium-browser /usr/lib;
mv chromium-browser.png /usr/share/pixmaps;
chmod 755 /usr/share/pixmaps/chromium-browser.png;
chmod 755 -R /usr/lib/chromium-browser;
echo "#!/bin/bash

/usr/lib/chromium-browser/chrome \"\$@\";
exit \$?;
" > /usr/bin/chromium-browser;
chmod 755 /usr/bin/chromium-browser;
echo "$version" > /usr/lib/chromium-browser/VERSION;
mv chromium-browser.desktop /usr/share/applications;
chmod 755 /usr/share/applications/chromium-browser.desktop;

echo "100"; ) | zenity --progress --auto-kill --auto-close --title "Chromium Installer";

# Remove temporary files
cd ~;
rm -r ~/.chromium-temp;

# Restart Chromium
zenity --question --title "Chromium Installer" --text "Setup completed!

If you want to start/restart Chromium now, click on OK, else click on Cancel";
if [ $? -eq 0 ]; then kill `pidof chrome`;
if [ $1 = "" ]; then exec chromium-browser; else exec su -c chromium-browser $@; fi;
fi;
) 2> /dev/null > /dev/null;
exit 0;


