#!/bin/bash # ------------ Begin Configuration ------------ # # The log file LOG=/var/log/pacget.log # Number of connections per server (don't go beyond 2, please) CONNECTIONS=2 # Max number of servers to download from (follows ordering in include file) SERVERS=4 # Maximum download speed (0 = unrestricted) MAX_SPEED=0 # Minimum download speed (0 = don't care) MIN_SPEED=0 # Maximum tries per download MAX_TRIES=2 # Server timeout period TIMEOUT=15 # Passive FTP or not: 'yes' or 'no' FTP_PASV=no # 'none' or 'prealloc' FILE_ALLOC=none #convert host to IP address if host resolves into multiple ip's: 'yes or 'no' (requires dnsutils, uses dig) #(This resolves the error issue with aria2c) CONVERT_TO_IP=no # ------------- End Configuration ------------- # ARIA2_BIN=$(which aria2c 2> /dev/null) if [ ! -x ${ARIA2_BIN} ]; then echo "aria2c was not found or isn't executable." exit 1 fi filename=$(basename ${1}) server=${1%/${filename}} # Determine which repo is being used if [ -n "$(grep "${server}" /etc/pacman.d/*)" ]; then repo=$(grep "${server}" /etc/pacman.d/* | cut -d ':' -f 1 | cut -d '/' -f 4) else repo="testing_or_custom" fi # Override number of connections for db files if [[ ${filename} = *.db.tar.gz ]]; then CONNECTIONS=1 fi # For db files, or when using [testing] or a custom repo (which most likely doesn't have any mirror), # use only the URL passed by pacman; Otherwise, extract the list of servers (from the include file of the repo) to download from if [[ ${filename} = *.db.tar.gz || "${repo}" = "testing_or_custom" ]]; then url="${1}" else url=$(sed -n 's/^Server = //; s,$,/'"${filename}"', ; /^ftp/p; /^http/p' /etc/pacman.d/${repo} | head -n ${SERVERS}) fi #convert URL to IP if [ "${CONVERT_TO_IP}" = "yes" ];then for mirror in $url;do host=$(cut -d/ -f3 <<<$mirror) if [[ $(dig +short $host|grep '^[0-9]'|wc -l) -gt 1 ]];then ip=$(dig +short $host|grep '^[0-9]'|head -n1) nurl+=$(sed "s,$host,$ip," <<<$mirror)' ' else nurl+=$mirror fi done url=$nurl fi # Passive FTP or not? [ "${FTP_PASV}" = "yes" ] && OPT_FTP_PASV="--ftp-pasv" || OPT_FTP_PASV="" echo "Target: ${filename}" ${ARIA2_BIN} --log=${LOG} --timeout=${TIMEOUT} --max-tries=${MAX_TRIES} --allow-overwrite=true \ --split=${CONNECTIONS} ${OPT_FTP_PASV} --file-allocation=${FILE_ALLOC} \ --lowest-speed-limit=${MIN_SPEED} --max-download-limit=${MAX_SPEED} \ ${url} --out=${filename}.pacget && [ ! -f ${filename}.pacget.aria2 ] && mv ${filename}.pacget ${2} exit 0