-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathmirror-meta.sh
executable file
·157 lines (138 loc) · 4.72 KB
/
mirror-meta.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# Goes through the mirror files and generates
# meta files like torrents for files >2MB
# in the subdirectory 'meta'
# depends on ctorrent (debian package)
# set -e
if [ ! -n "$1" ]; then
echo '$1 variable not set, must be the directory where to index, e.g. ~/files'
exit 1
fi
# *ONLY* relative to itself for this torrent.helper file path!
cd `dirname "$0"`
TORRENT_HELPER=`readlink -f ../www/torrent.helper`
FILES=`readlink -f $1`
echo "FILES: $FILES"
if [ ! -d $FILES ] ; then
echo files/ does not exist
exit 1
fi
# from now on we switch to the target $FILES directory
cd $FILES
FINDCMD='find . -type f -size +2M -follow -not -name "*spkg*" -not -name "*zsync" | grep -v -e "\./doc/.*"'
function metatorrent () {
OUTPUT=$1
# $2 is the title
ENDING=$3
# collect all metalinks in one file
[[ -e $OUTPUT ]] && rm -f $OUTPUT
touch $OUTPUT
# intro
for line in `cat index.html`; do
if [[ "$line" =~ "TOK1" ]] ; then
break
fi
echo "$line" >> $OUTPUT
done
# write content
echo "<h1>$2 for <a href='https://www.sagemath.org/'>SageMath</a></h1>" >> $OUTPUT
echo '<div><strong>Read more <a href="https://doc.sagemath.org/html/en/installation/index.html">in the Download Guide</a></strong></div>' >> $OUTPUT
echo '<div><table>' >> $OUTPUT
for f in `eval $FINDCMD`; do
f=${f:2}
METADIR=`dirname $f`/meta
FILE=`basename $f`
METAFILE=$METADIR/`basename $f`.$ENDING
echo '<tr><td>'${METADIR%/meta}'</td><td>' >> $OUTPUT
echo '<a onClick="pageTracker._trackEvent('MirrorDL', '`dirname $f`', '`basename $f`.$ENDING', 1)"' >> $OUTPUT
echo ' href="./'$METAFILE'">'$FILE'</a></td></tr>' >> $OUTPUT
done
echo '</table></div>' >> $OUTPUT
# end of index file
# outro
writeout=False
for line in `cat index.html`; do
if [[ "$line" =~ "TOK2" ]] ; then
writeout=True
fi
if [[ "$writeout" = True ]]; then
echo "$line" >> $OUTPUT
fi
done
# publish it
echo published $OUTPUT file to `dirname "./$OUTPUT"`
echo cp -f $OUTPUT ./$OUTPUT
}
# metatorrent metalinks.html Metalinks metalink
metatorrent torrents.html BitTorrents torrent
######################
# comment in torrent file
COMMENT="Sage Mathematical Software System: http://www.sagemath.org/"
# now the 'real' work
ROOT=`pwd`
ROOTLEN=`expr ${#ROOT} + 1`
# searches for all files, which are above a certain size, and also which are not
# spkges or part of the documentation
for f in `eval $FINDCMD`; do
# echo $f
f=${f:2} #removes find's ./ in the beginning
DIR=`dirname $f`
METADIR=`dirname $f`/meta
FILE=`basename $f`
TORFILE=$METADIR/`basename $f`.torrent
if [ ! -d $METADIR ]; then
mkdir $METADIR
fi
# -s tests, if file exists and has size >0
if [ ! -s $TORFILE ]
then
pushd $METADIR
cd ..
#ctorrent -t -u "udp://tracker.openbittorrent.com:80" -l 1025000 -s meta/$FILE.torrent $FILE
P=`pwd`
RELDIR=${P:$ROOTLEN}
echo $RELDIR
WEBSEEDS=""
while read line; do
# TODO: the trailing $FILE is just for µtorrent. all others work well with the trailing / !!!
WEBSEEDS=$WEBSEEDS,${line}${RELDIR}/$FILE
done < $TORRENT_HELPER
# substring deletes, if matched, see http://tldp.org/LDP/abs/html/string-manipulation.html
WEBSEEDS=${WEBSEEDS#,} # delete , at the start
WEBSEEDS=${WEBSEEDS%,} # delete , at the end
mktorrent -v \
-a "udp://tracker.opentrackr.org:1337/announce" \
-a "udp://tracker.coppersurfer.tk:6969/announce" \
-l 20 -c "$COMMENT" -w $WEBSEEDS \
-o meta/$FILE.torrent $FILE
popd
fi
#if [ ! -s $METADIR/$FILE.metalink ]
#then
# ~/bin/metalink --alldigests $f < ../metalink.helper > $METADIR/$FILE.metalink
# # insert link to torrent file
# # finds first line with <url> in xml, inserts a torrent file, then copies back the tmp file
# # fixes <file name="path .. filename"> tag by removing the subdir
# insert='<url type="bittorrent" preference="100">http://sagemath.org/mirror/'$TORFILE'</url>'
# first_url=True
# first_name=True
# while read line; do
# if [[ $first_name == True && "$line" =~ "<file name=" ]] ; then
# # assuming this string:
# # <file name="linux/32bit/sage-4.1.1-linux-Fedora_release_10_Cambridge-i686-Linux.tar.gz">
# # wrong
# # t=$(( ${#DIR} + 13 ))
# #echo ${line:0:12}${line:$t}
# echo '<file name="'$FILE'">'
# first_name=False
# else
# echo "$line"
# fi
# if [[ $first_url == True && "$line" =~ "<url" ]] ; then
# echo $insert
# first_url=False
# fi
# done < $METADIR/$FILE.metalink > $METADIR/tmp
# mv -f $METADIR/tmp $METADIR/$FILE.metalink
#fi
done