#!/bin/sh
# RTAI application loader script.
# (Processes the .runinfo files)
# April 2003, <rpm@xenomai.org>

# Copyright (C) 2005-2017 The RTAI project
# This [file] is free software; the RTAI project
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.

usage='usage: rtai-load [[dir:]target]'
prefix="/usr"
exec_prefix="/usr"
insmod=/sbin/insmod
modprobe=/sbin/modprobe
rmmod=/sbin/rmmod
modext=.ko
verbose=0
spc=" 	"

if test "x$1" = x--help; then
  echo $usage
  exit 0
fi

if test "x$1" = x--verbose; then
  verbose=1
  shift
fi

qual_target=$1
target_dir=`echo $qual_target|cut -d: -f1`
target_name=`echo $qual_target|cut -s -d: -f2`
if test "x$target_dir" = x; then
   target_dir=`pwd`
fi
if test "x$target_name" = x; then
   if `echo $target_dir | grep -q /`; then
      target_name=default
   else
      target_name=$target_dir
      target_dir=.
   fi
fi

run_info_file=$target_dir/.runinfo

if test \! -r $run_info_file; then
   echo "rtai-load: cannot find run info: $run_info_file"
   exit 2
fi

if test $target_name = default; then
  target_name=`cut -s -d: -f1 $run_info_file | head -1`
  if test "x$target_name" = x; then
     echo "rtai-load: no target defined in $run_info_file"
     exit 2
  fi
fi

stanza=`grep "^[$spc]*$target_name[$spc]*:" $run_info_file`

if test "x$stanza" = x; then
   have_user_command_option=`cut -d: -f1 .runinfo | grep user_command`
   if test "x$have_user_command_option" = x; then
       echo "passo di qui"
       echo "rtai-load: cannot find run info for target $target_name"
       exit 2
   else
       target_name=user_command
       stanza=`grep "^[$spc]*$target_name[$spc]*:" $run_info_file`
   fi
fi

rtai_moddir=`grep "^[$spc]*rtai_moddir[$spc]*=" $run_info_file`
user_moddir=`grep "^[$spc]*user_moddir[$spc]*=" $run_info_file`

if test "x$rtai_moddir" = x; then
  eval rtai_moddir='${DESTDIR}/lib/modules/4.4.257-7-0404rtai/extra'
else
  eval $rtai_moddir
fi

eval $user_moddir

if `$modprobe -nq adeos > /dev/null 2>&1`; then
   target_deps="adeos+"
else
   target_deps=
fi

target_deps="${target_deps}trace+hal+malloc+`echo $stanza | cut -d: -f2`"
target_info=`echo $stanza | cut -d: -f3`
target_mesg=`echo $stanza | cut -s -d: -f4`

if test "$target_name" = user_command; then
    target_info=`echo $target_info | sed s/";"/" $*\;"/` 
    shift $#
fi

test $verbose = 1 && echo "Running $target_name from $run_info_file"

if test \! "x`type -t sudo`" = x; then
  sudo=sudo
fi

if test \! "x$target_mesg" = x; then
  case "$target_mesg" in
  control_c)
    target_mesg="Type ^C to stop this application."
    ;;
  esac
  echo "*"
  echo "*"
  echo "* $target_mesg"
  echo "*"
  echo "*"
fi

sync

loadlist=`cat /proc/modules | ( \
  while read lm; do \
    set -- $lm; \
    loadlist="$loadlist $1"; \
  done  ; echo $loadlist )`

