Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # architettura definita automaticamente
- arch=$(uname -m)
- # Configurazione di aria2c
- ariaconf="--max-concurrent-downloads=44
- --file-allocation=none
- --continue
- --split=4
- --max-connection-per-server=4
- --max-tries=2
- --min-split-size=1M
- --allow-overwrite=true
- --summary-interval=0
- --log-level=error
- --no-conf
- --remote-time=true
- --timeout=5
- --dir=/var/cache/pacman/pkg
- --uri-selector=inorder"
- version="0.1b4"
- PROGNAME=$0
- # Si scegli se optare per confermare l' installazione da pacman
- NOCONFIRM=''
- #dichiaro un vettore
- declare -a args
- #ci butto dentro i miei argomenti
- args=("$@")
- print_help() {
- echo "Utilizzo: $PROGNAME <operazione> [...]"
- echo ""
- echo "<operazioni>:"
- echo -e "\t $PROGNAME -S [opzioni] {pacchetto/i}"
- echo -e "\t $PROGNAME -{R,U,Q,D,T,V} [opzioni] {pacchetto/i}"
- echo -e "\t $PROGNAME -v"
- echo ""
- }
- #funzione di creazione lista "liscia"
- update_mirror() {
- sed -e '/^#/d' -e '/^$/d' -e 's/Server = //' /etc/pacman.d/mirrorlist > /tmp/stripped_mirrorlist
- }
- #funzione di generazione lista pacchetti
- gen_pkglist() {
- declare -a argsnoupdate
- index=0
- for i in "${args[@]}"
- do
- if [[ $i = "--refresh" ]]; then
- :
- elif [[ $i = "-y" ]]; then
- :
- elif [[ $i =~ ^"--" ]]; then
- argsnoupdate[$index]=$i
- let index+=1
- elif [[ $i =~ ^-*y* ]]; then
- argsnoupdate[$index]=$(echo $i |sed 's/y//')
- let index+=1
- else
- argsnoupdate[$index]=$i
- let index+=1
- fi
- done
- pacman ${argsnoupdate[*]} -p --print-format %l£%s | sed '/.pkg.tar.[gx]z/!d' >/tmp/pkglist
- gawk -F"/" '{print $(NF)}' /tmp/pkglist | gawk -F"-" '{NF--; print}' | sed 's/ /-/g' | \
- sed ':a;N;$!ba;s/\n/ /g' | fold -s --width=$(tput cols)
- echo ""
- }
- # funzione di generazione lista degli url creata dall'unione di lista mirror + lista pacchetti
- gen_addresslist() {
- cat /dev/null > /tmp/addresslist
- total_size=0
- while read pkg
- do
- repo=$(echo $pkg|gawk -F"/" '{print $(NF-3)}')
- pack=$(echo $pkg|gawk -F[/£] '{print $(NF-1)}')
- shuf /tmp/stripped_mirrorlist |sed -e 's/$repo/'${repo}'/' | sed -e 's/$arch/'$arch'/' | \
- sed -e 's/$/\/'${pack}'/' | sed -e '$!{:a;N;s/\n/\t/;ta}' >> /tmp/addresslist
- size=${pkg#*£}
- let total_size+=$size
- done < /tmp/pkglist
- total_size_h=$(($total_size/1048576))
- echo "Dimensione totale dei file da scaricare:" $total_size "Byte" "(~"$total_size_h "MiB)"
- echo ""
- }
- #funzione di download della lista in tmp
- download() {
- if [[ $total_size != 0 ]]; then
- while true; do
- read -p "Vuoi eseguire il download? [S/n] " Sn
- case $Sn in
- ""|[Ss]) nice_print ; break;;
- [Nn]) exit;;
- *) echo "Type S or N"
- esac
- done
- fi
- }
- nice_print() {
- log=$(mktemp /tmp/aria2-out.XXXXX)
- trap "rm -f $log" SIGINT SIGTERM EXIT
- aria2c -i /tmp/addresslist $ariaconf &>"$log" &
- aria2_pid=$!
- while (ps $aria2_pid &>/dev/null && [[ -f $log ]]); do
- if grep -q "ERR|." "$log"; then
- break
- fi
- #calcolo la larghezza della console, per cancellare lo stampato del
- # ciclo precedente. Calcolo dinamico, per ovviare al caso
- # di ridimensionamento della console
- TCOLS=$(stty -a | tr -s ';' '\n' | grep "column" | sed s/'[^[:digit:]]'//g)
- cout=$(sed -n '/[\[0-9\]* SIZE:.*ETA:/p' "$log" | tail -n1)
- #numero di spazi meno la lunghezza dell' output di aria2
- spaces='' ; NUMspace=$(($TCOLS-${#cout}))
- for((j=0; j<$NUMspace; j++)); do
- spaces="$spaces "
- done
- echo -en "\r$cout$spaces"
- done
- if [[ ! -f "$log" ]]; then
- echo -e "\nError: Interrupted"
- return 1
- fi
- #gli errori verranno ripresi da pacman durante la verifica dei pacchetti
- if grep -q "ERR|.*$fname" "$log"; then
- echo -e "\nError: Download Failure"
- return 1
- fi
- if grep -q "errorCode=" "$log"; then
- echo "\nError: something went wrong"
- return 2
- fi
- rm -f $log >/dev/null
- return 0
- }
- #from yaourt
- explode_args() {
- unset OPTS
- local args=$1
- while [[ $args ]]; do
- [[ $args = "--" ]] && OPTS+=("$@") && break;
- if [[ ${args:0:1} = "-" && ${args:1:1} != "-" ]]; then
- OPTS+=("-${args:1:1}")
- (( ${#args} > 2 )) && args="-${args:2}" || { shift; args=$1; }
- else
- OPTS+=("$args"); shift; args=$1
- fi
- done
- }
- action() {
- # generazione lista pacchetti
- gen_pkglist
- # preparazione la lista degli url
- gen_addresslist
- download
- rm -f /tmp/addresslist >/dev/null
- rm -f /tmp/stripped_mirrorlist >/dev/null
- rm -f /tmp/pkglist >/dev/null
- }
- pacman_sync() {
- if [ $(whoami) != "root" ] ; then
- echo "Devi essere root per continuare!"
- exit 1
- fi
- if [[ $UPGRADE="--upgrades" || $DOWNONLY="--downloadonly" ]] ; then
- PACSPEED="1"
- else
- PACSPEED="0"
- fi
- A=${IGNOREPKG[*]}
- echo $A
- # creo mirrorlist "liscia"
- update_mirror
- #Se c'è un -S
- if [ $SYNC == "1" ] ; then
- #Se c'è la possibilità di accelerare il download
- if [ $PACSPEED == "1" ] ;then
- #Se c'è un -y, prima cosa che faccio è aggiornare la lista
- if [[ ${REFRESH[0]} = "--refresh" ]] ; then
- pacman -S ${REFRESH[*]} $NOPBAR
- fi
- #Se c'è un -w
- if [[ $DOWNONLY = "--downloadonly" ]] ; then
- action
- fi
- #Se c'è un -u
- if [[ $UPGRADE="--upgrades" ]] ; then
- #controllo se c'è qualche ignore
- if [[ $IS_IGNOREPKG = "1" && $IS_IGNOREGRP = "1" ]] ; then
- echo 1
- while [[ $IGNOREGRP ]] ; do IGNOREARRAY+="--ignoregroup ${IGNOREGRP[0]}" ; shift ; done
- while [[ $IGNOREPKG ]] ; do IGNOREARRAY+="--ignore ${IGNOREPKG[0]}" ; shift ; done
- elif [[ $IS_IGNOREPKG = "1" && $IS_IGNOREGRP != "1" ]] ; then
- echo 2
- while [[ ${IGNOREPKG[*]} ]] ; do IGNOREARRAY+="--ignore ${IGNOREPKG[0]}" ; echo ${IGNOREARRAY[*]} ; shift ; done
- elif [[ $IS_IGNOREPKG != "1" && $IS_IGNOREGRP = "1" ]] ; then
- echo 3
- while [[ $IGNOREGRP ]] ; do IGNOREARRAY+="--ignoregroup ${IGNOREGRP[0]}" ; shift ; done
- fi
- #Se c'è un --print continua normale
- #sennò accelero il download
- if [[ $PRINT != "--print" ]] ; then # TODO mancano il -S -Sg e basta
- action
- pacman -Su $NOCCONFIRM $NOPBAR $FORCE ${NODEPS[*]} ${OTHER[*]} ${IGNOREARRAY[*]}
- else
- pacman ${args[*]}
- fi
- fi
- fi
- fi
- }
- #from yaourt
- # Explode arguments (-Su -> -S -u)
- ARGSANS=("$@")
- explode_args "$@"
- set -- "${OPTS[@]}"
- unset OPTS
- #from yaourt, only -S section
- while [[ $1 ]]; do
- case "$1" in
- -S|--sync) SYNC=1 ;;
- -p|--print) PRINT="--print" ;;
- -f|--force) FORCE="--force" ;;
- -g|--groups) GROUP="--groups" ;;
- -u|--upgrades) UPGRADE="--upgrades" ;;
- -y|--refresh) REFRESH+=("--refresh") ;;
- -w|--downloadonly) DOWNLONLY="--downloadonly" ;;
- ##-d) ((DEPENDS++)); program_arg $((A_PS | A_M | A_PQ)) $1 ;;
- -d) NODEPS+=("--nodeps") ;;
- --noprogressbar|--noscriptlet) NOPBAR="--noprogressbar" ;;
- --ignore) shift; IGNOREPKG+=("$1") ; IS_IGNOREPKG=1 ;;
- --ignoregroup) shift; IGNOREGRP+=("$1") ; IS_IGNOREGRP=1 ;;
- ##--config|--dbpath|-r|--root) program_arg $((A_PC | A_PKC)) "$1" "$2"; shift ;;
- ##--cachedir|--logfile|--arch) program_arg $A_PC "$1" "$2"; shift ;;
- --noconfirm) NOCONFIRM="--noconfirm" ;;
- --*) ;;
- -*) ;;
- *) OTHER+=("$1") ;;
- esac
- shift
- done
- #scarico pacchetti con le seguenti combinazioni:
- # -Su
- # -Syu
- # -Sw
- # -Sf
- # -S
- case ${args[0]} in
- -h|--help) print_help
- ;;
- -S*) pacman_sync
- ;;
- -v|--version) echo "$version"-"$arch"
- ;;
- *) pacman ${args[*]}
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement