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

################################# Here we import our tools


################################# 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="com.alibabainc.dingtalk"
APP_NAME="钉钉"
SOURCE_LIST_FILE=""
GITHUB_REPO=""
VERSION="" #Here we use other way to get version so do not write here
TAGS="ubuntu;deepin;uos;debian;native"
CATAGORY="chat" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="多端同步，无缝切换\n钉钉客户端同时支持 Android，iOS，iPadOS，macOS，Windows，Linux 版本确保您的钉钉客户端为最新版本，以获取最新功能" # This should be written as json format
MAINTAINER="shenmo<shenmo@spark-app.store>"
HOMEPAGE="https://www.dingtalk.com"


################################# Fill the variables below only if you use write_control function

SECTION=""
PRIORITY=""
DEPENDS=""
DESCRIPTION_SHORT=""
DESCRIPTION_LONG=""
PROVIDES=""
REPLACES=""
RECOMMENDS=""

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


function prepare(){
    aptss_get_update
    PKG_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`
    PKG_URL=$(curl -sw %{redirect_url} https://www.dingtalk.com/win/d/qd=linux_${ARCH})
    PKG_VER_UPSTREAM=$(echo $PKG_URL | sed 's#.*/##g' | cut -d '_' -f 2)
    compare_version ${PKG_VER_APTSS} ${PKG_VER_UPSTREAM}
    # Compare version will also give the value to $VERSION
    # Now we should have the $VERSION 
    if [ -e $VERSION ];then
    log.error "VERSION is empty. This should not happen! exit."
    fi
}




function build(){

pushd $STORE_APP_PATH

aria2c ${PKG_URL}

popd

}


prepare
build
if [[ "$ARCH" = "amd64" ]];then
amber-pm-convert --base amber-pm-bookworm ./${PKG_NAME}_${VERSION}_${ARCH}.deb --version $VERSION
fi
write_json $STORE_APP_PATH


rm -rf $WORKDIR
