#!/bin/bash
#
### BEGIN INIT INFO
# Provides: mysqld-helper
# Required-Start: $remote_fs $syslog networking procps
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 5
# Default-Stop: S 1 4
# Short-Description: Mysql Helper
# Description: NDN Mysql Helper
### END INIT INFO

# MySQL helper daemon start/stop script.
# This gets run everytime the startup script gets run

PATH=/sbin:/usr/sbin:/bin:/usr/bin
# basedir=/dh/mysql
# bindir=base/bin

export PATH

mode=$1

# set some variables
. /dh/mysql/etc/envrc

# if the user wants a specific service(s), give it them!
if [ $2 ];
then
  SERVERS="$2 $3 $4 $5 $6 $7 $8 $9"
fi

# go
for S in $SERVERS
do
  # Safeguard (relative paths, core dumps..)
  cd $basedir

  case "$mode" in
    'all')
      # This section is run everytime /etc/init.d/mysqld is run.

      # make sure binlog purging is setup
      if test ! -d /dh/mysql/etc/binlog_purge ; then
        mkdir -p /dh/mysql/etc/binlog_purge
      fi
      if test ! -e /dh/mysql/etc/binlog_purge/$S ; then
        echo adding adding default binlog_purge time of 48 hours to /dh/mysql/etc/binlog_purge/$S
        echo -n "48" > /dh/mysql/etc/binlog_purge/$S
      fi
      ;;

    'flush-threads')
      echo mysqladmin $S flush-threads
      ;;

    'flush-privileges')
      echo mysqladmin $S flush-privileges
      ;;

    *)
      # usage
      echo "Usage: $0 command"
      exit 1
      ;;
  esac

done
