#!/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="microsoft-edge-dev"
APP_NAME="Microsoft Edge Dev开发者通道"
SOURCE_LIST_FILE=""
GITHUB_REPO=""
VERSION="" #Here we use other way to get version so do not write here
TAGS="ubuntu;deepin;uos;debian"
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Access the Internet/连接网络\nMicrosoft Edge is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier./Microsoft Edge 是一款将极简设计与先进技术相结合的浏览器，使网络变得更快、更安全、更轻松。\n每周更新\n比金丝雀更稳定。过去一周改进的最佳代表。\n我们的三个预览频道（Canary、Dev 和 Beta）可在所有受支持的 Windows、Windows Server 以及 macOS、Mobile 和 Linux 版本上使用。安装预览频道不会卸载已发布的 Microsoft Edge 版本，你可以同时安装多个预览频道。\n\n\n                    以下是 Microsoft Edge 新增功能介绍\n                从移动设备上传文件或照片\n通过扫描二维码，快速连接与 PC 处于同一网络的任何移动设备。这样，您便可以在 PC 上访问移动设备上的文件。\n阅读任何语言的 PDF\n只需右键单击 PDF，即可在 Microsoft Edge 边栏中将文本翻译为您的首选语言。" # This should be written as json format
MAINTAINER="shenmo<shenmo@spark-app.store>"
HOMEPAGE="https://www.microsoft.com/zh-cn/edge"


################################# 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_VER_UPSTREAM=`get_version_from_source_list_file ${PKG_NAME}:${ARCH} /etc/apt/sources.list.d/edge.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 $WORKDIR
apt update
apt download ${PKG_NAME}:${ARCH}=${VERSION}
PKG_FILE="${PKG_NAME}_${VERSION}_${ARCH}.deb"
dpkg -X ${PKG_FILE} $(pwd)
dpkg -e ${PKG_FILE} $(pwd)/DEBIAN

if [ -e "$HERE_PATH/template" ];then
cp -rv "$HERE_PATH/template/"* $(pwd)
fi

rm -vf ${PKG_FILE}
#write_control

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

popd

}



prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
