#!/bin/sh

# build_sdl2.rpi3.sh
# 2019-04-24
# by Gernot Walzl

# Downloads, builds and installs SDL2 on Raspbian.
# This build script enables hardware-accelerated graphics rendering.

# If libEGL.so or libGLESv2.so is missing,
# run rpi-update and reboot.

# After installation, the used video driver can be selected by
# setting the environment variable:
# export SDL_VIDEODRIVER=rpi
# Available video drivers: dummy rpi x11 opengl opengl_es1 opengl_es2 vulkan

VERSION="2.0.8"
SOURCE="SDL2-$VERSION.tar.gz"
DOWNLOAD="https://www.libsdl.org/release/$SOURCE"
MD5SUM="3800d705cef742c6a634f202c37f263f"
DEPENDS="build-essential autoconf libudev-dev"

# exit on error
set -e

# https://packages.debian.org/stretch/libsdl2-dev
DEPENDS="$DEPENDS
$(LANG=C apt-cache depends libsdl2-dev | sed -n 's/.*Depends: //p')"

# remove default SDL2 package
apt-get remove libsdl2-dev

# install dependencies
apt-get install $DEPENDS

# download
if [ ! -f "$SOURCE" ]; then
  wget -O "$SOURCE" "$DOWNLOAD"
fi

# check for correct source file
if [ "$(md5sum $SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi

# extract
tar xvf "$SOURCE"
cd "SDL2-$VERSION"
chown -R root:root .

# build
./autogen.sh
./configure
make

# install to /usr/local
make install