#!/bin/sh

# This wrapper script starts bibledit and the helper processes.



# The first reasons that made this script necessary was that Gtk got mixed up
# when a child process was repeatedly run.
# Bibledit collaborates by very often calling git child 
# processes. It has been noticed in practise that after much use during a day
# of work with Bibledit, the Gtk based graphical functions cease to work.
# Messages have been seen saying that there were too many open files.
# Both the Gtk based graphical functions ceased to work and the child processed 
# failed to be spawned.
# By separating those child processes completely from Bibledit's Gtk, these
# problems disappeared.
# Another reason for using helper programs is that Bibledit got big and 
# sometimes got slower because of the many child processes. 
# By separating these processed this improved too.



# The temporal directory. Create it.
mkdir -p /tmp/bibledit



# If bibledit is running already, give a message and then bail out.
ps ax > /tmp/bibledit/processes
grep -q "bibledit-bin" /tmp/bibledit/processes
if test $? -eq 0
  then
	echo Bibledit is already running: bailing out.
	bibledit-one	
	exit
fi
rm /tmp/bibledit/processes


# The log file of bibledit.
# The output of the processes is piped to the logfile.
# The previous logfile is be saved.

LOGFILE=/tmp/bibledit/bibledit.log
LOGFILEOLD=${LOGFILE}.old
mv -f ${LOGFILE} ${LOGFILEOLD} 2> /dev/null



# Same story for the log file for bibledit-git.

LOGFILEGIT=/tmp/bibledit/git.log
LOGFILEOLD=${LOGFILEGIT}.old
mv -f ${LOGFILEGIT} ${LOGFILEOLD} 2> /dev/null




# Start bibledit-git.
# The choice of the actual binary that will run is done so as to facilitate
# programming. This process is explained below, see with bibledit-bin.
# Do not start it if bibledit-bin is used for scripting.

# BIBLEDIT_GIT=bibledit-git

# BIBLEDIT_GIT_SAME_DIRECTORY=`dirname $0`/${BIBLEDIT_GIT}

# BIBLEDIT_GIT_DEVELOPMENT_DIRECTORY=`dirname $0`/../src/${BIBLEDIT_GIT}

# if [ -x ${BIBLEDIT_GIT_SAME_DIRECTORY} ]
#   then
#     BIBLEDIT_GIT=${BIBLEDIT_GIT_SAME_DIRECTORY}
#   elif [ -x ${BIBLEDIT_GIT_DEVELOPMENT_DIRECTORY} ]
#     then
#       BIBLEDIT_GIT=${BIBLEDIT_GIT_DEVELOPMENT_DIRECTORY}
# fi

# Start bibledit-git, and kill any previous such process.
# killall bibledit-git > /dev/null 2>&1
# ${BIBLEDIT_GIT} > ${LOGFILEGIT} 2>&1 &




# Start bibledit-bin with all the commandline arguments.
# Using $@ is more useful than $* if both are enclosed within double 
# quotation marks. See the bash manual for reasons.

# Starting bibledit-bin is done in a special way.
# First we see if there is a bibledit-bin in the same directory this script
# was called from. If so, we take that.
# Secondly we see if there is a bibledit-bin in subdirectory src of the 
# parent directory of the directory this script was called from.
# If so we take that one. This facilitates programming Bibledit.
# From the source directory src the programmer can then run ../script/bibledit
# and it will then call ./bibledit-bin in the src directory.
# If none of these were found, then we just take "bibledit-bin", looking in
# the $PATH.

BIBLEDIT_BIN=bibledit-bin

BIBLEDIT_BIN_SAME_DIRECTORY=`dirname $0`/${BIBLEDIT_BIN}

BIBLEDIT_BIN_DEVELOPMENT_DIRECTORY=`dirname $0`/../src/${BIBLEDIT_BIN}

if [ -x ${BIBLEDIT_BIN_SAME_DIRECTORY} ]
  then
    BIBLEDIT_BIN=${BIBLEDIT_BIN_SAME_DIRECTORY}
  elif [ -x ${BIBLEDIT_BIN_DEVELOPMENT_DIRECTORY} ]
    then
      BIBLEDIT_BIN=${BIBLEDIT_BIN_DEVELOPMENT_DIRECTORY}
fi

${BIBLEDIT_BIN} $@ > ${LOGFILE} 2>&1

