summaryrefslogtreecommitdiff
path: root/cron-jobs/make_repo_torrents
blob: dffd0dc2cc88db64ca9c63149c6d1310e89fce72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /bin/bash
# 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/>.

# This script depends on `mktorrent'

username=$( id -un )

case "${username}" in
    repo | root )
        true
        ;;
    * )
        echo "This script must be run as repo user or root user."
        echo "ByeBye!"
        exit 1
        ;;
esac

# pacman doesn't support multiple different packages of the same name,
# so it's OK to just stuff all the torrents into a single directory.
torrent_location='/srv/http/repo/public/torrents/'
public_location='/srv/http/repo/public/'

# Tracker announce URL
tracker='http://t67.eu:6969/announce'  # t67.eu is run by Xylon

# All mirrors go here
declare -a array=('http://repo.parabolagnulinux.org/' 'https://parabola.goodgnus.com.ar/' 'http://mirror.yandex.ru/mirrors/parabola/' 'http://alfplayer.com/parabola/')

# I got this function from http://mywiki.wooledge.org/BashFAQ/026 . It
# shuffles an array. Uses a global array variable. Must be compact
# (not a sparse array). The array must be called `array'.
shuffle() {
   local i tmp size max rand

   # $RANDOM % (i+1) is biased because of the limited range of $RANDOM
   # Compensate by using a range which is a multiple of the array size.
   size=${#array[*]}
   max=$(( 32768 / size * size ))

   for ((i=size-1; i>0; i--)); do
      while (( (rand=$RANDOM) >= max )); do :; done
      rand=$(( rand % (i+1) ))
      tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
   done
}

cd "${torrent_location}"

find "${public_location}" -name 'os' -type 'd' |
while read dir
do
    find "${dir}" -name '*\.pkg\.tar\.xz' |
    while read pkg
    do
	pkg_name="${pkg##*/}"

        if [[ -h "${pkg}" ]]  # check if it's a symbolic link
        then
            # We get the target of the symlink
            pkg=$( readlink -f "${pkg}" )
        fi

	if ! [[ -f "${torrent_location}${pkg_name}.torrent" ]]
	then
            # We need to make a comma seperated list of webseeds (this is passed
            # as a single argument to mktorrent)
            webseeds=''

            # Randomize the order of the list of webseeds because I
            # don't know if transmission might always use the one at
            # the top otherwize.
            shuffle

            for prefix in "${array[@]}"
            do
                webseeds+="${prefix}${pkg#${public_location}},"
            done

            # There should not be a random comma at the end of the webseeds
            webseeds="${webseeds%,}"

	    mktorrent -a "${tracker}" "${pkg}" -w "${webseeds}"  # "${torrent_location}"
	fi
    done
done

if [[ "${username}" == root ]]
then
    chown repo *
fi