#!/bin/bash
# dpkg configuration script for emc2
# Copyright (C) 2006 Jeff Epler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# When used to produce a debian package, this file is a script "used to
# control compilation and installation of the executable"

usage () {
    P=${0##*/}
    cat <<EOF
$P: Set up debian/ files to build for a particular kernel

Usage:
    $P sim
        Build for a system without a realtime kernel

    $P [kernel-version]
        Build for the realtime kernel [kernel-version], such as 2.6.12-magma

    $P -r
        Build for the currently running kernel

    $P -a
        If the currently running kernel has realtime extensions, use it.
        Otherwise use any available realtime kernel it can find.
EOF
}

cd "${0%/*}"

if [ $# -ne 1 -o "$1" == "-h" -o "$1" == "-help" -o "$1" == "--help" ]; then
    usage
    if [ $# -ne 1 ]; then
        exit 1
    fi
    exit 0
fi

if [ $1 = "-r" ]; then
    set -- `uname -r`
elif [ $1 = "-a" ]; then
    KERNEL="$(uname -r)"
    if [ ! -d "/usr/realtime-$KERNEL" ]; then
        KERNEL=$(ls -1d /usr/realtime-* 2> /dev/null | cut -d - -f 2- | head -1)
        if [ -z "$KERNEL" ]; then
            echo "no realtime kernels found!"
            exit 1
        fi
    fi
    set -- $KERNEL
fi

TARGET=$1

MODULE_PATH=usr/realtime-$1/modules/emc2
MODULE_EXT=.ko
KERNEL_DEPENDS=linux-image-$1,rtai-modules-$1
KERNEL_HEADERS=linux-headers-$1
EXTRA_FILES="usr/bin/emc_module_helper usr/bin/bfload"
KERNEL_VERSION=$1
DRIVERS=drivers.files.in
PYTHON_VERSION=$(python -c 'import sys; print sys.version[:3]')
PYTHON_VERSION_NEXT=$(python -c 'import sys; print sys.version[:2] + str(1+int(sys.version[2]))')

# hope this works on other systems too
DISTRIB_NAME=
if [ -f /etc/lsb-release ]; then
    source /etc/lsb-release
    DISTRIB_NAME=$DISTRIB_ID-$DISTRIB_RELEASE
fi

DOC_BUILD=lyx,texlive-extra-utils,texlive-latex-recommended,texlive-fonts-recommended,ghostscript,imagemagick,texlive-lang-french
EMC2_PACKAGE_NAME=emc2
case $TARGET in
    sim)
        MODULE_PATH=usr/lib/emc2/modules
	MODULE_EXT=.so
	KERNEL_DEPENDS=
	KERNEL_HEADERS=libpth-dev
	DRIVERS=
        EXTRA_FILES="usr/bin/rtapi_app"
	EXTRA_BUILD=lyx-qt
	if test "$DISTRIB_NAME" = "Ubuntu-8.04" ; then 
	    EXTRA_BUILD=$DOC_BUILD
	fi
	DISTRIB_NAME=sim-$DISTRIB_NAME
	EMC2_PACKAGE_NAME=emc2-sim
    ;;
    2.6.12-magma)
        KERNEL_HEADERS=$KERNEL_HEADERS,gcc-3.4
	EXTRA_BUILD=lyx-qt
    ;;
    2.6.15-magma)
        KERNEL_HEADERS=$KERNEL_HEADERS,gcc-4.0
	EXTRA_BUILD=lyx-qt
    ;;
    2.6.22.6-magma|2.6.24-16-rtai)
        KERNEL_HEADERS="$KERNEL_HEADERS"
        EXTRA_BUILD=$DOC_BUILD
    ;;
    2.6.24-16-rtai)
        KERNEL_HEADERS=$KERNEL_HEADERS,gcc-4.2
	EXTRA_BUILD=lyx
    ;;
    *)
	echo "your kernel '$TARGET' is not known. There might be needed dependencies which won't get set automatically."
esac

subst () {
sed -e "s|@MODULE_PATH@|$MODULE_PATH|g" \
    -e "s|@MODULE_EXT@|$MODULE_EXT|g" \
    -e "s|@KERNEL_VERSION@|$KERNEL_VERSION|g" \
    -e "s|@KERNEL_DEPENDS@|$KERNEL_DEPENDS|g" \
    -e "s|@KERNEL_HEADERS@|$KERNEL_HEADERS|g" \
    -e "s|@EXTRA_BUILD@|$EXTRA_BUILD|g" \
    -e "s|@EMC2_PACKAGE_NAME@|$EMC2_PACKAGE_NAME|g" \
    -e "s|@EXTRA_FILES@|$EXTRA_FILES|g" \
    -e "s|@DISTRIB_NAME@|$DISTRIB_NAME|g" \
    -e "s|@TARGET@|$TARGET|g" \
    -e "s|@PYTHON_VERSION@|$PYTHON_VERSION|g" \
    -e "s|@PYTHON_VERSION_NEXT@|$PYTHON_VERSION_NEXT|g" \
    $*
}

subst control.in > control
if [ "$TARGET" != "sim" ]; then subst control.firmware >> control; fi
if [ "$TARGET" == "sim" ]; then cp emc2-dev.files emc2-sim-dev.files; fi

subst rules.in > rules; chmod +x rules
if [ -f extras-$DISTRIB_NAME/emc2.files ]; then
    subst emc2.files.in $DRIVERS extras-$DISTRIB_NAME/emc2.files > $EMC2_PACKAGE_NAME.files
else
    subst emc2.files.in $DRIVERS > $EMC2_PACKAGE_NAME.files
fi

rm -f ../build-stamp
echo "successfully configured for '$DISTRIB_NAME'-'$TARGET'.."
