diff options
author | Joseph Graham <joseph@fibreglass.tunachunks> | 2014-04-23 10:31:41 +0100 |
---|---|---|
committer | Joseph Graham <joseph@fibreglass.tunachunks> | 2014-04-23 10:31:41 +0100 |
commit | f1fe83def1b4cc7e9e5b5d335be45aabe44c001f (patch) | |
tree | f646523e024b4e910e0a00c25cb195aebfd9c2f2 /pacman2pacman-get |
Initial commit. Everything beleived to be i working order.
Diffstat (limited to 'pacman2pacman-get')
-rw-r--r-- | pacman2pacman-get | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/pacman2pacman-get b/pacman2pacman-get new file mode 100644 index 0000000..7dde661 --- /dev/null +++ b/pacman2pacman-get @@ -0,0 +1,137 @@ +#! /bin/bash +# /usr/bin/pacman2pacman-get +# +# Copyright (C) 2014 Joseph Graham <joseph@t67.eu> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +# Where Pacman2pacman will look to find .torrent files. +torrents='http://repo.parabolagnulinux.org/torrents/' + +# Pacman2pacman should be called like this from /etc/pacman.conf: +# XferCommand = /usr/bin/pacman2pacman-get %u %o + +# Check that the args are not empty. +[[ -z ${1} ]] && { echo "The first argument should be the download url." ; exit 1 ; } +[[ -z ${2} ]] && { echo "The second argument should be the local filename, plus a “.part” extension." ; exit 1 ; } + +url="${1}" # The download url. +filename="${2}" # This is where the downloaded file needs to be saved + # or hardlinked + +# Check if transmission daemon is running. +if ! systemctl show --property='ActiveState' transmission | grep -q 'ActiveState=active' +then + echo 'Error: The transmission daemon is not running.' + exit 1 +fi + +torrent_url="${torrents}${url##*/}.torrent" + +cd /srv/pacman2pacman/torrents + +# Find the name of the package that will be displayed +pname="${url##*/}" +pname="${pname%%-[[:digit:]]*}" + +# Check for a .torrent. +if curl -O -f -L -s -A 'Pacman2pacman' "${torrent_url}" +then + # Add the torrent to transmission + transmission-remote -a "${url##*/}.torrent" -w /srv/pacman2pacman/downloads &>/dev/null + + # The torrent is now downloading. To get info about the torrent in + # order to display a progress bar we need to know it's id. + + # Find out the id of the torrent + id=$(transmission-remote -l | grep "${url##*/}") + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" # Once should be enough but somehow it's not so I do + # it loads of times. + id="${id%% *}" + + # If there is an error there will be an asterix displayed on the + # end of the id. We must remove it. + id="${id%\*}" + + if ! [[ ${id} =~ [[:digit:]] ]] + then + echo 'Error.' + exit 1 + fi + + # Display a progress bar until it's finished and then hardlink it to + # the right place. + progress='0%' + + echo -n "Pacman2pacman download: ${pname}: ${progress}" + + until [[ "${progress}" == '100%' ]] + do + progress=$(transmission-remote -t "${id}" -i | grep 'Percent Done:') + progress="${progress##* }" # Remove stuff we don't want + + printf "\rPacman2pacman download: %s: %s " "${pname}" "${progress}" + + sleep 0.7 + done + + echo + + ln "/srv/pacman2pacman/downloads/${url##*/}" "${filename}" || exit 1 + +else + # There's no .torrent so we download it by just HTTP. + + # We must do some complicated stuff to customise the progress + # meter how we want it, to make it consistent with the progress + # bar when we download stuff with transmission. + + progress='0%' + + echo -n "Pacman2pacman download: ${pname}: ${progress}" + + # The first loop gets rid of all the backspace and `#'es and + # stuff. The second loop isolates the percent done number. + curl -# -f -L -o "${filename}" -A 'Pacman2pacman' "${url}" 2>&1 | + while read -N 1 char + do + [[ "${char}" =~ [[:digit:].%\ ] ]] && echo -n "${char}" + done | + while read -d '%' word + do + progress="${word}%" + + # For consistency with the transmission progress bar: + [[ "${progress}" == '100.0%' ]] && progress='100%' + + printf "\rPacman2pacman download: %s: %s " "${pname}" "${progress}" + done + + echo + + [[ -f "${filename}" ]] || exit 1 +fi + +exit |