#!/bin/bash
## We should firstly confirm the repo info is the latest
while true;do
sudo aptss update
if [ "$?" = "0" ];then
break
else
sleep 10
echo "aptss is locked. Will try it later in 10 secs"
fi
done
REALPATH=`realpath $0`
APP_IN_STORE_PATH=`dirname $REALPATH`
pkgname=firefox-spark
archi=amd64
VERSION_FIREFOX_IN_REPO=`aptss show firefox-spark | grep -oP 'Version: \K\S+'`
VERSION_FIREFOX_IN_MOZILLA=`curl -fI 'https://download.mozilla.org/?product=firefox-latest&os=linux64&lang=zh-CN' | grep -o 'firefox-[0-9.]\+[0-9]' | sed 's/.*firefox-//'`
if dpkg --compare-versions "$VERSION_FIREFOX_IN_REPO" "<<" "$VERSION_FIREFOX_IN_MOZILLA";then
## Here we give the value to version
version=$VERSION_FIREFOX_IN_MOZILLA
WORKDIR=`mktemp -d`
cd $WORKDIR
mkdir -p pkgdir/DEBIAN
cd pkgdir
### Write Control
cat << EOFFFFFF > DEBIAN/control
Package: firefox-spark
Version: ${version}
Installed-Size: 214784
Maintainer: shenmo <shenmo@spark-app.store>
Homepage: https://www.mozilla.org/zh-CN/firefox/all/#product-desktop-release
Architecture: $archi
Description: 一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中...
Section: web
Priority: optional
Depends: libdbus-glib-1-2,zenity
Recommends: speech-dispatcher
Provides: x-www-browser,gnome-www-browser,firefox(=${version})
Conflicts: firefox,firefox-spark(<=117.0)
Replaces: firefox,firefox-spark(<=117.0)

EOFFFFFF
chmod 755 DEBIAN/control
### Write postinst

cat << EOFFFFFF > DEBIAN/postinst
#!/bin/bash
function zenity(){


    #Detect the user using such display
    local user=\$(who  | awk '{print \$1}' | head -n 1)

    #Detect the id of the user
    local uid=\$(id -u \$user)


    sudo -u \$user  DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/\$uid/bus zenity "\$@"
}
    if [ ! -e /usr/bin/firefox ];then
    zenity --error --text="You met Error when upgrading Firefox, Please try reinstall\nPlease copy the link below and open it in Spark Store search bar\n您在更新firefox时出现了问题 ，请尝试重新安装\n请复制如下的链接并在星火商店的搜索栏打开\n\n\nspk://store/network/firefox-spark" --width 450
    fi




EOFFFFFF
chmod +x DEBIAN/postinst


mkdir -p opt/apps/firefox-spark/entries/applications
### Write desktop
cat << EOFFFFFF > opt/apps/firefox-spark/entries/applications/firefox-spark.desktop
[Desktop Entry]
Name=Firefox
Name[zh_CN]=Firefox火狐浏览器
Version=1.0
Exec=firefox %U
Comment=Firefox
Icon=/opt/apps/firefox-spark/files/firefox/browser/chrome/icons/default/default128.png
Type=Application
Terminal=false
StartupNotify=true
Encoding=UTF-8
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml_xml;application/x-mimearchive;x-scheme-handler/http;x-scheme-handler/https;


EOFFFFFF


### Write info
cat << EOFFFFFF > opt/apps/firefox-spark/info
{
    "appid": "firefox-spark",
    "name": "firefox-spark",
    "version": "${version}",
    "arch": ["amd64"],
    "permissions": {
        "autostart": false,
        "notification": true,
        "trayicon": false,
        "clipboard": true,
        "account": false,
        "bluetooth": true,
        "camera": true,
        "audio_record": true,
        "installed_apps": false
    }
}
EOFFFFFF

mkdir -p opt/apps/firefox-spark/files

### Download and uncompress firefox latest

Download_Name="fflatest.tar.bz2"
Download_Url="https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=zh-CN"
aria2c -c -x 16 -s 16  -d "opt/apps/firefox-spark/files" "$Download_Url" -o "$Download_Name" --summary-interval=1
tar -xvf "opt/apps/firefox-spark/files"/"$Download_Name" -C "opt/apps/firefox-spark/files"
rm "opt/apps/firefox-spark/files"/"$Download_Name"

mkdir -p opt/apps/firefox-spark/files/firefox/distribution
### Write distribution info and policies
cat << EOFFFFFF > opt/apps/firefox-spark/files/firefox/distribution/distribution.ini
[Global]
id=flamescion
version=1.0
about=Mozilla Firefox for Spark Store

[Preferences]
app.distributor="Project Spark"
app.distributor.channel="Spark Store"
mozilla.partner.id="flamescion"
EOFFFFFF

cat << EOFFFFFF > opt/apps/firefox-spark/files/firefox/distribution/policies.json
{
    "policies":
    {
        "DisableAppUpdate": true
    }
}
EOFFFFFF

### Write usr bin
mkdir -p usr/bin
cat << EOFFFFFF > usr/bin/firefox
#!/bin/bash
/opt/apps/firefox-spark/files/firefox/firefox "\$@"
EOFFFFFF
chmod +x usr/bin/firefox

cd $WORKDIR/pkgdir
fakeroot dpkg-deb -Z xz -b . ../${pkgname}_${version}_${archi}.deb
cd $WORKDIR
mv ${pkgname}_${version}_${archi}.deb $APP_IN_STORE_PATH
cd $APP_IN_STORE_PATH
rm -rf $WORKDIR


SIZE=`du -s ./${pkgname}_${version}_${archi}.deb`
SIZE=`echo ${SIZE%%.*}`


### Write app.json
cat << EOFFFFFF > app.json.update
{
    "Name": "火狐浏览器国际版 Firefox",
    "Version": "${version}",
    "Filename": "${pkgname}_${version}_${archi}.deb",
    "Torrent_address": "${pkgname}_${version}_${archi}.deb.torrent",
    "Pkgname": "firefox-spark",
    "Author": "shenmo <shenmo@spark-app.store>",
    "Contributor": "shenmo <shenmo@spark-app.store>",
    "Website": "https://www.mozilla.org/zh-CN/firefox/all/#product-desktop-release",
    "Update": "$(date +'%Y-%m-%d %H:%M:%S')",
    "Size": "${SIZE}",
    "More": "Firefox是一个自由和开放源代码的网页浏览器\n一款开源自由的浏览器，现在由星火自动维护系统（测试版）维护中...",
    "Tags": "community;ubuntu;deepin;uos",
    "img_urls": "[\"https://examine-spark.oss-cn-shanghai.aliyuncs.com/images/2023/08/30/87ad15a3d25b442aa5e78358ca31a8e5.png\",\"https://examine-spark.oss-cn-shanghai.aliyuncs.com/images/2023/08/30/4c7826cf206d4c8bbab36e8e8fc79568.png\"]",
    "icons": "https://examine-spark.oss-cn-shanghai.aliyuncs.com/icons/2023/08/30/f4ee46124c644408b6e430c1d38aff1f.png"
}
EOFFFFFF

else
echo "No need to upgrade, exit"

fi

