#!/bin/sh

# Test for Ch C language interpreter
_CH_=0
ch -v > /dev/null 2> /dev/null
if [ $? -eq 0 ] ; then
  _CH_=1
fi

# Test for RTAI 3, using heuristic that there will be a module in
# the booted kernel's /lib/modules/XXX/rtai/ directory named 'rtai_hal.o',
# and there will be a directory named something like /usr/src/rtai-3.X

echo -n "looking for RTAI 3 booted...    "
if [ -f /lib/modules/`uname -r`/rtai/rtai_hal.o ] ; then
# have RTAI 3
  echo "found it"
  echo -n "looking for RTAI 3 directory... "
  rtdirs=`\find /usr/src -maxdepth 1 -type d -name 'rtai-3.*'`
  for dir in $rtdirs ; do
    rtdir=$dir
  done
  if [ "x$dir" = "x" ] ; then
    echo '***Error*** none found'
    exit 1
  else
    echo "found it in $rtdir"
    echo -n "creating Makefile.inc...        "
    \rm -f Makefile.inc
    \cat > Makefile.inc <<EOF
RTAI_DIR = $rtdir
RTAI_INCLUDE = -I\$(RTAI_DIR)/rtai-compat/include -I\$(RTAI_DIR) -I\$(RTAI_DIR)/rtai-core/include -I/usr/src/linux/include
CC = gcc
CFLAGS = -g \$(RTAI_INCLUDE) -DRTAI -DRTAI_3
RT_CFLAGS = \$(RTAI_INCLUDE) -O2 -DRTAI -DRTAI_3
LD = ld
LDFLAGS = -r -static
_CH_ = $_CH_
EOF
    echo "done"
    exit 0
  fi
else
  echo "not found"
fi

# Test for RTAI 2, using heuristic that there will be a module in
# the booted kernel's /lib/modules/XXX/rtai/ directory named 'rtai.o',
# and there will be a directory named something like /usr/src/rtai-24.X

echo -n "looking for RTAI 2 booted...    "
if [ -f /lib/modules/`uname -r`/rtai/rtai.o ] ; then
# have RTAI 2
  echo "found it"
  echo -n "looking for RTAI 2 directory... "
  rtdirs=`\find /usr/src -maxdepth 1 -type d -name 'rtai-2*'`
  for dir in $rtdirs ; do
    rtdir=$dir
  done
  if [ "x$dir" = "x" ] ; then
    echo '***Error*** none found'
    exit 1
  else
    echo "found in $rtdir"
    echo -n "creating Makefile.inc...        "
    \rm -f Makefile.inc
    \cat > Makefile.inc <<EOF
RTAI_DIR = $rtdir
RTAI_INCLUDE = -I\$(RTAI_DIR)/include -I/usr/src/linux/include
CC = gcc
CFLAGS = -g \$(RTAI_INCLUDE) -DRTAI -DRTAI_2
RT_CFLAGS = \$(RTAI_INCLUDE) -O2 -DRTAI -DRTAI_2
LD = ld
LDFLAGS = -r -static
_CH_ = $_CH_
EOF
    echo "done"
    exit 0
  fi
else
  echo "not found"
fi

# Test for RTAI 1, using heuristic that there will be a module in
# the booted kernel's /lib/modules/XXX/misc/ directory named 'rtai.o',
# and there will be a directory named something like /usr/src/rtai-1.X

echo -n "looking for RTAI 1 booted...    "
if [ -f /lib/modules/`uname -r`/misc/rtai.o ] ; then
# have RTAI 1
  echo "found it"
  echo -n "looking for RTAI 1 directory... "
  rtdirs=`\find /usr/src -maxdepth 1 -type d -name 'rtai-1.*'`
  for dir in $rtdirs ; do
    rtdir=$dir
  done
  if [ "x$dir" = "x" ] ; then
    echo '***Error*** none found'
    exit 1
  else
    echo "found in $rtdir"
    echo -n "creating Makefile.inc...        "
    \rm -f Makefile.inc
    \cat > Makefile.inc <<EOF
RTAI_DIR = $rtdir
RTAI_INCLUDE = -I\$(RTAI_DIR)/include -I/usr/src/linux/include
CC = gcc
CFLAGS = -g \$(RTAI_INCLUDE) -DRTAI -DRTAI_1
RT_CFLAGS = \$(RTAI_INCLUDE) -O2 -DRTAI -DRTAI_1
LD = ld
LDFLAGS = -r -static
_CH_ = $_CH_
EOF
    echo "done"
    exit 0
  fi
else
  echo "not found"
fi

\rm -f Makefile.inc
\cat > Makefile.inc <<EOF
RTAI_DIR = <path_to_rtai_directory>
RTAI_INCLUDE = -I\$(RTAI_DIR) -I/usr/src/linux/include
CC = gcc
CFLAGS = -g \$(RTAI_INCLUDE)
RT_CFLAGS = \$(RTAI_INCLUDE) -O2
LD = ld
LDFLAGS = -r -static
_CH_ = $_CH_
EOF

echo '***Error*** please set RTAI_DIR in Makefile.inc'

exit 1

