#!/bin/sh
# chkconfig:         2345 75 15
# description:       nexus
### BEGIN INIT INFO
# Provides:          nexus
# Required-Start:    $local_fs $network $remote_fs $syslog $time
# Required-Stop:     $local_fs $network $remote_fs $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nexus
### END INIT INFO

original_dir=`pwd`

actualfile=$(realpath "$0")
progname=`basename "$actualfile"`
linkdir=`dirname "$actualfile"`
HOME="$linkdir"
cd "$HOME" || exit 1
bootJar=$(ls -1 sonatype-nexus-repository-*.jar | head -n1)
bootJar=$(realpath $bootJar)
cd '../' || exit 1
PARENT=`pwd`
EMBEDDED_JDK="$(realpath jdk/temurin_*_linux_x86_64/jdk-17.*)"

# user to execute as; optional but recommended to set
run_as_user=''

# load optional configuration
rc_file="$HOME/nexus.rc"
if [ -f "$rc_file" ]; then
  . "$rc_file"
fi

# detect if execute as root user
run_as_root=true
user_id=`id -u`
user_name=`id -u -n`
if [ -z "$run_as_user" ] && [ $user_id -ne 0 ]; then
  run_as_root=false
elif [ -n "$run_as_user" ] && [ "$run_as_user" != 'root' ]; then
  run_as_root=false
fi

user_id=`id -u`
# complain if root execution is detected
if $run_as_root; then
  echo 'WARNING: ************************************************************'
  echo 'WARNING: Detected execution as "root" user.  This is NOT recommended!'
  echo 'WARNING: ************************************************************'
elif [ -n "$run_as_user" ] && [ "$run_as_user" != "$user_name" ]; then
  # re-execute launcher script as specified user
  exec su - $run_as_user "$HOME/nexus" $@
fi

# Priority list of jvm location
# APP_JAVA_HOME environement variable
# INSTALL4J_JAVA_HOME_OVERRIDE environement variable (deprecated)
# app_java_home variable from nexus.rc file
# EMBEDDED_JDK as configured earlier in script
if [ -n "$APP_JAVA_HOME" ]; then
  app_java_home=$APP_JAVA_HOME
elif [ -n "$INSTALL4J_JAVA_HOME_OVERRIDE" ]; then
  app_java_home=$INSTALL4J_JAVA_HOME_OVERRIDE
fi

# app_java_home may be set by above if statement, or from the nexus.rc file
if [ -z "$app_java_home" ]; then
  app_java_home="$EMBEDDED_JDK"
fi

read_vmoptions() {
  vmoptions_file=`eval echo "$1" 2>/dev/null`
  if [ ! -r "$vmoptions_file" ]; then
    vmoptions_file="$linkdir/$vmoptions_file"
  fi
  if [ -r "$vmoptions_file" ] && [ -f "$vmoptions_file" ]; then
    exec 8< "$vmoptions_file"
    while read cur_option<&8; do
      is_comment=`expr "W$cur_option" : 'W *#.*'`
      if [ "$is_comment" = "0" ]; then
        vmo_classpath=`expr "W$cur_option" : 'W *-classpath \(.*\)'`
        vmo_classpath_a=`expr "W$cur_option" : 'W *-classpath/a \(.*\)'`
        vmo_classpath_p=`expr "W$cur_option" : 'W *-classpath/p \(.*\)'`
        vmo_include=`expr "W$cur_option" : 'W *-include-options \(.*\)'`
        if [ ! "W$vmo_include" = "W" ]; then
          if [ "W$vmo_include_1" = "W" ]; then
            vmo_include_1="$vmo_include"
          elif [ "W$vmo_include_2" = "W" ]; then
            vmo_include_2="$vmo_include"
          elif [ "W$vmo_include_3" = "W" ]; then
            vmo_include_3="$vmo_include"
          fi
        fi
        if [ ! "$vmo_classpath" = "" ]; then
          local_classpath="$i4j_classpath:$vmo_classpath"
        elif [ ! "$vmo_classpath_a" = "" ]; then
          local_classpath="${local_classpath}:${vmo_classpath_a}"
        elif [ ! "$vmo_classpath_p" = "" ]; then
          local_classpath="${vmo_classpath_p}:${local_classpath}"
        elif [ "W$vmo_include" = "W" ]; then
          needs_quotes=`expr "W$cur_option" : 'W.* .*'`
          if [ "$needs_quotes" = "0" ]; then
            vmoptions_val="$vmoptions_val $cur_option"
          else
            if [ "W$vmov_1" = "W" ]; then
              vmov_1="$cur_option"
            elif [ "W$vmov_2" = "W" ]; then
              vmov_2="$cur_option"
            elif [ "W$vmov_3" = "W" ]; then
              vmov_3="$cur_option"
            elif [ "W$vmov_4" = "W" ]; then
              vmov_4="$cur_option"
            elif [ "W$vmov_5" = "W" ]; then
              vmov_5="$cur_option"
            fi
          fi
        fi
      fi
    done
    exec 8<&-
    if [ ! "W$vmo_include_1" = "W" ]; then
      vmo_include="$vmo_include_1"
      unset vmo_include_1
      read_vmoptions "$vmo_include"
    fi
    if [ ! "W$vmo_include_2" = "W" ]; then
      vmo_include="$vmo_include_2"
      unset vmo_include_2
      read_vmoptions "$vmo_include"
    fi
    if [ ! "W$vmo_include_3" = "W" ]; then
      vmo_include="$vmo_include_3"
      unset vmo_include_3
      read_vmoptions "$vmo_include"
    fi
  fi
}

