#!/bin/bash

downloads="$HOME/software/downloads"

if [ ! -e /usr/include/tiffio.h ]; then
  echo "Error: could not find file tiffio.h. Please install libtiff4-dev."
  exit 1
fi

export LC_ALL=C

mkdir -p "${downloads}"

cd "$(dirname $0)/.."

if [ -e splash2 ]; then
  echo "Error: a directory with the name splash2 already exists. Please"
  echo "remove it before running this script, or run this script in another"
  echo "directory."
  exit 1
fi

(
  cd "$downloads"
  if [ ! -e splash2.tar.gz ]; then
    wget -q http://www-flash.stanford.edu/apps/SPLASH/splash2.tar.gz
  fi
  if [ ! -e splash2-modified.patch.gz ]; then
    wget -q http://www.capsl.udel.edu/splash/splash2-modified.patch.gz
  fi
)

tar zxf $downloads/splash2.tar.gz
cd splash2
gzip -cd < "$downloads/splash2-modified.patch.gz" | patch -p1 -s
patch -p1 -s <<'EOF'
diff -ru orig/splash2/codes/apps/radiosity/display.C splash2/codes/apps/radiosity/display.C
--- orig/splash2/codes/apps/radiosity/display.C	2008-05-25 10:38:52.000000000 +0200
+++ splash2/codes/apps/radiosity/display.C	2008-05-25 10:37:51.000000000 +0200
@@ -14,6 +14,7 @@
 /*                                                                       */
 /*************************************************************************/
 
+#include <math.h>
 #include <stdio.h>
 
 EXTERN_ENV;
--- orig/splash2/codes/apps/radiosity/Makefile	2008-05-25 10:38:52.000000000 +0200
+++ splash2/codes/apps/radiosity/Makefile	2008-05-25 13:05:36.000000000 +0200
@@ -15,7 +15,7 @@
 
 LOADLIBES = glibdumb/glib.a glibps/glibps.a
 
-$(TARGET): $(OBJS)
+$(TARGET): $(OBJS) $(LOADLIBES)
 	$(CC) $(OBJS) $(CFLAGS) $(LOADLIBES) -o $(TARGET) $(LDFLAGS)
 
 radiosity.h: patch.h model.h parallel.h task.h
@@ -29,3 +29,7 @@
 visible.c: visible.C $(HS)
 rad_tools.c: rad_tools.C $(HS)
 room_model.c: room_model.C $(HS)
+glibdumb/glib.a:
+	make -C glibdumb glib.a
+glibps/glibps.a:
+	make -C glibps glibps.a
diff -ru orig/splash2/codes/apps/raytrace/rltotiff/Makefile splash2/codes/apps/raytrace/rltotiff/Makefile
--- orig/splash2/codes/apps/raytrace/rltotiff/Makefile	2008-05-25 10:38:52.000000000 +0200
+++ splash2/codes/apps/raytrace/rltotiff/Makefile	2008-05-25 10:31:57.000000000 +0200
@@ -4,6 +4,3 @@
 include ../../../Makefile.config
 
 LDFLAGS := $(LDFLAGS) -ltiff
-
-clean:
-	rm -rf $(OBJS) $(TARGET)
EOF


patch -p1 -s <<EOF
diff -ru orig/splash2/codes/Makefile.config splash2/codes/Makefile.config
--- orig/splash2/codes/Makefile.config	2008-05-25 10:38:52.000000000 +0200
+++ splash2/codes/Makefile.config	2008-05-25 10:14:25.000000000 +0200
@@ -1,5 +1,5 @@
-CC := gcc
+CC := gcc
-CFLAGS := -O3 -pthread -D_POSIX_C_SOURCE=200112
+CFLAGS := -g3 -m32 -O3 -pthread -D_POSIX_C_SOURCE=200112
 #CFLAGS := -g3 -pthread -D_POSIX_C_SOURCE=200112
 CFLAGS := \$(CFLAGS) -Wall -W -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wdisabled-optimization
 CFLAGS := \$(CFLAGS) -Wpadded -Winline -Wpointer-arith -Wsign-compare -Wendif-labels
@@ -5,8 +5,8 @@
 CFLAGS := \$(CFLAGS) -Wpadded -Winline -Wpointer-arith -Wsign-compare -Wendif-labels
 LDFLAGS := -lm
 
-BASEDIR := \$(HOME)/splash2/codes
-MACROS := \$(BASEDIR)/null_macros/c.m4.null
+BASEDIR := ${PWD}/codes
+MACROS := \$(BASEDIR)/null_macros/c.m4.null.POSIX_BARRIER
 M4 := m4 -s -Ulen -Uindex
 
 x = *
--- orig/splash2/codes/apps/radiosity/elemman.C	2008-07-13 20:24:35.000000000 +0200
+++ splash2/codes/apps/radiosity/elemman.C	2008-07-13 20:45:15.000000000 +0200
@@ -24,6 +24,7 @@
 
 
 #include <stdio.h>
+#include "drd.h"
 
 EXTERN_ENV;
 
@@ -707,6 +708,12 @@ void process_rays(Element *e, long proce
     e->n_interactions = 0 ;
     UNLOCK(e->elem_lock->lock);
 
+    {
+      Interaction* p;
+      for (p = i_list; p; p = p->next)
+	DRD_IGNORE_VAR(*p);
+    }
+
     /* For each interaction, do BF-error-analysis */
     bf_error_analysis_list( e, i_list, process_id ) ;
 
--- orig/splash2/codes/apps/radiosity/rad_main.C	2008-07-13 20:24:35.000000000 +0200
+++ splash2/codes/apps/radiosity/rad_main.C	2008-07-13 20:50:01.000000000 +0200
@@ -30,6 +30,7 @@
 #include <panel.h>
 #endif
 #endif
+#include "drd.h"
 
 /* ANL macro initialization */
 
@@ -1084,6 +1085,18 @@ void init_global(long process_id)
     /* Initialize statistical info */
     init_stat_info(process_id) ;
 
+    {
+      int i;
+
+      DRD_IGNORE_VAR(global->pbar_count);
+      for (i = 0;
+	   i < sizeof(global->task_queue) / sizeof(global->task_queue[0]);
+	   i++)
+	{
+	  DRD_IGNORE_VAR(global->task_queue[i].n_tasks);
+	}
+      DRD_IGNORE_VAR(global->element_buf);
+    }
 }
 
 
--- orig/splash2/codes/apps/radiosity/Makefile	2008-07-13 20:24:35.000000000 +0200
+++ splash2/codes/apps/radiosity/Makefile	2008-07-13 20:09:27.000000000 +0200
@@ -6,7 +6,7 @@ HS = model.h parallel.h patch.h radiosit
 
 include ../../Makefile.config
 
-CFLAGS := \$(CFLAGS) -I./glibdumb -I./glibps
+CFLAGS := \$(CFLAGS) -I./glibdumb -I./glibps -I../../../../../drd -I../../../../../include
 
 #CCOPTS = -I -float -I/usr/include \$(CFLAGS)
 #LDFLAGS = -lm -lpthread
EOF

for m in $(find codes -name Makefile)
do
  echo "==== $m ===="
  make -s -C $(dirname $m)
done