#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind
#
# scripts to start the MySQL demon and restart it if it dies unexpectedly
#
# This should be executed in the MySQL directory if you are using a
# binary installation that has other paths than you are using.
#
# mysql.server works by first doing a cd to the base directory and from there
# executing safe_mysqld

pidfile=`hostname`.pid
log=`hostname`.log

# Check if we are starting this relative (for the binary release)
if test -f ./data/mysql/db.frm -a -f ./share/english/errmsg.sys -a \
 -x ./bin/mysqld
then
  MY_BASEDIR_VERSION=`pwd`		# Where bin, share and data is
  DATADIR=$MY_BASEDIR_VERSION/data	# Where the databases are
  ledir=$MY_BASEDIR_VERSION/bin		# Where mysqld are
else
  MY_BASEDIR_VERSION=/usr/pkg/mysql-3.20.17-beta
  DATADIR=/usr/pkg/mysql-3.20.17-beta/var
  ledir=/usr/pkg/mysql-3.20.17-beta/libexec
fi

#
# If there exists an old pid file, check if the demon is already running
# Note: The switches to 'ps' may depend on your operating system

if test -f $DATADIR/$pidfile
then
  PID=`cat $DATADIR/$pidfile`
  if /usr/bin/kill -0 $PID
  then
    if /usr/bin/ps -ef | grep mysqld | grep  > /dev/null
    then    # The pid contains a mysqld process
      echo "A mysqld process already exists"
      exit 0;
    fi
  fi
  if rm -f $DATADIR/$pidfile
  then
    echo "Fatal error: Can't remove the pid file: $DATADIR/$pidfile"
    echo "mysqld demon not started"
  fi
fi

echo "Starting mysqld demon with databases from $DATADIR"

#Default communication ports
#MYSQL_TCP_PORT=3333
#MYSQL_UNIX_PORT="/tmp/mysql.sock"
#export MYSQL_TCP_PORT MYSQL_UNIX_PORT

echo -n "mysqld started on " >> $DATADIR/$log
date >> $DATADIR/$log
while true
do
  $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR \
    "$@" >> $DATADIR/$log 2>&1
  if test ! -f $DATADIR/$pidfile	# This is removed if normal shutdown
  then
    break;
  fi
  echo "mysqld restarted" >> $DATADIR/$log
  echo "mysqld restarted"
done

echo -n "mysqld ended on " >> $DATADIR/$log
date >> $DATADIR/$log
echo "" >> $DATADIR/$log
echo "mysqld demon ended"
