]> git.decadent.org.uk Git - dak.git/blob - tools/link_morgue_with_snapshot_farm.sh
debianqueued: remove current directory from @INC
[dak.git] / tools / link_morgue_with_snapshot_farm.sh
1 #!/bin/bash
2 # No way I try to deal with a crippled sh just for POSIX foo.
3
4 # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # Homer: Are you saying you're never going to eat any animal again? What
20 #        about bacon?
21 # Lisa: No.
22 # Homer: Ham?
23 # Lisa: No.
24 # Homer: Pork chops?
25 # Lisa: Dad, those all come from the same animal.
26 # Homer: Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
27
28 # Let files inside morgue be symlinks to the snapshot farm
29
30 # exit on errors
31 set -e
32 # make sure to only use defined variables
33 set -u
34 # ERR traps should be inherited from functions too. (And command
35 # substitutions and subshells and whatnot, but for us the functions is
36 # the important part here)
37 set -E
38
39 # Make sure we start out with a sane umask setting
40 umask 022
41
42 # And use one locale, no matter what the caller has set
43 export LANG=C
44 export LC_ALL=C
45
46 # log something (basically echo it together with a timestamp)
47 # Set $PROGRAM to a string to have it added to the output.
48 function log () {
49         local prefix=${PROGRAM:-$0}
50         echo "$(date +"%b %d %H:%M:%S") $(hostname -s) ${prefix}[$$]: $@"
51 }
52
53 PROCESSDIR="/srv/morgue.debian.org"
54 FARMBASE="/srv/snapshot.debian.org/farm"
55
56 cd "${PROCESSDIR}"
57 log "Processing ${PROCESSDIR}"
58 find ${PROCESSDIR} -type f |
59 while read mfile; do
60     # Get the files sha1sum
61     mshasum=$(sha1sum ${mfile})
62     mshasum=${mshasum%% *}
63     # And now get the "levels" of the farm
64     if [[ ${mshasum} =~ ([0-9a-z][0-9a-z])([0-9a-z][0-9a-z]).* ]]; then
65         LVL1=${BASH_REMATCH[1]}
66         LVL2=${BASH_REMATCH[2]}
67     else
68         log "Ups, unknown error in regex for ${mfile} (${mshasum})"
69         continue
70     fi
71
72     # See if we have a target
73     if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
74         # Yes, lets symlink it
75         log "Symlinking ${mfile} to ${FARMBASE}/${LVL1}/${LVL2}/${mshasum}"
76         ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
77     else
78         # No, just tell
79         log "No symlink target for ${mfile}"
80     fi
81 done # mfile read mfile