#!/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.github.clash-for-windows"
APP_NAME="Clash For Windows(汉化版)"
GITHUB_REPO="Fndroid/clash_for_windows_pkg"
VERSION="" #Here we use other way to get version so do not write here
TAGS="community;ubuntu;deepin;uos"
CATAGORY="network" #https://gitee.com/deepin-community-store/spark-store/blob/dev/DOCS/spk-doc.md
DETAIL="Clash For Windows 中文版，现在由Spark Building Service维护" # This should be written as json format




################################# Fill the variables below only if you use write_control function
MAINTAINER="shenmo<shenmo@spark-app.store>"
SECTION="net"
PRIORITY="optional"
DEPENDS="nftables, iproute2"
HOMEPAGE="https://github.com/Fndroid/clash_for_windows_pkg"
DESCRIPTION_SHORT="A rule-based tunnel in Go."
DESCRIPTION_LONG="A Windows/macOS GUI based on Clash."
PROVIDES=""
REPLACES=""
RECOMMENDS=""

source $HERE_PATH/sbs-utils.bashimport
source $HERE_PATH/sbs-build-helper.bashimport
if [ "$1" = "" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="x64"
elif [ "$1" = "amd64" ] || [ "$1" = "x64" ];then
ARCH="amd64"
ARCH_ANOTHERWAY="x64"
elif [ "$1" = "arm64" ] || [ "$1" = "arm" ];then
ARCH="arm64"
ARCH_ANOTHERWAY="arm64"
else
log.error "Err:Invalid Architecture"
exit 1
fi


function prepare(){
    aptss_get_update
    PKG_VER_APTSS=`get_version_from_aptss ${PKG_NAME}`
    PKG_VER_UPSTREAM=`get_version_from_github ${GITHUB_REPO}`
    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
init_package_directory


url1="https://ghproxy.com/https://github.com/Fndroid/clash_for_windows_pkg/releases/download/${VERSION}/Clash.for.Windows-${VERSION}-${ARCH_ANOTHERWAY}-linux.tar.gz"
url2="https://ghproxy.com/https://github.com/Z-Siqi/Clash-for-Windows_Chinese/releases/download/CFW-V${VERSION}_CN/app.7z"

############# Now download and get the extracted file(Later will intergrate to SBS)

aria2c $url1 -d "opt/apps/${PKG_NAME}/files/"
tar -xvf "opt/apps/${PKG_NAME}/files/Clash.for.Windows-${VERSION}-${ARCH_ANOTHERWAY}-linux.tar.gz" -C "opt/apps/${PKG_NAME}/files"

rm "opt/apps/${PKG_NAME}/files/Clash.for.Windows-${VERSION}-${ARCH_ANOTHERWAY}-linux.tar.gz"
mv "opt/apps/${PKG_NAME}/files/Clash for Windows-${VERSION}-${ARCH_ANOTHERWAY}-linux"/* "opt/apps/${PKG_NAME}/files"
rm -rf "opt/apps/${PKG_NAME}/files/Clash.for.Windows-${VERSION}-${ARCH_ANOTHERWAY}-linux"


############# Patch Chinese language
rm "opt/apps/${PKG_NAME}/files/resources/app.asar"
aria2c $url2 -d "opt/apps/${PKG_NAME}/files/resources/"
pushd opt/apps/${PKG_NAME}/files/resources/
7z x app.7z
rm app.7z
popd

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




popd

}


prepare
build
write_json $STORE_APP_PATH

rm -rf $WORKDIR
