#!/bin/bash
## We should firstly confirm the repo info is the latest

REALPATH=`realpath $0`
APP_IN_STORE_PATH=`dirname $REALPATH`
pkgname=thunderbird.spark
archi=amd64
VERSION_THUNDERBIRD_IN_REPO=`aptss show ${pkgname} | grep -oP 'Version: \K\S+'`
VERSION_THUNDERBIRD_IN_MOZILLA=$(curl -fI 'https://download.mozilla.org/?product=thunderbird-latest-SSL&os=linux64&lang=zh-CN' | grep -o 'thunderbird-[0-9.]\+[0-9]' | sed 's/.*thunderbird-//')
if dpkg --compare-versions "$VERSION_THUNDERBIRD_IN_REPO" "<<" "$VERSION_THUNDERBIRD_IN_MOZILLA";then
## Here we give the value to version
version=$VERSION_THUNDERBIRD_IN_MOZILLA
WORKDIR=`mktemp -d`
cd $WORKDIR
mkdir -p pkgdir/DEBIAN
cd pkgdir
### Write Control
cat << EOFFFFFF > DEBIAN/control
Package: ${pkgname}
Version: ${version}
Installed-Size: 214784
Maintainer: shenmo <shenmo@spark-app.store>
Homepage: https://www.thunderbird.net
Architecture: $archi
Description: Thunderbird是Mozilla开发的一款流行的跨平台邮件客户端
Section: web
Priority: optional
Depends: libdbus-glib-1-2,zenity
Recommends: birdtray
Provides: thunderbird(=${version})
Replaces: thunderbird(=${version})
Conflicts: thunderbird

EOFFFFFF
chmod 755 DEBIAN/control


mkdir -p opt/apps/thunderbird.spark/entries/applications
### Write desktop
cat << EOFFFFFF > opt/apps/thunderbird.spark/entries/applications/thunderbird.spark.desktop
[Desktop Entry]
Name=Thunderbird
Name[zh_CN]=Thunderbird邮件
Version=1.0
Exec="/opt/apps/thunderbird.spark/files/thunderbird/thunderbird" %U
Comment=thunderbird.spark
Icon=/opt/apps/thunderbird.spark/files/thunderbird/chrome/icons/default/default128.png
Type=Application
Terminal=false
StartupNotify=true
Encoding=UTF-8
Categories=Network;WebBrowser;
MimeType=message/rfc822;x-scheme-handler/mailto;text/calendar;text/x-vcard;

EOFFFFFF


### Write info
cat << EOFFFFFF > opt/apps/thunderbird.spark/info
{
    "appid": "${pkgname}",
    "name": "${pkgname}",
    "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/thunderbird.spark/files

### Download and uncompress thunderbird.spark latest

Download_Name="thunderbirdlatest.tar.bz2"
Download_Url="https://download.mozilla.org/?product=thunderbird-latest-SSL&os=linux64&lang=zh-CN"
aria2c  -d "opt/apps/thunderbird.spark/files" "$Download_Url" -o "$Download_Name" --summary-interval=1
tar -xvf "opt/apps/thunderbird.spark/files"/"$Download_Name" -C "opt/apps/thunderbird.spark/files"
rm -vf "opt/apps/thunderbird.spark/files"/"$Download_Name"


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

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": "雷鸟邮件 Thunderbird",
    "Version": "${version}",
    "Filename": "${pkgname}_${version}_${archi}.deb",
    "Torrent_address": "${pkgname}_${version}_${archi}.deb.torrent",
    "Pkgname": "${pkgname}",
    "Author": "shenmo <shenmo@spark-app.store>",
    "Contributor": "shenmo <shenmo@spark-app.store>",
    "Website": "https://www.thunderbird.net",
    "Update": "$(date +'%Y-%m-%d %H:%M:%S')",
    "Size": "${SIZE}",
    "More": "Thunderbird是Mozilla开发的一款流行的跨平台邮件客户端\n现在由星火自动维护系统（测试版）维护中...",
    "Tags": "community;ubuntu;deepin;uos;debian"
}
EOFFFFFF

else
echo "No need to upgrade, exit"

fi

