#!/bin/sh
#
# This script will automates the steps used to producing a static win32
# package of SoX.
#
# It requires cygwin with gcc-4 and zip packages installed.  Also, it 
# requires to already have several external libraries already installed
# under /usr/local.
#
# And last, it will optional package a wget compiled with VC++ instead
# of cygwin version (which has no DLL's to distribute) if found
# in wget-1.11.4 directory.  If not found, it will distribute the
# cygwin version and various DLL's it requires.
#
# Various notes:
#
# Script makes use of "-static" option to tell compiler to prefer static
# external libraries. Some libtool versions (2.2.7a?) will cause
# linker to ignore -static if a lib*.la is found and it defines
# a dynamic version of the library.  Work arounds include to only
# install static libraries, delete the lib*.la file, or edit the
# lib*.la file and set dlnames and library_names variables to empty
# string ("").
#
# The following command lines were used to generate the static external
# libraries SoX ships with.
#
# cd libpng-1.2.32
#./configure --disable-shared --enable-static;make;make install
#
# cd ../wavpack-4.50.1
#./configure --disable-shared --enable-static;make;make install
#
# cd ../flac-1.2.1
#./configure --disable-shared --enable-static;make;make install
#
# cd ../libogg-1.1.3
#./configure --disable-shared --enable-static;make;make install
#
# cd ../libvorbis-1.2.0
#./configure --disable-shared --enable-static;make;make install
#
# cd ../libsndfile-1.0.19
#./configure --disable-shared --enable-static;make;make install
#
# To get MP3 header files used to enable MP3 support (no libraries used):
#
# cd ../lame-398-2
#./configure --enable-shared --disable-static;make;sudo make install
#
# cd ../libmad
#./configure --enable-shared --disable-static;make;sudo make install

[ ! -x configure ] && autoreconf -i

# Some versions of autoconf (2.63?) seem to get easily confused about
# CPP variable. If you see warning messages about header files
# rejected by preprocessor then its most likely from that.
# Force the value of CPP=cpp works around that bug.
if [ $# -ne 0 -o ! -r Makefile ]; then
  ./configure \
    --disable-shared \
    --with-libltdl \
    --enable-dl-lame --enable-dl-mad \
    CC=gcc-4 CPP=cpp\
    CPPFLAGS=-I/usr/local/include \
    LDFLAGS="-static -L/usr/local/lib" \
    $*
fi

make -s all pdf txt || exit 1

dir=sox-`grep Version: sox.pc | cut -d ' ' -f 2`
rm -rf $dir $dir-win32.zip
mkdir -p $dir

for f in ChangeLog LICENSE.GPL README README.win32; do
  cp -p $f $dir/$f.txt
  unix2dos $dir/$f.txt
done

binaries=src/sox

[ ! -r "../wget-1.11.4/wget.exe" -a -r /usr/bin/wget ] && binaries+=" /usr/bin/wget"
 
binaries=$(for f in `cygcheck $binaries|dos2unix`
    do cygpath -u $f; done|sort|uniq|grep -v ^/cygdrive/)

cp -p \
  $binaries \
  sox.pdf \
  soxformat.pdf \
  soxi.pdf \
  libsox.pdf \
  scripts/batch-example.bat \
  $dir

unix2dos $dir/batch-example.bat

if test -r "../wget-1.11.4/wget.exe"; then
  cp ../wget-1.11.4/wget.ini $dir
else
  if test -r /usr/bin/wget -a -r /etc/wgetrc; then
    cp -p /etc/wgetrc $dir/wget.ini
    chmod +r $dir/wget.ini
  fi
fi

zip -r $dir-win32.zip $dir
rm -rf $dir
