#!/bin/bash
# Special thank to Shiroko, he give me the insight of coding in proper way

# Usage: $0 [arch]


################################# Here we set the configs
REALPATH=`realpath $0`
HERE_PATH=`dirname $REALPATH`
STORE_APP_PATH="$HERE_PATH"
WORKDIR=`mktemp -d`
ARCH="" # Define it later
ARCH_ANOTHERWAY="" # Define it later

PKG_NAME="firefox-spark"
APP_NAME="Firefox火狐浏览器"
#GITHUB_REPO="" #格式像这样。只在从github获取信息才使用，如果不是，则注释掉。后续下载也整合进入helper
VERSION="" # 定义版本号。或者，在后续prepare或build中定义。可使用其他两个bashimport文件中的功能
TAGS="ubuntu;deepin;debian;community" # 星火的tag信息
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Firefox最新版。适用于Ubuntu 2204/Debian 12/deepin23. 其他用户请使用firefox-spark-ace." # This should be written as json format
HOMEPAGE="https://gitee.com/spark-building-service/firefox-spark-arm"
MAINTAINER="shenmo<shenmo@spark-app.store>"



################################# Here we import our tools
if [ ! -e "/opt/bashimport/sbs-utils.bashimport" ];then
echo "Need to install spark-building-service deb first! exit"
exit 1
fi
source /opt/bashimport/sbs-utils.bashimport
source /opt/bashimport/sbs-build-helper.bashimport

##这两个里面有详尽的解释
if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="-amd64"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="-aarch64"
else
log.error "Err:Invalid Architecture"
exit 1
fi

# ===== Log 用法 =====
# log.info xxx
# log.warn xxx
# log.info xxx
# log.debug xxx
# 带颜色的echo

function prepare(){

# Below is the usable funcions

# aptss_get_update()：更新apt源并检查是否成功，如果成功则记录日志为“aptss update ok”，否则记录日志为“aptss is locked.Give up”并退出。

# get_version_from_aptss()：从aptss(全部)源中获取指定软件包的版本号。实战中建议使用下方的函数替代以避免无法预知的情况

# get_version_from_source_list_file ${PKG_NAME} ${PATH_TO_SOURCE_LIST}

# compare_version ${VER_Apt} ${VER_Upstream} (Will give the upstream version as variable $VERSION if it is higher or exit if they are the same or Ver_Apt is higher)

# get_version_from_github()：从github上获取指定仓库的最新发布版本号。需要定义GITHUB_REPO变量。会给返回值

# get_size()：获取软件包目录的大小并返回值
aptss_get_update
#VER_APTSS=1
VER_APTSS=$(get_version_from_source_list_file "${PKG_NAME}:${ARCH}" /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list)
VER_UPSTREAM=$(get_version_from_source_list_file "firefox:${ARCH}" /etc/apt/sources.list.d/firefox.list)
#VERSION=$VER_UPSTREAM
#VER_APTSS=1.0
compare_version ${VER_APTSS} ${VER_UPSTREAM}
################################# Fill the variables below only if you use write_control function
SECTION="utils"
PRIORITY="optional"
DEPENDS="libasound2 (>= 1.0.16), libatk1.0-0 (>= 1.12.4), libc6 (>= 2.28), libcairo-gobject2 (>= 1.10.0), libcairo2 (>= 1.10.0), libdbus-1-3 (>= 1.9.14), libfontconfig1 (>= 2.12.6), libfreetype6 (>= 2.3.5), libgcc1 (>= 1:4.5), libgdk-pixbuf2.0-0 (>= 2.22.0) | libgdk-pixbuf-2.0-0 (>= 2.22.0), libglib2.0-0 (>= 2.37.3), libgtk-3-0 (>= 3.13.7), libpango-1.0-0 (>= 1.14.0), libpangocairo-1.0-0 (>= 1.14.0), libstdc++6 (>= 5), libx11-6, libx11-xcb1, libxcb-shm0, libxcb1, libxcomposite1 (>= 1:0.3-1), libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1), libxext6, libxfixes3, libxi6, libxrandr2 (>= 2:1.4.0), libxrender1,ffmpeg"
DESCRIPTION_SHORT="一款开源自由的浏览器"
DESCRIPTION_LONG="长介绍"
PROVIDES="x-www-browser,gnome-www-browser,firefox(=${VERSION})"
REPLACES="firefox"
RECOMMENDS="speech-dispatcher"
CONFLICTS="firefox,firefox-spark(<=117.0)"
}




function build(){

pushd $WORKDIR

# init_package_directory()：初始化软件包目录，创建DEBIAN、opt/apps/<PKG_NAME>/entries/applications、opt/apps/<PKG_NAME>/entries/icons和opt/apps/<PKG_NAME>/files等目录，并将模板应用到软件包目录中。

# write_control()：在软件包目录中生成control文件，并设置软件包的基本信息，如名称、版本、维护者、主页、架构、依赖关系、描述等。

#    write_info()：在软件包目录/opt/apps/<PKG_NAME>/下生成info文件，记录软件包的基本信息，如appid、名称、版本、架构、权限等。

#    write_json()：在软件包目录中生成app.json.update文件，记录软件包的详细信息，如名称、版本、文件名、种子地址、作者、贡献者、主页、更新时间、大小、更多信息、标签、截图地址和图标地址等。应当在商店目录使用而不是workdir
init_package_directory
write_control
write_info
apt update
apt download firefox:${ARCH}=${VERSION}
apt download firefox-l10n-*
mkdir temp-package
dpkg -X ./firefox_${VERSION}_${ARCH}.deb ./temp-package
for deb_file in ./firefox-l10n-*.deb; do
    # 检查文件是否存在（防止没有匹配文件时执行）
    if [ -f "$deb_file" ]; then
        echo "正在处理: $deb_file"
        dpkg -X "$deb_file" ./temp-package
        echo "完成: $deb_file"
        echo "------------------------"
    fi
done

mv ./temp-package/usr/lib/firefox-addons/extensions ./temp-package/usr/lib/firefox/browser/extensions
mv ./temp-package/usr/lib/firefox/ ./opt/apps/firefox-spark/files/firefox
rm -rf ./temp-package
rm -rf *.deb
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=Spark Store
version=1.0
about=Mozilla Firefox for Spark Store

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


dpkg-deb -Z xz -b . ${STORE_APP_PATH}/${PKG_NAME}_${VERSION}_${ARCH}.deb
popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
