summaryrefslogtreecommitdiff
path: root/cron-jobs/make_repo_torrents
blob: 767e182a90825799a7ac8a28cf57796a40be81f3 (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
#!/bin/bash
# Copyright (C) 2014, 2017 Joseph Graham <joseph@xylon.me.uk>
#
# 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 finds any updated packages and calls
# `make_indivudual_torrent' for each of them. Run every hour from cron.

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.
script_directory="$(dirname "$(readlink -e "$0")")/.."
. "$(dirname "$(readlink -e "$0")")/../config"
public_location="$FTP_BASE/"
torrent_location="$FTP_BASE/torrents/"

cd "${torrent_location}"

pkgfilelist=$(mktemp)

# Find any directories that might have packages in them
find "${public_location}" -name 'os' -type 'd' |
    while read dir
    do
        # Find any packages
        find "${dir}" -regex '[^ ]+\.pkg\.tar\.xz'
    done > "${pkgfilelist}"

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 a .torrent file does not already exist for this package, we call
    # `make_individual_torrent' to make it.
    if ! [[ -f "${torrent_location}${pkg_name}.torrent" ]]
    then
        "$script_directory/make_individual_torrent" "${pkg}" "${public_location}"
    fi
done < "${pkgfilelist}"

# For torrents older than 1 year, we check if it's package still
# exists, else clean it up. This shouldn't be done every hour, but it
# is good for it to be in this cronjob so it can re-use the
# $pkgfilelist we made earlier. So we do it with a 1-in-30
# probability.
if ! (( $(shuf -e {0..29} | head -1) ))
then
    find -H "${torrent_location}" -mtime +365 -name '*.torrent' -type f |
        while read oldtorrent
        do
            oldtorrentnm="${oldtorrent##*/}"
            correspackagenm="${oldtorrentnm%.torrent}"
        
            grep "${correspackagenm}" "${pkgfilelist}" &> /dev/null || rm "${oldtorrent}"
        done
fi

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

rm -f "${pkgfilelist}"