#!/bin/sh

#
# This script starts VFB (Virtual Frame Buffer) under X to allow
# the creation of gif images using GEMPAK on the IBM-SP.
# The one word name of the current shell  (when this script is called) must
# be passed in as an argument to create the proper source file.
#
# The script stopxvfb must be run once you are done creating
# images to stop the X process.
#

if [ $# -ne 1 ] ; then
  echo "USAGE:  $0 [current shell type]"
  echo "Example $0 /bin/sh"
  exit 99
fi

#set -x
parent_shell=`basename $1`

#
# First find out what other virtual displays are being used
#
rm tmp.vfbprocs
ps ax | grep X | grep vfb | awk -F: '{print " "$3}'>tmp.vfbprocs

i=1
stop=0

#
# Find a vacant virtual display
#

while [ $stop -eq 0 ] ; do
  grep " $i " tmp.vfbprocs
  stat=$?
  if [ $stat -eq 1 ] ; then
    stop=1
    vfbwindow=$i
  else
    let i=i+1
  fi
done

rm tmp.vfbprocs

#
# Create a file to be sourced by the parent script
#

echo "X -force -vfb -x GLX -x abx -x dbe -cc 3 -d 8 :${vfbwindow} &" > ./STARTXVFB
echo "sleep 5" >> ./STARTXVFB

case $parent_shell in

 "sh" | "ksh")
  echo "export DISPLAY=:${vfbwindow}.0" >> ./STARTXVFB
  echo "export VFBWINDOW=${vfbwindow}" >> ./STARTXVFB ;;

 "csh")
  echo "setenv DISPLAY :${vfbwindow}.0" >> ./STARTXVFB
  echo "setenv VFBWINDOW ${vfbwindow}" >> ./STARTXVFB ;;

esac
