#!/bin/bash
# chris langreiter, andreas bolka; 2008-10-08 / 2009-01-05

# 0.) abort if any error occurs

set -e

# 1.) remove all vanilla-related stuff from /Library/WebServer/Documents
#     and /Library/WebServer/CGI-Executables 
#     remove vanilla installation in /usr/local/vanilla-0.6.2

sudo rm -rf /Library/WebServer/Documents/vanilla-static
sudo rm -f /Library/WebServer/CGI-Executables/vanilla*
sudo rm -rf /usr/local/vanilla-0.6.2

# 2.) create target directory and a working directory to store 
#     temp files for your install:

sudo mkdir /usr/local/vanilla-0.6.2
sudo chmod -R a+rw /usr/local/vanilla-0.6.2

cd
rm -rf vanilla-install/
mkdir vanilla-install
cd vanilla-install

# 3.) download and install rebol:

curl -O http://www.rebol.com/downloads/core024.tar.gz
tar -xzf core024.tar.gz
sudo mv core024 /usr/local/vanilla-0.6.2/rebol

# 4.) download and prepare vanilla:

curl -O http://www.vanillasite.at/dist/vanilla-0.6.2.tar.gz
tar -xzf vanilla-0.6.2.tar.gz
cd vanilla
sh chmod.sh
chmod a+x res/
chmod a+x res/attachments/
chmod -R a+rw res/
cd ..

# 5.) install and adapt vanilla's executable:

VANILLA_REBOL="\\/usr\\/local\\/vanilla-0.6.2\\/rebol\\/rebol"
sudo cat vanilla/wrapper.cgi \
    | sed s/@@path-to-rebol@@/$VANILLA_REBOL/ \
    > /Library/WebServer/CGI-Executables/vanilla.cgi

chmod a+x /Library/WebServer/CGI-Executables/vanilla.cgi
rm vanilla/wrapper.cgi

# 6.) install vanilla's config file and adapt vanilla-root-path and
#     the resource-url:

VANILLA_ROOT="\\/usr\\/local\\/vanilla-0.6.2\\/vanilla\\/"
sudo cat vanilla/vanilla.r.conf \
    | sed s/@@vanilla-root-path@@/$VANILLA_ROOT/ \
    | sed s/\\/resources\\//\\/vanilla-static\\// \
    > /Library/WebServer/CGI-Executables/vanilla.cgi.conf

rm vanilla/vanilla.r.conf

# 7.) install the rest of vanilla:

sudo mv vanilla/res /Library/WebServer/Documents/vanilla-static
ln -s /Library/WebServer/Documents/vanilla-static vanilla/res 
mv vanilla /usr/local/vanilla-0.6.2/vanilla

# 8.) remove the working directory:

cd
rm -rf vanilla-install/
