#!/bin/bash
#
# Startup script for mldonkey
#
# chkconfig: - 86 14
# description: mldonkey.
# processname: mlnet
# pidfile: /var/run/mlnet.pid
# config: 

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/mldonkey ]; then
        . /etc/sysconfig/mldonkey
fi

# Defaults, override them in /etc/sysconfig/mldonkey
: ${ML_ROOT=/chroot/mldonkey}
: ${ML_USER=mldonkey}
: ${ML_GROUP=mldonkey}
: ${ML_HOME=/usr/share/mldonkey}

copy_files_aux() {
  for file in "$@"; do
    mkdir -m 755 -p $ML_ROOT`dirname $file`
    # -R needed for /dev/log (???)
    cp -pR $file $ML_ROOT$file
    if test -L "$file"; then
      cp -pRL $file $ML_ROOT$file
    fi
  done
}

setup_chroot() {
  copy_files_aux /etc/localtime /dev/null /dev/log \
    /etc/resolv.conf /etc/hosts \
    /lib64/libnss_files.so.* /lib64/libnss_dns.so.* \
    /usr/lib64/mldonkey/mlnet /usr/X11R6/lib64/libX11.so.* /bin/sh \
    /lib64/ld-linux*.so.2

#  copy_files_aux `find $ML_ROOT -xdev -type f \
#  | xargs file \
#  | grep -Ev ':.*(Bourne shell|text|data|empty|statically linked)' \
#  | awk -F: '{print $1}' | xargs ldd | grep '=>' | awk '{print $3}'`

  copy_files_aux `find $ML_ROOT -xdev -type f \
  | xargs file | grep -E ':.*ELF' \
  | awk -F: '{print $1}' | xargs ldd | grep '=> */' | awk '{print $3}'`

  echo /usr/X11R6/lib64 > $ML_ROOT/etc/ld.so.conf
  ldconfig -r $ML_ROOT
  #
  mkdir -p $ML_ROOT/etc
  echo "$ML_USER:x:`id -u mldonkey`:`id -g mldonkey`:mldonkey User:$ML_HOME:/noshell" > $ML_ROOT/etc/passwd
  echo "$ML_USER:!!:::::::" > $ML_ROOT/etc/shadow
  echo "$ML_GROUP:x:$ML_GID:" > $ML_ROOT/etc/group
  echo "$ML_GROUP:!::" > $ML_ROOT/etc/gshadow
  cat > $ML_ROOT/etc/nsswitch.conf <<EOF
passwd: files
shadow: files
group: files
hosts: files dns
EOF
  mkdir -p $ML_ROOT/tmp
  chown -Rh mldonkey:mldonkey $ML_ROOT/
}

test_mount () {
  mount | grep "$ML_HOME on $ML_ROOT$ML_HOME type none (rw,bind)" > /dev/null
}

start_mldonkey() {
  cd $ML_ROOT$ML_HOME
  nice -n 19 /usr/bin/compartment \
     --chroot $ML_ROOT \
     --user $ML_USER --group $ML_GROUP \
     --fork /bin/sh -c "cd $ML_HOME && exec /usr/lib64/mldonkey/mlnet >> /mldonkey.log 2>&1 < /dev/null" &
}

stop_mldonkey() {
  if echo kill | nc 127.0.0.1 4000 > /dev/null; then
    killproc /usr/lib64/mldonkey/mlnet || /bin/true
  else
    killproc /usr/lib64/mldonkey/mlnet
  fi
}

start() {
  echo -n $"Starting mldonkey: "
  pid=`pidofproc /usr/lib64/mldonkey/mlnet`
  if [ -n "$pid" ]; then
    echo -n "mldonkey ($pid) is already running!"
    failure $"mldonkey ($pid) is already running!"
    echo
    return 1
  fi

  mkdir -p $ML_HOME
  chown mldonkey:mldonkey $ML_HOME

  setup_chroot

  # do the mounts
  if test_mount; then
    :
  else
    mkdir -p $ML_ROOT$ML_HOME
    mount -o bind $ML_HOME $ML_ROOT$ML_HOME || echo "Mount failed" || failure "Mount failed" || exit 1
  fi

  start_mldonkey

  RETVAL=$?
  sleep 1
  if [ $RETVAL = 0 ]; then
    pid=`pidofproc /usr/lib64/mldonkey/mlnet`
    if [ -n "$pid" ]; then
      touch /var/lock/subsys/mldonkey
      echo $pid > /var/run/mlnet.pid
      success $"mldonkey startup"
      echo
      return 0
    fi
    RETVAL=1
  fi
  failure $"mldonkey startup"
  echo
  return $RETVAL
}

stop() {
  echo -n $"Stopping mldonkey: "

  stop_mldonkey

  RETVAL=$?
  # undo the mounts
  if test_mount; then
    umount $ML_ROOT$ML_HOME || echo "Umount failed" || failure "Umount failed" || exit 1
  fi
  
  if [ $RETVAL = 0 ]; then
    rm -f /var/lock/subsys/mldonkey
    rm -f /var/run/mlnet.pid
    success $"mldonkey shutdown"
  else
    failure $"mldonkey shutdown"
  fi
  echo
  return $RETVAL
}

reload() {
  stop_mldonkey && start_mldonkey
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  reload)
	reload
	;;
  status)
        status mlnet
	;;
  *)
	echo $"Usage: $prog {start|stop|status|restart|reload}"
	exit 1
esac

exit $RETVAL
