#!/bin/sh
#   This file is part of fidoip. It is free software and it is covered
#   by the GNU general public license. See the file LICENSE for details. */
OPTS=" -DCq INSTALLDIR/usr/etc/binkd.cfg"
DAEMON="INSTALLDIR/usr/sbin/binkd"
USERNAME="USERNAME2change"
OS=`uname`
if [ "$OS" = "Linux" ]; then
# sets 1 if linux runs under Android Termux AnLinux
ANLINUX=`top -n1  2>/dev/null | grep "proot" | grep -v "grep" | wc -l`
fi

case "$1" in
  start)
    echo "Starting FTN mailer: binkd "
if [ "$ANLINUX" = "1" ]; then
  $DAEMON $OPTS
else
    su -m "$USERNAME" -c "$DAEMON $OPTS"
fi
    echo "."
    ;;
 stop)
    echo "Stopping FTN mailer..."
if [ "$ANLINUX" = "1" ]; then
killall binkd 2>/dev/null 
     echo "Stopped binkd"
      echo "."
else
    # Get all process IDs of the service using ps and grep
    PIDS=$(ps aux | grep "[b]inkd" | grep -v "grep" | awk '{print $2}')
    if [ -n "$PIDS" ]; then
   # Set core file size limit to zero
     ulimit -c 0
      for PID in $PIDS; do
        kill -9 "$PID" 2>/dev/null 
       echo
        echo "Stopped binkd with PID: $PID"
      done
      echo "."
    else
      echo "FTN mailer: binkd is not running"
    fi
fi
    ;;
  *)
    echo "Usage: INSTALLDIR/usr/etc/rc.d/binkdsrv {start|stop}"
    exit 1
    ;;
esac

exit 0
