#!/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="code"
APP_NAME="Visual Studio Code(vscode)"
#GITHUB_REPO="Fndroid/clash_for_windows_pkg" #格式像这样。只在从github获取信息才使用，如果不是，则注释掉。后续下载也整合进入helper
VERSION="" # 定义版本号。或者，在后续prepare或build中定义。可使用其他两个bashimport文件中的功能
TAGS="community;ubuntu;deepin;uos" # 星火的tag信息
CATAGORY="development" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="VSCode是一款非常牛逼的编辑器" # This should be written as json format
MAINTAINER="shenmo<shenmo@spark-app.store>"
HOMEPAGE="https://code.visualstudio.com/"




################################# Fill the variables below only if you use write_control function
SECTION="utils"
PRIORITY="optional"
DEPENDS=""
PROVIDES=""
REPLACES=""
RECOMMENDS=""
DESCRIPTION_SHORT=""
DESCRIPTION_LONG=""
################################# Here we import our tools
source $HERE_PATH/sbs-utils.bashimport
source $HERE_PATH/sbs-build-helper.bashimport
##这两个里面有详尽的解释
if [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY=""
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_VER_UPSTREAM=`get_version_from_source_list_file ${PKG_NAME}:${ARCH} /etc/apt/sources.list.d/vscode.list`
    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

aptss download ${PKG_NAME}:${ARCH}=${VERSION}


popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR

