#!/bin/sh

# AM INSTALL SCRIPT VERSION 3. 
set -u
APP=floorp
SITE="Floorp-Projects/Floorp"

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/tmp" "/opt/$APP/icons" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > ../remove
printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> ../remove
chmod a+x ../remove || exit 1

# DOWNLOAD AND PREPARE THE APP, $version is also used for updates
version=$(curl -Ls https://api.github.com/repos/Floorp-Projects/Floorp/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*" | grep -vi "i386\|i686\|aarch64\|arm64\|armv7l" | grep -i "x86_64.tar.bz2" | head -1)
wget "$version" || exit 1
[ -e ./*7z ] && 7z x ./*7z && rm -f ./*7z
[ -e ./*tar.* ] && tar fx ./*tar.* && rm -f ./*tar.*
[ -e ./*zip ] && unzip -qq ./*zip 1>/dev/null && rm -f ./*zip
cd ..
if [ -d ./tmp/* 2>/dev/null ]; then mv ./tmp/*/* ./; else mv ./tmp/* ./"$APP" 2>/dev/null || mv ./tmp/* ./; fi
rm -R -f ./tmp || exit 1
echo "$version" > ./version
chmod a+x ./$APP || exit 1

# LINK TO PATH
ln -s "/opt/$APP/$APP" "/usr/local/bin/$APP"

# SCRIPT TO UPDATE THE PROGRAM
cat >> ./AM-updater << 'EOF'
#!/bin/sh
set -u
APP=floorp
SITE="Floorp-Projects/Floorp"
version0=$(cat "/opt/$APP/version")
version=$(curl -Ls https://api.github.com/repos/Floorp-Projects/Floorp/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*" | grep -vi "i386\|i686\|aarch64\|arm64\|armv7l" | grep -i "x86_64.tar.bz2" | head -1)
[ -n "$version" ] || { echo "Error getting link"; exit 1; }
if [ "$version" != "$version0" ]; then
	mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	notify-send "A new version of $APP is available, please wait"
	wget "$version" || exit 1
	[ -e ./*7z ] && 7z x ./*7z && rm -f ./*7z
	[ -e ./*tar.* ] && tar fx ./*tar.* && rm -f ./*tar.*
	[ -e ./*zip ] && unzip -qq ./*zip 1>/dev/null && rm -f ./*zip
	cd ..
	if [ -d ./tmp/* 2>/dev/null ]; then mv --backup=t ./tmp/*/* ./; else mv --backup=t ./tmp/* ./"$APP" 2>/dev/null || mv --backup=t ./tmp/* ./; fi
	chmod a+x ./"$APP" || exit 1
	echo "$version" > ./version
	rm -R -f ./tmp ./*~
	notify-send "$APP is updated!"
else
	echo "Update not needed!"
fi
EOF
chmod a+x ./AM-updater || exit 1

# ICON
mkdir -p icons
cp ./browser/chrome/icons/default/*128* ./icons/"$APP" 2> /dev/null

# LAUNCHER
echo "[Desktop Entry]
Version=1.0
Name=Floorp Web Browser
Name[fr]=Navigateur Web Floorp
Name[it]=Floorp Browser Web
Name[ja]=Floorp ウェブ・ブラウザ
Name[ko]=Floorp 웹 브라우저
Name[pt]=Floorp Navegador Web
Name[pt_BR]=Navegador Web Floorp
Name[ru]=Веб-браузер Floorp
Name[zh_CN]=Floorp 网络浏览器
Name[zh_TW]=Floorp 網路瀏覽器
Comment=Browse the World Wide Web
Comment[de]=Im Internet surfen
Comment[fr]=Naviguer sur le Web
Comment[it]=Esplora il web
Comment[ja]=ウェブを閲覧します
Comment[ko]=웹을 돌아 다닙니다
Comment[pt]=Navegue na Internet
Comment[pt_BR]=Navegue na Internet
Comment[ru]=Доступ в Интернет
Comment[zh_CN]=浏览互联网
Comment[zh_TW]=瀏覽網際網路
GenericName=Web Browser
GenericName[ar]=متصفح ويب
GenericName[ast]=Restolador Web
GenericName[bn]=ওয়েব ব্রাউজার
GenericName[ca]=Navegador web
GenericName[cs]=Webový prohlížeč
GenericName[da]=Webbrowser
GenericName[el]=Περιηγητής διαδικτύου
GenericName[es]=Navegador web
GenericName[et]=Veebibrauser
GenericName[fa]=مرورگر اینترنتی
GenericName[fi]=WWW-selain
GenericName[fr]=Navigateur Web
GenericName[gl]=Navegador Web
GenericName[he]=דפדפן אינטרנט
GenericName[hr]=Web preglednik
GenericName[hu]=Webböngésző
GenericName[it]=Browser web
GenericName[ja]=ウェブ・ブラウザ
GenericName[ko]=웹 브라우저
GenericName[ku]=Geroka torê
GenericName[lt]=Interneto naršyklė
GenericName[nb]=Nettleser
GenericName[nl]=Webbrowser
GenericName[nn]=Nettlesar
GenericName[no]=Nettleser
GenericName[pl]=Przeglądarka WWW
GenericName[pt]=Navegador Web
GenericName[pt_BR]=Navegador Web
GenericName[ro]=Navigator Internet
GenericName[ru]=Веб-браузер
GenericName[sk]=Internetový prehliadač
GenericName[sl]=Spletni brskalnik
GenericName[sv]=Webbläsare
GenericName[tr]=Web Tarayıcı
GenericName[ug]=توركۆرگۈ
GenericName[uk]=Веб-браузер
GenericName[vi]=Trình duyệt Web
GenericName[zh_CN]=网络浏览器
GenericName[zh_TW]=網路瀏覽器
Keywords=Internet;WWW;Browser;Web;Explorer;Floorp
Keywords[ar]=انترنت;إنترنت;متصفح;ويب;وب;Floorp
Keywords[ast]=Internet;WWW;Restolador;Web;Esplorador;Floorp
Keywords[ca]=Internet;WWW;Navegador;Web;Explorador;Explorer;Floorp
Keywords[cs]=Internet;WWW;Prohlížeč;Web;Explorer;Floorp
Keywords[da]=Internet;Internettet;WWW;Browser;Browse;Web;Surf;Nettet;Floorp
Keywords[de]=Internet;WWW;Browser;Web;Explorer;Webseite;Site;surfen;online;browsen;Floorp
Keywords[el]=Internet;WWW;Browser;Web;Explorer;Διαδίκτυο;Περιηγητής;Firefox;Φιρεφοχ;Ιντερνετ;Floorp
Keywords[es]=Explorador;Internet;WWW;Floorp
Keywords[fi]=Internet;WWW;Browser;Web;Explorer;selain;Internet-selain;internetselain;verkkoselain;netti;surffaa;Floorp
Keywords[fr]=Internet;WWW;Browser;Web;Explorer;Fureteur;Surfer;Navigateur;Floorp
Keywords[he]=דפדפן;אינטרנט;רשת;אתרים;אתר;פיירפוקס;מוזילה;Floorp
Keywords[hr]=Internet;WWW;preglednik;Web;Floorp
Keywords[hu]=Internet;WWW;Böngésző;Web;Háló;Net;Explorer;Floorp
Keywords[it]=Internet;WWW;Browser;Web;Navigatore;Floorp
Keywords[is]=Internet;WWW;Vafri;Vefur;Netvafri;Flakk;Floorp
Keywords[ja]=Internet;WWW;Web;インターネット;ブラウザ;ウェブ;エクスプローラ;Floorp
Keywords[nb]=Internett;WWW;Nettleser;Explorer;Web;Browser;Nettside;Floorp
Keywords[nl]=Internet;WWW;Browser;Web;Explorer;Verkenner;Website;Surfen;Online;Floorp 
Keywords[pt]=Internet;WWW;Browser;Web;Explorador;Navegador;Floorp
Keywords[pt_BR]=Internet;WWW;Browser;Web;Explorador;Navegador;Floorp
Keywords[ru]=Internet;WWW;Browser;Web;Explorer;интернет;браузер;веб;файрфокс;огнелис;Floorp
Keywords[sk]=Internet;WWW;Prehliadač;Web;Explorer;Floorp
Keywords[sl]=Internet;WWW;Browser;Web;Explorer;Brskalnik;Splet;Floorp
Keywords[tr]=İnternet;WWW;Tarayıcı;Web;Gezgin;Web sitesi;Site;sörf;çevrimiçi;tara;Floorp
Keywords[uk]=Internet;WWW;Browser;Web;Explorer;Інтернет;мережа;переглядач;оглядач;браузер;веб;файрфокс;вогнелис;перегляд;Floorp
Keywords[vi]=Internet;WWW;Browser;Web;Explorer;Trình duyệt;Trang web;Floorp
Keywords[zh_CN]=Internet;WWW;Browser;Web;Explorer;网页;浏览;上网;火狐;Firefox;ff;互联网;网站;Floorp
Keywords[zh_TW]=Internet;WWW;Browser;Web;Explorer;網際網路;網路;瀏覽器;上網;網頁;火狐;Floorp
Exec=$APP %u
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=/opt/$APP/icons/$APP
Categories=GNOME;GTK;Network;WebBrowser;
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
Actions=new-window;new-private-window;

[Desktop Action new-window]
Name=Open a New Window
Name[ar]=افتح نافذة جديدة
Name[ast]=Abrir una ventana nueva
Name[bn]=Abrir una ventana nueva
Name[ca]=Obre una finestra nova
Name[cs]=Otevřít nové okno
Name[da]=Åbn et nyt vindue
Name[de]=Ein neues Fenster öffnen
Name[el]=Νέο παράθυρο
Name[es]=Abrir una ventana nueva
Name[fi]=Avaa uusi ikkuna
Name[fr]=Ouvrir une nouvelle fenêtre
Name[gl]=Abrir unha nova xanela
Name[he]=פתיחת חלון חדש
Name[hr]=Otvori novi prozor
Name[hu]=Új ablak nyitása
Name[it]=Apri una nuova finestra
Name[ja]=新しいウィンドウを開く
Name[ko]=새 창 열기
Name[ku]=Paceyeke nû veke
Name[lt]=Atverti naują langą
Name[nb]=Åpne et nytt vindu
Name[nl]=Nieuw venster openen
Name[pt]=Abrir nova janela
Name[pt_BR]=Abrir nova janela
Name[ro]=Deschide o fereastră nouă
Name[ru]=Новое окно
Name[sk]=Otvoriť nové okno
Name[sl]=Odpri novo okno
Name[sv]=Öppna ett nytt fönster
Name[tr]=Yeni pencere aç 
Name[ug]=يېڭى كۆزنەك ئېچىش
Name[uk]=Відкрити нове вікно
Name[vi]=Mở cửa sổ mới
Name[zh_CN]=新建窗口
Name[zh_TW]=開啟新視窗
Exec=$APP -new-window

[Desktop Action new-private-window]
Name=Open a New Private Window
Name[ar]=افتح نافذة جديدة للتصفح الخاص
Name[ca]=Obre una finestra nova en mode d'incògnit
Name[cs]=Otevřít nové anonymní okno
Name[de]=Ein neues privates Fenster öffnen
Name[el]=Νέο ιδιωτικό παράθυρο
Name[es]=Abrir una ventana privada nueva
Name[fi]=Avaa uusi yksityinen ikkuna
Name[fr]=Ouvrir une nouvelle fenêtre de navigation privée
Name[he]=פתיחת חלון גלישה פרטית חדש
Name[hu]=Új privát ablak nyitása
Name[it]=Apri una nuova finestra anonima
Name[ja]=新しいプライベートウィンドウを開く
Name[nb]=Åpne et nytt privat vindu
Name[ru]=Новое приватное окно
Name[sl]=Odpri novo okno zasebnega brskanja
Name[sv]=Öppna ett nytt privat fönster
Name[tr]=Yeni gizli pencere aç
Name[uk]=Відкрити нове вікно у потайливому режимі
Name[zh_TW]=開啟新隱私瀏覽視窗
Exec=$APP -private-window" > /usr/local/share/applications/"$APP"-AM.desktop
