#!/bin/sh

##
# Install lighttpd and dependencies smoothly on Mac OS X.
#
# USE AT YOUR OWN RISK.
#
# AUTHOR: Arne Brodowski http://www.arnebrodowski.de
# 

export PATH=$PATH:/usr/local/bin
PREFIX=/usr/local

mkdir src
cd src

# Install pcre dependency
curl -O ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.1.tar.bz2
tar xfj pcre-7.1.tar.bz2
cd pcre-7.1
./configure --prefix=${PREFIX} CFLAGS=-O1 && make
sudo make install
cd ..

# Install lighttpd
curl -O http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2
tar xfj lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure --prefix=${PREFIX} --with-openssl --with-pcre=${PREFIX} && make
sudo make install
sudo mkdir /usr/local/etc
cd ..

# pre-create log files
sudo touch /var/log/lighttpd.access.log
sudo touch /var/log/lighttpd.error.log
sudo chown nobody /var/log/lighttpd.*

#change some default values in the config file
curl -O http://public.rcs4u.de/_arne/files/lighttpd.conf.patch
cp lighttpd-1.4.18/doc/lighttpd.conf .
patch -p1 < lighttpd.conf.patch
USERN=`whoami`
sed -e "s/\/Users\/USER\/Sites/\/Users\/$USERN\/Sites/" lighttpd.conf > lighttpd.conf.new
mv lighttpd.conf.new lighttpd.conf

sudo cp lighttpd.conf /usr/local/etc/
cd ..


echo "Installation complete. Please edit /usr/local/etc/lighttpd.conf to suite your needs."
