#!/bin/sh

### BEGIN INIT INFO
# Provides: shanghai_weiling
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the APPLICATION unicorns at boot
# Description: Enable ntmenu at boot time.
### END INIT INFO

# Change these to match your app:
APP_NAME=weiling
APP_ROOT="/www/weiling-company/current"
PID="/www/weiling-company/shared/pids/weiling-unicorn.pid"

PATH="/usr/local/nodejs12.16.1/bin:/usr/local/ruby2.6.5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export LANG='en_US.UTF-8'
export PATH=$PATH
export GEM_PATH=/usr/local/ruby2.6.5/lib/ruby/gems/2.6.0
set -e

CMD="bundle exec unicorn_rails -D -E production -c $APP_ROOT/config/unicorn.conf.rb"

old_pid="$PID.oldbin"

cd $APP_ROOT || exit 1

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $old_pid && kill -$1 `cat $old_pid`
}

case ${1-help} in
  start)
 sig 0 && echo >&2 "Already running" && exit 0
sh -c "$CMD"
 ;;
  stop)
 sig QUIT && exit 0
 echo >&2 "Not running"
 ;;
  force-stop)
 sig TERM && exit 0
 echo >&2 "Not running"
 ;;
  restart|reload)
 sig HUP && echo reloaded OK && exit 0
 echo >&2 "Couldn't reload, starting '$CMD' instead"
sh -c "$CMD"
 ;;
  upgrade)
 sig USR2 && exit 0
 echo >&2 "Couldn't upgrade, starting '$CMD' instead"
sh -c "$CMD"
 ;;
  rotate)
 sig USR1 && echo rotated logs OK && exit 0
 echo >&2 "Couldn't rotate logs" && exit 1
 ;;
  *)
 echo >&2 "Usage: $0 <start|stop|restart|force-stop>"
 exit 1
 ;;
esac
