#!/bin/bash
# This wrapper is a work-around for links2 -g failing 
# with directfb from scripts unless setuid root... P.G. 2008-07-20

# Warn $USER if using non-tty
if [ "$TERM" != "linux" ] && [ "$DISPLAY" = "" ] ; then
    echo -e "\n  Links2 -g (xlinks2) doesn't run from \"GNU Screen\" or \"dvtm\", sorry..."
    echo -e "  Try from a tty (tty1-6) instead."
    sleep 7
    setterm -cursor on # Directfb eats the cursor :/
    exit 1
fi

set -m                              # Job control - makes "fg" work

# Background loop etc.

(
# Watch for links2 -g  (links2 with directfb driver only runs with 'exec', from scripts)
until pidof links2 > /dev/null 2>&1
  do
    :                               # For a split second, until links2 -g runs
  done

# Now do nothing much in background while links2 -g is running
while pidof links2 > /dev/null 2>&1
  do
    sleep .5 # Using 'sleep' is much less demanding on CPU than spinning 'no-op' ("true")
  done

) &                                 # Spins its wheels while xlinks2 runs. 

xlinks2 "$@"                        # xlinks2 is 'exec links2 -g "$@"'

# Wake up! Control passes to the next command in the calling script...

fg # Bring back/foreground the loop, which stops immediately, transferring control to calling script again





