#!/bin/sh

set -e

echo
echo '---------------- Extra Codecs Installer 1.0.0 ----------------'
echo

echo -n ' * Looking for supported web browsers ... '
opera_lib_path=`ls /usr/lib/*/opera/libEGL.so 2>/dev/null || ls /usr/*/opera/libEGL.so 2>/dev/null || echo ''`
echo OK
echo

echo -n ' * Checking operating system architecture ... '
if [ `uname -a | grep -ic ubuntu` -eq 1 ]; then
	echo OK
	echo
	echo ' * Installing ...'
	echo
	echo I would like to run the following commands:
	echo 'sudo apt-get update && sudo apt-get install chromium-codecs-ffmpeg-extra'
	echo
	sudo apt-get update && sudo apt-get install chromium-codecs-ffmpeg-extra 
	if [ $? -ne 0 ]; then
		echo
		echo Error!
		echo If you think it is our bug, please drop us a line at https://speakapp.link/contact
		echo
		exit 2
	fi
else
	cd /tmp
	if [ `uname -m | grep -c 64` -eq 1 ]; then
		arch='amd64'
		sum='6f7d11554dc7c1b73e1daa6a0b0224b91aa54bd6'
	else 
		arch='i386'
		sum='f51e2dc44433f1ce18247557768badb33bc16cd9'
	fi
	echo OK
	echo
	echo ' * Fetching the codecs from the web ...'
	echo
	wget -N "http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-codecs-ffmpeg-extra_73.0.3683.75-0ubuntu0.16.04.1_$arch.deb"
	echo
	echo -n ' * Unpacking ... '
	ar p chromium-codecs-ffmpeg-extra_*.deb data.tar.xz | tar xJf - ./usr/lib/chromium-browser/libffmpeg.so --strip 4
	echo OK
	echo
	echo -n ' * Verifying ... '
	echo "$sum libffmpeg.so" | sha1sum -c -
	echo
	echo ' * Installing ... '
	echo
	sudo mkdir -p /usr/lib/chromium-browser/ && sudo install libffmpeg.so /usr/lib/chromium-browser/libffmpeg.so
	if [ $? -ne 0 ]; then
		echo
		echo Error!
		echo If you think it is our bug, please drop us a line at https://speakapp.link/contact
		echo
		exit 2
	fi
fi

if [ -n "$opera_lib_path" ]; then
	opera_dir=${opera_lib_path%/libEGL.so}
	lib_extra_dir=$opera_dir/lib_extra
	sudo mkdir -p $lib_extra_dir
	cd $opera_dir
	sudo mv libffmpeg.so libffmpeg.so.BAK
	sudo ln -s /usr/lib/chromium-browser/libffmpeg.so . 
	cd $lib_extra_dir
	sudo ln -s /usr/lib/chromium-browser/libffmpeg.so . || echo ' ^- seems already there, OK!'
fi

if [ -d /opt/vivaldi ]; then
	cd /opt/vivaldi
	sudo ln -s /usr/lib/chromium-browser/libffmpeg.so . || echo ' ^- seems already there, OK!'
fi;

echo 
echo Done! Please restart your web browser and enjoy!
echo 
echo If you still cannot see any video in SpeakApp, drop us a line at https://speakapp.link/contact
echo 'We are happy to help :-)'
echo

