TOOLS/binary_codecs.sh: avoid creating fake "bestsites"
[mplayer/glamo.git] / TOOLS / binary_codecs.sh
blob1335cbfdd9dcb6f51c4a9d7f7d28b42d3b9d6890
1 #!/bin/sh
2 set -e
4 # avoid insecure tempfile creation
5 umask 0022
7 # This script will download binary codecs for MPlayer unto a Debian system.
9 # Author: thuglife, mennucc1
12 CODECDIR=/usr/lib/codecs
13 PREFDIR=/var/lib/mplayer/prefs
14 MYSITE='http://people.debian.org/~mennucc1/mplayer'
16 dpkgarch=$(dpkg --print-architecture)
18 [ -d $PREFDIR ] || mkdir -v $PREFDIR
19 [ -d $CODECDIR ] || mkdir -v $CODECDIR
20 cd $CODECDIR
21 [ -d mplayer_binary_codecs ] || mkdir -v mplayer_binary_codecs
24 choosemirror ()
26 cd $PREFDIR
28 #if [ ! -r mirrors ] || find mirrors -mtime +20 ; then
29 echo "Downloading mirrors list"
30 wget -nv -c -N $MYSITE/mirrors || true
31 #fi
32 if [ ! -r bestsites ] || [ mirrors -nt bestsites ] || \
33 find bestsites -mtime +20 | grep -q bestsites ; then
34 if which netselect > /dev/null ; then
35 echo Choosing best mirrors using netselect
36 netselect -s 5 -t 5 $( cat mirrors ) | awk '{print $2}' > bestsites
37 elif which fping > /dev/null ; then
38 fping -C 1 $( sed 's#.*//##;s#/.*##' mirrors ) 2>&1 | \
39 egrep -v 'bytes.*loss' | sort -n -k3 | \
40 grep -v ': *-' | awk '/:/{print $1}' | head -5 | ( while read mainsite ; do
41 grep $mainsite $PREFDIR/mirrors ; done ) > bestsites
42 else
43 echo "(If you install 'netselect' or 'fping', it will select the best mirror for you"
44 echo " you may wish to stop this script and rerun after installation)"
45 sleep 3
52 INSTALL () {
53 filename="$3"
54 dir="$2"
55 url="$1"
57 cd $CODECDIR/mplayer_binary_codecs
59 if [ -r $filename ] ; then
60 cp $filename $filename.bak
63 if [ "$url" = @MAINSITE@ ] ; then
64 list=$PREFDIR/bestsites
65 test -r $list || list=$PREFDIR/mirrors
66 cat $list | while read mainsite ; do
67 echo Downloading $filename from $mainsite ...
68 wget -c -N $mainsite/$dir/$filename || true
69 if [ -r "$filename" ] ; then
70 UNPACK "$filename"
71 return 0
73 done
74 else
75 wget -c -N $url/$dir/$filename || true
76 if [ -r "$filename" ] ; then
77 UNPACK "$filename"
78 return 0
86 UNPACK ()
88 filename="$1"
89 if [ -r $filename.bak ] && cmp $filename.bak $filename && [ -r $filename.list ] ; then
90 echo It appears that $filename was already succesfully installed
91 [ -r $filename.bak ] && rm $filename.bak
92 else
93 if grep -q " $filename$" $PREFDIR/MD5SUMS ; then
94 echo Checking MD5 for $filename
95 grep " $filename$" $PREFDIR/MD5SUMS | md5sum -c -
96 else
97 echo Warning: no MD5 for $filename were found. Hit enter to continue.
98 read dummy
100 echo Installing $filename ...
101 if [ -r $filename.list ] ; then
102 tr '\n' '\000' < $filename.list | xargs -r0 rm || true
103 UNLINK $filename.list
104 rm $filename.list
107 tarfail () { echo FAILED $filename ; rm $filename.list ; exit 1 ; }
109 case "$filename" in
110 *.tar.gz)
111 tar xvzf $filename > $filename.list || tarfail
112 #rm $filename
114 *.tgz)
115 tar xvzf $filename > $filename.list || tarfail
116 #rm $filename
118 *.tar.bz2)
119 tar --bzip2 -xvf $filename > $filename.list || tarfail
120 #rm $filename
122 esac
123 [ -r $filename.bak ] && rm $filename.bak
124 LINK $filename.list
125 echo "Installed $filename Succesfully!"
129 LINK () {
130 cd $CODECDIR/
131 cat $CODECDIR/mplayer_binary_codecs/$1 | while read f ; do
132 ln -sbf mplayer_binary_codecs/"$f" .
133 done
136 UNLINK () {
137 ### FIXME
138 # cd $CODECDIR
139 # cat $CODECDIR/mplayer_binary_codecs/$1 | while f do
140 # ln -sbf mplayer_binary_codecs/"$f"
141 # done
142 if which symlinks > /dev/null ; then
143 symlinks -d $CODECDIR
147 if [ `whoami` != root ]; then
148 echo "You must be 'root' to use this script. Login as root first!"
149 exit 1
152 case "$1" in
153 install)
154 if test -x /bin/bzip2 || test -x /usr/bin/bzip2 ; then : ; else
155 echo You need to install bzip2
156 exit 1
158 choosemirror
159 cd $PREFDIR
160 #if [ ! -r codecs_list ] || find codecs_list -mtime +20 ; then
161 echo "Getting codecs list"
162 wget -nv -c -N $MYSITE/codecs_list || true
165 cd $PREFDIR
166 echo Downloading MD5 sums from main site
167 [ -r MD5SUMS ] && mv MD5SUMS MD5SUMS.bak
168 if wget -nv -N http://www.mplayerhq.hu/MPlayer/releases/codecs/MD5SUMS ; then
169 [ -r MD5SUMS.bak ] && rm MD5SUMS.bak
170 else
171 echo "failed"
172 if [ -r MD5SUMS.bak ] ; then
173 echo "trying to use backup"
174 mv MD5SUMS.bak MD5SUMS
178 if grep -q "^$dpkgarch" $PREFDIR/codecs_list ; then
179 egrep -v "^[[:space:]]*(#|$)" $PREFDIR/codecs_list | \
180 while read arch url dir file info ; do
181 if [ "$dpkgarch" = "$arch" ]; then
182 echo Downloading and installing $file $info...
183 INSTALL "$url" "$dir" "$file"
185 done
186 needlibstd=no
187 test "$dpkgarch" = "powerpc" && needlibstd=yes
188 test "$dpkgarch" = "i386" && needlibstd=yes
189 if test "$needlibstd" = "yes" && ! test -r /usr/lib/libstdc++.so.5 ; then
190 echo "Warning: you need to install libstdc++ 5 libraries"
191 echo -n "Do it now? "
192 read R
193 case $R in
194 y*) apt-get install libstdc++5 ;;
195 *) echo "If you change your mind, use the command"
196 echo " apt-get install libstdc++5" ;;
197 esac
199 else
200 echo "Sorry, no codecs for your arch '$dpkgarch'. Sorry dude :("
201 exit 1
205 uninstall)
206 cd $CODECDIR/
207 rm -rf mplayer_binary_codecs
208 #FIXME we need a better clean system
209 if which symlinks > /dev/null ; then
210 symlinks -d .
211 else
212 echo "please install the package 'symlinks' and run 'symlinks -d $CODECDIR' "
214 echo "Uninstalled Succesfully!"
218 echo "Usage: {install|uninstall}"
219 echo "This program will install binary codecs for MPlayer."
220 exit 1
223 esac
226 exit 0