#!/bin/bash


export DIST_ROOT="/userdata/apt-mirror/mirror/mirror.tuna.tsinghua.edu.cn"
#export URL_BASE="https://mirrors.ustc.edu.cn"
export URL_BASE="https://mirror.tuna.tsinghua.edu.cn"
 

function download_InRelease()
{
    SITE=$1
    shift 1
    BRANCH=$@
    
    for i in ${BRANCH[@]}; do
        DIR1=${DIST_ROOT}/${SITE}/dists/${i}
        URL1=${URL_BASE}/${SITE}/dists/${i}
        
        mkdir -p ${DIR1} 
        
        wget ${URL1}/InRelease --output-document=InRelease 
        if [ $? -eq 0 ]; then
            #ls -l   
            touch InRelease  
            mv -v InRelease ${DIR1}/InRelease
            echo "#### ${DIR1}/InRelease"
        fi    
    done 
    return 
}


function download_pkg()
{
    SITE=$1
    shift 1
    BRANCH=$@
    
    for i in ${BRANCH[@]}; do
        DIR1=${DIST_ROOT}/${SITE}/dists/${i}
        URL1=${URL_BASE}/${SITE}/dists/${i}
        
        mkdir -p ${DIR1}/
       
		ARR=("i18n")
        for j in  ${ARR[@]}; do  
			mkdir -p ${DIR1}/${j}
		
			echo "#### wget ${URL1}/${j}"
			wget  ${URL1}/${j} --output-document=/tmp/listpage.html
			while [ 0 ]; do
				grep -q "Translation-en.xz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="xz"
					wget ${URL1}/${j}/Translation-en.${zip}  --output-document=Translation-en.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Translation-en.${zip}  
						mv -v Translation-en.${zip}  ${DIR1}/${j}/Translation-en.${zip}  
						echo "#### ${DIR1}/${j}/Translation-en.${zip} "
					fi 
					break
				fi
				
				grep -q "Translation-en.bz2" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="bz2"
					wget ${URL1}/${j}/Translation-en.${zip}  --output-document=Translation-en.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Translation-en.${zip}  
						mv -v Translation-en.${zip}  ${DIR1}/${j}/Translation-en.${zip}  
						echo "#### ${DIR1}/${j}/Translation-en.${zip} "
					fi 
					break
				fi
				
				grep -q "Translation-en.gz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="gz"
					wget ${URL1}/${j}/Translation-en.${zip}  --output-document=Translation-en.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Translation-en.${zip}  
						mv -v Translation-en.${zip}  ${DIR1}/${j}/Translation-en.${zip}  
						echo "#### ${DIR1}/${j}/Translation-en.${zip} "
					fi 
					break
				fi
				zip="*"
				RET=1 
				break
			done  ####
			rm -f /tmp/listpage.html
			if [ ${RET} -ne 0 ]; then
				echo "#### ERROR: ${URL1}/${j}/Translation-en.${zip} "	
			fi
		done
        
        ARR=("binary-all" "binary-arm64")
        for j in  ${ARR[@]}; do 
            mkdir -p ${DIR1}/${j}/ 
			
			echo "#### wget ${URL1}/${j}"
			wget  ${URL1}/${j} --output-document=/tmp/listpage.html
			while [ 0 ]; do
			    grep -q "Packages.xz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="xz"
					wget ${URL1}/${j}/Packages.${zip}  --output-document=Packages.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Packages.${zip}  
						mv -v Packages.${zip}  ${DIR1}/${j}/Packages.${zip}  
						echo "#### ${DIR1}/${j}/Packages.${zip} "
					fi 
					break
				fi
				
				grep -q "Packages.bz2" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="bz2"
					wget ${URL1}/${j}/Packages.${zip}  --output-document=Packages.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Packages.${zip}  
						mv -v Packages.${zip}  ${DIR1}/${j}/Packages.${zip}  
						echo "#### ${DIR1}/${j}/Packages.${zip} "
					fi 
					break
				fi
				
				grep -q "Packages.gz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="gz"
					wget ${URL1}/${j}/Packages.${zip}  --output-document=Packages.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Packages.${zip}  
						mv -v Packages.${zip}  ${DIR1}/${j}/Packages.${zip}  
						echo "#### ${DIR1}/${j}/Packages.${zip} "
					fi 
					break
				fi
				zip="*"
				RET=1 
				break
			done  ####
			rm -f /tmp/listpage.html
            if [ ${RET} -ne 0 ]; then
				echo "#### ERROR: ${URL1}/${j}/Packages.${zip} "	
			fi
			
        done
    done 
    return
}


