#!/usr/bin/env bash

set -xue

if [[ "$OSTYPE" =~ "linux" ]]; then
    if which apt-get && which dpkg; then
        # Debian or Ubuntu
        apt-get --yes install \
            autoconf automake autopoint bash bison bzip2 flex gettext\
            git g++ gperf intltool libffi-dev libgdk-pixbuf2.0-dev \
            libtool libltdl-dev libssl-dev libxml-parser-perl make \
            openssl p7zip-full patch perl pkg-config python ruby \
            sed unzip wget xz-utils
        if ! [[ `uname -m` =~ "i686" ]]; then
            apt-get --yes install g++-multilib libc6-dev-i386
        fi
        # install bc to compare Debian releases
        apt-get --yes install bc
        DIST=`lsb_release -si`
        REL=`lsb_release -sr`
        if ( [[ $DIST =~ "Debian" ]] && (( `echo "$REL > 8" | bc -l` )) ) || \
            ( [[ $DIST =~ "Ubuntu" ]] && (( `echo "$REL > 14.10" | bc -l` )) ) || \
            ( [[ "$REL" == "unstable" ]] ); then
            apt-get --yes install libtool-bin
        fi
        # install Lua for build-pkg
        apt-get --yes install lua5.1
    elif which yum; then
        # Fedora
        yum install \
            autoconf automake bash bison bzip2 flex gcc-c++ \
            gdk-pixbuf2-devel gettext git gperf intltool make \
            sed libffi-devel libtool openssl-devel p7zip patch \
            perl pkgconfig python ruby unzip wget xz
    elif which pacman-g2; then
        # Frugalware
        pacman-g2 -S \
            autoconf automake bash bzip2 bison flex gcc gdk-pixbuf2\
            gettext git gperf intltool make sed libffi libtool \
            openssl patch perl perl-xml-parser pkgconfig python \
            ruby unzip wget xz xz-lzma
    elif which emerge; then
        # Gentoo
        emerge \
            sys-devel/autoconf sys-devel/automake app-shells/bash \
            sys-devel/bison app-arch/bzip2 \
            sys-devel/flex sys-devel/gcc sys-devel/gettext \
            dev-vcs/git dev-util/gperf dev-util/intltool \
            sys-devel/make sys-apps/sed dev-libs/libffi \
            sys-devel/libtool dev-libs/openssl app-arch/p7zip \
            sys-devel/patch dev-lang/perl dev-perl/XML-Parser \
            dev-util/pkgconfig dev-lang/python dev-lang/ruby \
            app-arch/unzip net-misc/wget \
            app-arch/xz-utils x11-libs/gdk-pixbuf
    elif which zypper; then
        # openSUSE
        zypper install -R \
            autoconf automake bash bison bzip2 flex gcc-c++ \
            gdk-pixbuf-devel gettext-tools git gperf intltool \
            libffi-devel libtool make openssl libopenssl-devel \
            p7zip patch perl perl-XML-Parser pkg-config python \
            ruby sed unzip wget xz
        if ! [[ `uname -m` =~ i686 ]]; then
            zypper install -R \
                gcc-32bit glibc-devel-32bit libgcc46-32bit \
                libgomp46-32bit libstdc++46-devel-32bit
        fi
    fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
    port install \
        autoconf automake bison coreutils flex gettext \
        gdk-pixbuf2 glib2 gnutar gsed intltool libffi libtool \
        openssl p5-xml-parser p7zip pkgconfig wget xz
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    pkg install \
        automake autoconf bash bison coreutils flex \
        gcc gdk-pixbuf2 gettext git glib gmake gperf gsed intltool libffi \
        libtool openssl p5-XML-Parser p7zip patch perl5 \
        pkgconf python ruby unzip wget
    pkg install file
else
    echo "unknown: $OSTYPE"
    exit 1
fi
