#!/bin/bash
# author: Sebastian Blumenthal
# date: 16.09.2009
#
# This scripts automatically downloads OpenCV version 1.1.0 and configures it to run with ffmpeg,
# compiles and installs it. The problem with ffmpeg is that OpenCV doesn't find the header files
# although they are installed. This can be easily resolved by creating proper symbolic links to
# the existing header files.
#
# Source code is planted at /usr/local/src/opencv-1.1.0
# After installation OpenCV headersare available in /usr/local/include/opencv and libraries are in
# /usr/local/lib/
# Feel free to change the script to your own needs. 
#
# Usage: Just run this script (without any parameters): sudo ./install_OpenCV_with_ffmpeg.sh
#
# NOTE: Tested with UBUNTU 9.04 + gcc 4.3
# ---- 

echo "Starting to install OpenCV library (version 1.1.0) with ffmpeg."

# automatically download OpenCV library (might be skipped if lib is already downloaded)
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/1.1pre1/opencv-1.1pre1.tar.gz?use_mirror=surfnet
 
# uninstall files from repository
sudo apt-get remove --purge libcv-dev libcv1 libcvaux-dev libcvaux1 libhighgui-dev libhighgui1 opencv-doc python-opencv 

# install prerequisites (might not be complete!)
sudo apt-get install libgtk2.0-dev libavcodec-dev ffmpeg libavformat-dev libswscale-dev libavutil-dev libjpeg62-dev libtiff4-dev 

# copy src
sudo cp opencv-1.1pre1.tar.gz /usr/local/src/ /usr/local/src/
cd /usr/local/src/
sudo tar -xvf opencv-1.1pre1.tar.gz
cd opencv-1.1.0

# do some hacks, that OpenCV finds ffmpeg headers 
if [ -r /usr/local/include/ffmpeg ];	
then
	echo "ffmpeg backward comatibility headers found"
else
	echo "ffmpeg backward comatibility headers not found: creating symbolic links"
	sudo mkdir /usr/local/include/ffmpeg

	#avcodec.h
	if [ -r /usr/include/libavcodec/avcodec.h ];
	then
		sudo ln -s /usr/include/libavcodec/avcodec.h /usr/local/include/ffmpeg/avcodec.h 
	else
		echo "ffmpeg not properly installed. aborting!"
	fi

	#avformat.h
	if [ -r /usr/include/libavformat/avformat.h ];
	then
		sudo ln -s /usr/include/libavformat/avformat.h /usr/local/include/ffmpeg/avformat.h 
	else
		echo "ffmpeg not properly installed. aborting!"
	fi

	#avio.h
	if [ -r /usr/include/libavformat/avio.h ];
	then
		sudo ln -s /usr/include/libavformat/avio.h /usr/local/include/ffmpeg/avio.h 
	else
		echo "ffmpeg not properly installed. aborting!"
	fi

	#avutil.h
	if [ -r /usr/include/libavutil/avutil.h ];
	then
		sudo ln -s /usr/include/libavutil/avutil.h /usr/local/include/ffmpeg/avutil.h 
	else
		echo "ffmpeg not properly installed. aborting!"
	fi

	#swscale.h
	if [ -r /usr/include/libswscale/swscale.h ];
	then
		sudo ln -s /usr/include/libswscale/swscale.h /usr/local/include/ffmpeg/swscale.h 
	else
		echo "ffmpeg not properly installed. aborting!"
	fi

	echo "Here are the new backward comatibility headers (should be five):"
	ls -l /usr/local/include/ffmpeg
	echo ""
fi

# compile OpenCV code
./configure --with-ffmpeg --without-quicktime --enable-shared
sudo make -j
sudo make install

echo "Done."