function download_src()
{
    SITE=$1
    shift 1
    BRANCH=$@
    
    for i in ${BRANCH[@]}; do
        DIR1=${DIST_ROOT}/${SITE}/dists/${i}
        URL1=${URL_BASE}/${SITE}/dists/${i}
        
        ARR=("source" )
        for j in  ${ARR[@]}; do  
			mkdir -p ${DIR1}/${j}/
			
			echo "#### wget ${URL1}/${j}"
			wget  ${URL1}/${j} --output-document=/tmp/listpage.html
			while [ 0 ]; do
			    grep -q "Sources.xz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="xz"
					wget ${URL1}/${j}/Sources.${zip}  --output-document=Sources.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Sources.${zip}  
						mv -v Sources.${zip}  ${DIR1}/${j}/Sources.${zip}  
						echo "#### ${DIR1}/${j}/Sources.${zip} "
					fi 
					break
				fi
				
				grep -q "Sources.bz2" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="bz2"
					wget ${URL1}/${j}/Sources.${zip}  --output-document=Sources.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Sources.${zip}  
						mv -v Sources.${zip}  ${DIR1}/${j}/Sources.${zip}  
						echo "#### ${DIR1}/${j}/Sources.${zip} "
					fi 
					break
				fi
				
				grep -q "Sources.gz" /tmp/listpage.html
				if [ $? -eq 0 ]; then
					zip="gz"
					wget ${URL1}/${j}/Sources.${zip}  --output-document=Sources.${zip} 
					RET=$?
					if [ ${RET} -eq 0 ]; then
						#ls -l  
						touch Sources.${zip}  
						mv -v Sources.${zip}  ${DIR1}/${j}/Sources.${zip}  
						echo "#### ${DIR1}/${j}/Sources.${zip} "
					fi 
					break
				fi
				zip="*"
				RET=1 
				break
			done  ####
			rm -f /tmp/listpage.html
            if [ ${RET} -ne 0 ]; then
				echo "#### ERROR: ${URL1}/${j}/Sources.${zip} "	
			fi
        done
    done 
    return 
}
 

rm  -f InRelease Packages.xz* Translation-en.xz*


#### 共下载3个InRelease文件
SITE="debian"
BRANCHS=("bullseye" "bullseye-updates" ) ##"bullseye-backports" )
download_InRelease ${SITE} ${BRANCHS[@]}

#### 共下载1个InRelease文件
SITE="debian-security"
BRANCHS=("bullseye-security")
download_InRelease ${SITE} ${BRANCHS[@]}


#### 共下载6个Translation-en文件，12个Packages文件，6个Sources文件
SITE="debian" 
BRANCHS=("bullseye/main" "bullseye/contrib" "bullseye-updates/main"  "bullseye-updates/contrib" ) ## "/bullseye-backports/main" "bullseye-backports/contrib" 
download_pkg ${SITE} ${BRANCHS[@]}
download_src ${SITE} ${BRANCHS[@]}

#### 共下载2个Translation-en文件，4个Packages文件，2个Sources文件
SITE="debian-security"
BRANCHS=("bullseye-security/main" "bullseye-security/contrib")
download_pkg ${SITE} ${BRANCHS[@]}
download_src ${SITE} ${BRANCHS[@]}

#### 合计4个InRelease文件 8个Translation-en文件，16个Packages文件，8个Sources文件
find . -name "InRelease" | xargs ls -l | grep "bullseye"
echo
find . -name "Translation-en.*" | xargs ls -l | grep "bullseye"
echo
find . -name "Packages.*" | xargs ls -l | grep "bullseye"
echo
find . -name "Sources.*" | xargs ls -l | grep "bullseye"
echo

find ./debian ./debian-security   -type f -size 0c | xargs rm -f 