( cd $target_dir ;

inslist=""
children_pids=""

cleanup () {

    if test \! "x$children_pids" = x; then
#      $sudo kill -SIGINT $children_pids > /dev/null 2>&1 see comment below
       $sudo kill -INT $children_pids > /dev/null 2>&1
       children_pids=""
       sleep 1
    fi
    if test \! "x$inslist" = x; then
       for mod in $inslist ; do
	  test $verbose = 1 && echo "+ $rmmod $mod"
	  $sudo $rmmod $mod
       done
       inslist=""
    fi
}

#trap cleanup SIGINT  this is an old reminder, it seems it must be like below
trap cleanup INT

while test -n "$target_deps" ; do
  mod=`echo $target_deps|cut -d+ -f1`
  target_deps=`echo $target_deps|cut -s -d+ -f2-`
  if test -h $rtai_moddir/rtai_$mod$modext; then
     # Substitute the symlink with the target name since
     # new modutils (>= 2.5.48) will use the actual name.
     mod=`ls -l $rtai_moddir/rtai_$mod$modext|sed -e "s,.* \-> .*rtai_\([a-z0-9A-Z]*\)$modext.*,\1,"`
  fi
  if `echo $loadlist | grep -vq "\\brtai_$mod\\b"`; then
     # If we cannot find the module in our install dir, try Linux's
     # one. If the latter fails too, assume the feature is built into
     # the scheduler or not needed. Obviously, we should _always_ find
     # the scheduler...
     if test -r $rtai_moddir/rtai_$mod$modext; then
        test $verbose = 1 && echo "+ $insmod $rtai_moddir/rtai_$mod$modext"
        $sudo $insmod $rtai_moddir/rtai_$mod$modext
	if test $? = 0 ; then
           inslist="rtai_$mod $inslist"
           loadlist="$loadlist $mod"
	else
	   echo "ERROR: cannot load $rtai_moddir/rtai_$mod$modext"
	   break
        fi
     elif `$modprobe -n $mod > /dev/null 2>&1`; then
        test $verbose = 1 && echo "+ $modprobe $mod"
        $sudo $modprobe $mod
        inslist="$mod $inslist"
        loadlist="$loadlist $mod"
     fi
  fi
done

while test -n "$target_info" ; do
  action=`echo $target_info|cut -d';' -f1`
  target_info=`echo $target_info|cut -s -d';' -f2-`
  set -- $action

  case "$1" in

    push)

	mod=`basename $2 $modext`
        if test -r $rtai_moddir/$mod$modext; then
           modpath="$rtai_moddir/$mod"
        elif test -r $user_moddir/$mod$modext; then
           modpath="$user_moddir/$mod"
        else
           modpath="$mod"
        fi
	if `echo $loadlist | grep -vq "\\b$mod\\b"`; then
	   test $verbose = 1 && echo "+ $insmod $modpath$modext"
	   $sudo $insmod $modpath$modext
	   if test $? = 0; then
              loadlist="$loadlist $mod"
	      inslist="$mod $inslist"
	   else
	      echo "ERROR: cannot load $modpath$modext"
	   fi
	fi
	;;

    pop)

	shift
	if test "x$*" = x; then
           rmlist=`echo $inslist | cut -s -f1 -d' '`
        else
           rmlist=$*
	fi
        _inslist="$inslist"
	if test \! "x$rmlist" = x; then
           for mod in $rmlist ; do
	      test $verbose = 1 && echo "+ $rmmod $mod"
	      $sudo $rmmod $mod && _inslist=`echo $_inslist | sed -e "s,$mod,,g"`
           done
        inslist="$_inslist"
	fi
	;;

    popall|flush)

        cleanup
	;;

    *)

        case "$1" in
	  exec|spawn) shift ;;
	  klog) set -- \! tail -f /var/log/messages ;;
	esac
	e=`echo $* | sed -e "s,^[$spc]*![$spc]*\\(.*\\)$,\\1,"`
        test "$e" = "$*" && suflag= || suflag=$sudo
	set -- $e
	e=`echo $* | sed -e "s,\\(.*\\)&[$spc]*$,\\1,"`
        test "$e" = "$*" && waitflag=1 || waitflag=0
	set -- $e
	test $verbose = 1 && echo "+ spawning command ($*)"
	if test $waitflag = 1; then
           $suflag $*
        else
           $suflag $* &
	   children_pids="$! $children_pids"
	fi
	;;
  esac

done )

exit 0