cd "$HOME" || exit 1
vmoptions_val=""
read_vmoptions "nexus.vmoptions"
INSTALL4J_ADD_VM_PARAMS="$INSTALL4J_ADD_VM_PARAMS $vmoptions_val"

# deduce the chosen data directory and prepare log and tmp directories
vm_args="$INSTALL4J_ADD_VM_PARAMS"
if expr "$vm_args" : '.*-Dkaraf\.data=.*' > /dev/null; then
  data_dir=`echo "$vm_args -X" | sed -e 's/.*-Dkaraf\.data=//' -e 's/  *-[A-Za-z].*//'`
else
  echo "required property 'karaf.data' undefined, check $HOME/nexus.vmoptions"
  exit 1
fi

# resolve a relative path the path
if [[ "$data_dir" != /* ]]; then
  data_dir="$PARENT/$data_dir"
fi

# Make the directory before using realpath
mkdir -p "$data_dir"
data_dir=$(realpath "$data_dir")

mkdir -p "$data_dir/log" "$data_dir/tmp"
pidfile="$data_dir/nexus.pid"

return_code=0
case "$1" in
  start)
    echo "Starting nexus"

    cd "$PARENT" || exit 1
    nohup "$app_java_home/bin/java" -server '-Dnexus.installer.type=linux-x86-64' $INSTALL4J_ADD_VM_PARAMS -jar "$bootJar" > /dev/null 2>&1 &
    return_code=$?
    pid=$!
    echo "$pid" > "$pidfile"
    cd "$original_dir" || exit 1
  ;;
  start-launchd)
    echo "Starting nexus"
    $0 run
  ;;
  stop)
    echo "Shutting down nexus"

    if [ -f "$pidfile" ]; then
      pid=$(cat "$pidfile")
      if ps -p $pid > /dev/null; then
        echo "Stopping nexus running with pid $pid"
        # -15 is SIGTERM
        kill -15 "$pid"
        return_code=$?
        if [ $return_code -eq 0 ]; then
          rm "$pidfile"
        fi
      else
        echo "Nexus not running with pid $pid removing pid file $pidfile"
        rm "$pidfile"
        return_code=1
      fi
    else
      echo "PID file not found"
      return_code=1
    fi
  ;;
  restart|force-reload)
    $0 stop
    $0 start
  ;;
  status)
    # no-op
    return_code=0
  ;;
  run|run-redirect)
    cd "$PARENT" || exit 1
    exec "$app_java_home/bin/java" -server '-Dnexus.installer.type=linux-x86-64' $INSTALL4J_ADD_VM_PARAMS -jar "$bootJar"
    return_code=$?
    cd "$original_dir" || exit 1
  ;;
  *)
    echo "Usage: $0 {start|stop|run|run-redirect|status|restart|force-reload}"
    exit 1
  ;;
esac

exit $return_code
