#!/bin/bash

SPARK_DOWNLOAD_SERVER_URL="https://d.spark-app.store/"
SPARK_DOWNLOAD_SERVER_URL_NO_PROTOCOL="d.spark-app.store"
source /opt/durapps/spark-store/bin/bashimport/transhell.amber
source /opt/durapps/spark-store/bin/bashimport/log.amber
load_transhell

case `arch` in
   x86_64)
	STORE_URL="store"
	STORE_LIST_URL=""
	;;
   aarch64)
	STORE_URL="aarch64-store"
	STORE_LIST_URL="-aarch64"
	;;
   loongarch64)
   	STORE_URL="loong64-store"
   	STORE_LIST_URL="-loong64"
   	;;
esac
SS_APT_FAST="/opt/durapps/spark-store/bin/apt-fast/ss-apt-fast"


is_empty_dir(){ 
    return `ls -A $1|wc -w`
}

function update_list(){
curl --progress-bar -o  /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore.list "${SPARK_DOWNLOAD_SERVER_URL}/sparkstore${STORE_LIST_URL}.list"
log.info "sparkstore${STORE_LIST_URL}.list update done"  
}

function update_conf(){
mkdir -p /tmp/aptss-conf/
curl  --progress-bar -o  /tmp/aptss-conf/apt-fast.conf "${SPARK_DOWNLOAD_SERVER_URL}/apt-fast.conf"
log.info "apt-fast.conf update done"
chmod -R 755 /tmp/aptss-conf  
}

if [ "$(id -u)" != "0" ];then
#############################无root权限时
echo -e "\e[1;32m${TRANSHELL_CONTENT_RUNNING_IN_NOT_ROOT_USER}\e[0m"

else


ln -sf /etc/apt/sources.list.d/* /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d
###让这里和系统同步，先链接，然后清除无效链接
find /opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d -xtype l -delete

fi

if [ ! -e "/tmp/aptss-conf/apt-fast.conf" ];then
###刷新apt-fast配置
mkdir -p /tmp/aptss-conf/
echo -e "\e[1;32m${TRANSHELL_CONTENT_GETTING_SERVER_CONFIG_AND_MIRROR_LIST}\e[0m"
echo
update_conf

fi


if [ ! -e "/var/lib/aptss/lists/${SPARK_DOWNLOAD_SERVER_URL_NO_PROTOCOL}_${STORE_URL}_Packages" ] && [ ! -e "/var/lib/aptss/lists/d.store.deepinos.org.cn_${STORE_URL}_Packages" ] && [ ! -e "/var/lib/aptss/lists/mirrors.sdu.edu.cn_spark-store-repository_${STORE_URL}_Packages" ];then

mkdir -p /tmp/aptss-conf/
echo -e "\e[1;32m${TRANSHELL_CONTENT_GETTING_SERVER_CONFIG_AND_MIRROR_LIST}\e[0m"
echo

update_list
update_conf 

#只更新星火源


fi





if  [ "$1" = "install" ] || [ "$1" = "upgrade" ] || [ "$1" = "full-upgrade" ] || [ "$1" = "dist-upgrade" ]; then



###执行

${SS_APT_FAST} "$@" --allow-downgrades -c /opt/durapps/spark-store/bin/apt-fast-conf/aptss-apt.conf
ret="$?"
if [ "$ret" -ne 0 ];then
echo -e "\e[1;33m$TRANSHELL_CONTENT_PLEASE_USE_APTSS_INSTEAD_OF_APT\e[0m"
exit $ret
fi




elif [ "$1" = "ssupdate" ];then

mkdir -p /tmp/aptss-conf/
echo -e "\e[1;32m${TRANSHELL_CONTENT_GETTING_SERVER_CONFIG_AND_MIRROR_LIST}\e[0m"
echo
 

update_list
update_conf
 
/usr/bin/apt update -c /opt/durapps/spark-store/bin/apt-fast-conf/aptss-apt.conf -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" -o Dir::Etc::sourcelist="/opt/durapps/spark-store/bin/apt-fast-conf/sources.list.d/sparkstore.list"

#只更新星火源

elif [ "$1" = "update" ];then

echo -e "\e[1;32m${TRANSHELL_CONTENT_GETTING_SERVER_CONFIG_AND_MIRROR_LIST}\e[0m"
echo
update_list
update_conf
### 额外一份拿来给aptss自动补全用

                ${SS_APT_FAST} "$@" -c /opt/durapps/spark-store/bin/apt-fast-conf/aptss-apt.conf

else
                ${SS_APT_FAST} "$@" -c /opt/durapps/spark-store/bin/apt-fast-conf/aptss-apt.conf
fi

