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