#!/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="xbyyunpan"
APP_NAME="阿里云盘（小白羊修复版）"
GITHUB_REPO="gaozhangmin/aliyunpan" #格式像这样。只在从github获取信息才使用，如果不是，则注释掉。后续下载也整合进入helper
VERSION="" # 定义版本号。或者，在后续prepare或build中定义。可使用其他两个bashimport文件中的功能
TAGS="ubuntu;deepin;uos" # 星火的tag信息
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="修复版小白羊云盘\n基于阿里云盘网页版开发的第三方Linux客户端(超好用)。\n***关于安装后应用没有图标问题：\n0.打开终端，依次输入以下命令：\n1.sudo gedit /usr/share/applications/xbyyunpan.desktop\n(deepin和uos需要将gedit换为vim，vim用法请自行百度\n2.将“Icon=xbyyunpan”一行改为\n“Icon=/opt/小白羊云盘/resources/images/icon_256x256.png”\n3.保存退出" # This should be written as json format
HOMEPAGE="https://github.com/gaozhangmin/aliyunpan"
MAINTAINER="shenmo<shenmo@spark-app.store>"


################################# Fill the variables below only if you use write_control function
SECTION="utils"
PRIORITY="optional"
DEPENDS="依赖"
DESCRIPTION_SHORT="短介绍"
DESCRIPTION_LONG="长介绍"
PROVIDES=""
REPLACES=""
RECOMMENDS=""

################################# 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()：获取软件包目录的大小并返回值

VER_APTSS=$(get_version_from_source_list_file ${PKG_NAME} /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore${ARCH_ANOTHERWAY}.list)
VER_GITHUB=$(get_version_from_github $GITHUB_REPO)
VER_GITHUB_NO_v=$(echo $VER_GITHUB | sed 's/^[^0-9]*//')
compare_version ${VER_APTSS} ${VER_GITHUB_NO_v}
}




function build(){

pushd $STORE_APP_PATH

# 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

url1="https://github.com/${GITHUB_REPO}/releases/download/${VER_GITHUB}/XBYDriver-${VERSION}-linux-${ARCH}.deb"
echo $url1
aria2c $url1 --check-certificate=false -o ${PKG_NAME}_${VERSION}_${ARCH}.deb

popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
