%define win32dir %{_libdir}/win32

%define alt_url1 ftp://ftp1.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url2 ftp://ftp2.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url3 ftp://ftp3.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url4 ftp://ftp4.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url5 ftp://ftp5.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url6 ftp://ftp6.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url7 ftp://ftp7.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url8 ftp://ftp8.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url9 ftp://ftp9.mplayerhq.hu/MPlayer/releases/codecs/
%define alt_url10 ftp://ftp10.mplayerhq.hu/MPlayer/releases/codecs/

Name:          win32codecs
Version:       @SRPMVERSION@
Release:       @SRPMRELEASE@
Summary:       Win32 native dll codecs
Group:         System/Multimedia
Vendor:        @vendor@
Distribution:  @distribution@
Packager:      Silvan Calarco <silvan.calarco@mambasoft.it>
URL:           ftp://www.mplayerhq.hu/MPlayer/releases/codecs/
License:       Non Distributable
Source:        LICENSE
BuildPrereq:   /usr/bin/curl
BuildPrereq:   /usr/bin/tempfile
Provides:      win32_codecs
BuildRoot:     %{_tmppath}/%{name}-%{version}-root

%description
These are binary multimedia codecs for use with Unix players such as MPlayer,Xine, and avifile.
They are useless for normal Windows players as they only contain the DLLs without installer and other fancy stuff needed to use them with common Windows players.

%prep

%setup -q -D -T -c
[ -r /etc/sysconfig/@sysconfigfile@ ] && . /etc/sysconfig/@sysconfigfile@

# best effort in W32CODS_URL is undefined
[ "$W32CODS_URL" ] || W32CODS_URL="\
%{alt_url1} %{alt_url2} %{alt_url3} %{alt_url4} %{alt_url5} \
%{alt_url6} %{alt_url7} %{alt_url8} %{alt_url9} %{alt_url10}"

#tmpfile=`/usr/bin/tempfile 2>/dev/null` || exit 1
tmpfile=urlfiles.lst

# get the file list available at %{URL}...
for URL in $W32CODS_URL; do
   curl -s -f -L --connect-timeout 10 --retry 2 \
      --url $URL --list-only -o $tmpfile 2>/dev/null && break
done

[ -s $tmpfile ] || { rm -f $tmpfile; exit 1; }

sed -i 's/\x0D$//' $tmpfile
# ...download the most recent tarball `all-[0-9]{8}.tar.bz2'

if cat $tmpfile | grep -Eq '<A HREF="'; then
   # download via http (connections trough a proxy server?)
   tarball=`sort -r $tmpfile | \
      grep -E -m1 'all-[0-9]{8}.tar.bz2' | \
      sed 's|^<A HREF="||;s|".*||' 2>/dev/null`
else
   tarball=`sort -r $tmpfile | \
      grep -E -m1 'all-[0-9]{8}.tar.bz2' 2>/dev/null`
fi

rm -f $tmpfile

[ "$tarball" ] || exit 1

## check if the latest version is already installed
#if [ -r %{win32dir}/VERSION_w32codecs ]; then
#   currver="$(cat %{win32dir}/VERSION_w32codecs)"
#   case "$tarball" in
#   "all-${currver}.tar.bz2")
#      echo "the latest version is already installed"
#      exit 1 ;;
#   esac
#fi

curl --url $URL/$tarball -o $tarball || exit 1
echo $tarball | sed 's/all-\([0-9]*\).tar.*/\1/' > VERSION_w32codecs

tar xf $tarball --strip-components=1 && rm -f $tarball

%build

%install
[ "%{buildroot}" != / ] && rm -rf %{buildroot}
find . -type f -exec install -D "{}" "%{buildroot}%{win32dir}/{}" \;

%clean
[ "%{buildroot}" != / ] && rm -rf %{buildroot}

%files
%defattr(-,root,root)
%{win32dir}/*

%changelog
* Thu Mar 10 2011 Silvan Calarco <silvan.calarco@mambasoft.it> 0.9.3-1mamba
- fixed installation of codecs containing spaces in name

* Tue Oct 16 2007 Silvan Calarco <silvan.calarco@mambasoft.it> 0.9.2-1mamba
- update release to avoid permission problems now this script is run by user

* Fri Feb 02 2007 Davide Madrisan <davide.madrisan@qilinux.it> 0.9.1-5qilnx
- get download parameters from a sysconfig file
- added support for connections trough a proxy server

* Mon Nov 28 2005 Massimo Pintore <massimo.pintore@qilinux.it> 0.9.1-4qilnx
- added alternative repositories

* Mon Aug 29 2005 Silvan Calarco <silvan.calarco@qilinux.it> 0.9.1-3qilnx
- download from alternative repositories

* Wed Apr 06 2005 Silvan Calarco <silvan.calarco@qilinux.it> 0.9.1-2qilnx
- added LICENSE file

* Thu Mar 10 2005 Davide Madrisan <davide.madrisan@qilinux.it> 0.9.1-1qilnx
- update to version 0.9.1 by autospec
- check the current version of win32 codecs installed before download
- removed the source entry in the specfiles preamble to permit the srpm
  rebuilding

* Mon Feb 21 2005 Davide Madrisan <davide.madrisan@qilinux.it> 0.9-1qilnx
- package creation
