#!/usr/bin/bash
#  get-widevine.sh
#  
#  Copyright 2026 José Alberto Valle Cid <j.alberto.vc@gmail.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  Based in chromiun-widevine of Arch community
#  https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=chromium-widevine
#last-update 14/May/2026

pkgver=4.10.3050.0

if [ $UID != 0 ] ; then
	pkexec $0
	exit $?
fi

export TEXTDOMAIN="Tuningdrake"
applang="$(locale|grep LANG|cut -d '=' -f 2|cut -d '.' -f 1|cut -d '_' -f 1)"
case "$applang" in
	es|pt|fr);;
	*)export export LANGUAGE=en;;
esac

machine="$(uname -m)"

case "$machine" in
x86_64)
	chromiumdir="/usr/lib64/chromium-browser"
	platform="x64";;
*) echo "$(gettext "Sistema no soportado")"
	exit 1;;
esac
if [ -d /tmp/widevine ]; then
	rm -rf /tmp/widevine
fi

mkdir -p /tmp/widevine
pushd /tmp/widevine
#if ! wget -q http://ftp.blogdrake.org/widevine.txt ; then
	#echo "$(gettext "No se pudo obtener informacion de version de widevine")"
	#popd
	#rm -rf /tmp/widevine
	#exit 1
#fi

#pkgver="$(grep ${platform} widevine.txt|cut -d ':' -f2)"

zipfile="oimompecagnajdejgnnjijobebaeigek_${pkgver}_linux_aduyhcxcs7a2znuo6ss5tffn5jpa.crx3"
url="https://www.google.com/dl/release2/chrome_component/bywu6vkyg6gmcthxdaetzfkptm_${pkgver}/$zipfile"

if [ -f "$chromiumdir/WidevineCdm/manifest.json" ] ; then
	currver=$(grep \"version $chromiumdir/WidevineCdm/manifest.json|cut -d ':' -f2|sed -e 's|\"||g;s|\,||')
else
    currver=0
fi

case "$(vercmp "$pkgver" "$currver")" in
	0) echo "widevine $pkgver $(gettext "ya esta instalado")";;
	1)  
		if ! wget -q $url ; then
			echo "$(gettext "No se pudo obtener widevine")"
			popd
			rm -rf /tmp/widevine
			exit 1
		fi
		
		unzip $zipfile
		rm -rf $zipfile _metadata
		if [ ! -d "${chromiumdir}/WidevineCdm" ]; then
			mkdir -p "${chromiumdir}/WidevineCdm"
		else
			rm -rf "${chromiumdir}/WidevineCdm/*"
		fi
		cp -afv * ${chromiumdir}/WidevineCdm/
		# backward compatibility
		ln -sf ${chromiumdir}/WidevineCdm/_platform_specific/linux_x64/libwidevinecdm.so ${chromiumdir}/libwidevinecdm.so;;
esac
popd
rm -rf /tmp/widevine
