#!/bin/bash

#To install run 'mkdir -p /usr/local/nebula && wget -O /usr/local/nebula/nebula.sh https://as-download:i1n638rNeU2X@pkg.dev.snaju.com/misse/release/nebula.sh && bash /usr/local/nebula/nebula.sh install'

CMD=$1
USER="as-download"
PASS="i1n638rNeU2X"
PIDFILE="/usr/local/nebula/nebula.pid"
ENDPOINT="$USER:$PASS@pkg.dev.snaju.com/rac/release"

# Functions to configure different services into the system.d for auto boot

system_d_nebula() {
  echo "Setting Up System.d Service..."
  cat >"/etc/systemd/system/nebula.service" <<-EOM
[Unit]
Description=Snaju Nebula SDK.space Service

[Service]
User=root
WorkingDirectory=/usr/local/nebula
ExecStart=/bin/nebula start
ExecStop=/bin/nebula stop
Restart=always
RestartSec=3
RemainAfterExit=yes
StandardOutput=syslog

[Install]
WantedBy=multi-user.target
EOM

  systemctl daemon-reload
  systemctl enable nebula.service
}

system_d_uft() {

  cat >"/opt/nebula/uft/start.sh" <<-EOM
#!/bin/bash
until /opt/nebula/uft/UFT/uft_server --local-host=0.0.0.0 --local-port=9000 --timeout=1800; do
    echo "UFT Server crashed with exit code $?.  Respawning.." >&2
    sleep 1
done
EOM

  chmod ugo+rwx /opt/nebula/uft/start.sh

  echo "Setting up System.d service for UFT..."
  cat >"/etc/systemd/system/uft.service" <<-EOM
[Unit]
Description=Snaju LLC UFT Service

[Service]
User=root
WorkingDirectory=/opt/nebula/uft/UFT
ExecStart=/opt/nebula/uft/start.sh
Restart=always
RemainAfterExit=yes
StandardOutput=syslog

[Install]
WantedBy=multi-user.target
EOM

  systemctl daemon-reload
  systemctl enable nebula.service
}

log_setup() {
  echo "Setting Up $1 Logging..."

  mkdir -p /var/log/nebula

  cat >"/etc/rsyslog.d/$1.conf" <<-EOM
if \$programname == '$1' then /var/log/nebula/$1.log
& stop
EOM

  touch "/var/log/nebula/$1.log"

  #  chown syslog:adm /var/log/nebula/$1.log
  chmod ugo+rwx /var/log/nebula/$1.log

  # Setup Log Rotate
  echo "Setting up log rotate for daily"
  cat >"/etc/logrotate.d/$1" <<-EOM

/var/log/nebula/$1.log {
    rotate 5
    daily
    create
    missingok
    delaycompress
    compress
}

EOM

  logrotate -v /etc/logrotate.d/$1
  systemctl restart rsyslog
  systemctl restart logrotate
}

#system_d_config_pin() {
#  echo "Starting UP Configpin.sh Service"
#  cat >"/lib/systemd/system/config_pins.service" <<-EOM
#[Unit]
#Description=Enable pin configuration at startup
#After=generic-board-startup.service
#
#[Service]
#Type=simple
#ExecStart=/opt/nebula/msc_python/configuration/config_pins.sh
#
#[Install]
#WantedBy=multi-user.target
#EOM
#  systemctl daemon-reload
#  systemctl enable config_pins.service
#
#  echo "Starting UP Configpin PY Service"
#  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
#[Unit]
#Description=Enable pin configuration at startup
#After=config_pins.service
#
#[Service]
#Type=simple
#ExecStart=/usr/bin/python3 /opt/nebula/msc_python/configuration/config_pins.py
#
#[Install]
#WantedBy=multi-user.target
#EOM
#  systemctl daemon-reload
#  systemctl enable config_pins_py.service
#}

system_d_python_mpdb() {
  # Python Main
  echo "Setting up PY Service for MPDB Board"
  cat >"/lib/systemd/system/misse_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins_py.service

[Service]
User=root
ExecStart=/bin/bash /opt/nebula/dual_computer_board/start.sh
Restart=always
RestartSec=3
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable misse_py.service

  # Config Pin .sh

  echo "Starting UP Configpin.sh Service"
  cat >"/lib/systemd/system/config_pins.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service

[Service]
User=root
ExecStart=/opt/nebula/dual_computer_board/configuration/config_pins.sh

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins.service

  # Config Pin Py

  echo "Starting UP Configpin PY Service"
  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/dual_computer_board/configuration/config_pins.py

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins_py.service

}

system_d_python_msc() {
  echo "Setting up PY Service for MSC Board"

  cat >"/opt/nebula/msc_python/start.sh" <<-EOM
#!/bin/bash
until /opt/nebula/msc_python/mscV2; do
    echo "Python Server crashed with exit code $?.  Respawning.." >&2
    sleep 1
done
EOM

  chmod ugo+x /opt/nebula/msc_python/mscV2
  chmod ugo+rwx /opt/nebula/msc_python/start.sh
  chmod ugo+x /opt/nebula/msc_python/configuration/config_pins.sh

  cat >"/lib/systemd/system/misse_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins_py.service

[Service]
User=root
ExecStart=/bin/bash /opt/nebula/msc_python/start.sh
Restart=always
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable misse_py.service

  echo "Starting UP Configpin.sh Service"
  cat >"/lib/systemd/system/config_pins.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service

[Service]
User=root
ExecStart=/opt/nebula/msc_python/configuration/config_pins.sh

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins.service

  echo "Starting UP Configpin PY Service"
  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/msc_python/configuration/config_pins.py

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins_py.service
}

system_d_python_exp() {
  echo "Setting up PY Service for EXP Board"
  cat >"/lib/systemd/system/misse_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins_py.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/experimental_board/main.py
Restart=always
RestartSec=3
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable misse_py.service

  echo "Starting UP Configpin.sh Service"
  cat >"/lib/systemd/system/config_pins.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service

[Service]
User=root
ExecStart=/opt/nebula/experimental_board/configuration/config_pins.sh

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins.service

  echo "Starting UP Configpin PY Service"
  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/experimental_board/configuration/config_pins.py

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins_py.service
}

system_d_python_rac() {

  echo "Generating Start Script"
  cat >"/opt/nebula/rac-python/start.sh" <<-EOM
#!/bin/bash
until /opt/nebula/rac-python/main; do
    echo "Python Server crashed with exit code $?.  Respawning.." >&2
    sleep 1
done
EOM

  chmod ugo+rwx /opt/nebula/rac-python/start.sh

  echo "Setting up PY Service for RAC Board"
  cat >"/lib/systemd/system/rac_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins_py.service

[Service]
User=root
ExecStart=/opt/nebula/rac-python/main
Restart=always
RestartSec=3
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable rac_py.service

  echo "Starting UP Configpin.sh Service"
  cat >"/lib/systemd/system/config_pins.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service

[Service]
User=root
ExecStart=/opt/nebula/rac-python/configuration/config_pins.sh

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins.service

  echo "Starting UP Configpin PY Service"
  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/rac-python/configuration/config_pins.py

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins_py.service
}

system_d_python_msb() {
  echo "Setting up PY Service for MSB Board"
  cat >"/lib/systemd/system/misse_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins_py.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/dual_computer_board/msb_mainv2.py
Restart=always
RestartSec=3
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable misse_py.service

  echo "Starting UP Configpin.sh Service"
  cat >"/lib/systemd/system/config_pins.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=generic-board-startup.service

[Service]
User=root
ExecStart=/opt/nebula/dual_computer_board/configuration/config_pins.sh

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins.service

  echo "Starting UP Configpin PY Service"
  cat >"/lib/systemd/system/config_pins_py.service" <<-EOM
[Unit]
Description=Enable pin configuration at startup
After=config_pins.service

[Service]
User=root
ExecStart=/usr/bin/python3 /opt/nebula/dual_computer_board/configuration/config_pins.py

[Install]
WantedBy=multi-user.target
EOM
  systemctl daemon-reload
  systemctl enable config_pins_py.service
}

system_d_camera() {
    echo "Generating Start Script"

  cat >"/opt/nebula/camera/start.sh" <<-EOM
#!/bin/bash
until /opt/nebula/camera/Camera; do
    echo "Camera Server crashed with exit code $?.  Respawning.." >&2
    sleep 1
done
EOM

  chmod ugo+rwx /opt/nebula/camera/start.sh

  echo "Starting Up Camera Service"
  cat >"/lib/systemd/system/camera.service" <<-EOM
[Unit]
Description=Enable camera service at startup
After=config_pins_py.service
Before=misse_py.service

[Service]
User=root
WorkingDirectory=/opt/nebula/camera
ExecStart=/opt/nebula/camera/start.sh
Restart=always
RemainAfterExit=yes
StandardOutput=syslog

[Install]
WantedBy=multi-user.target
EOM

  systemctl daemon-reload
  systemctl enable camera.service
}

system_d_mil1553() {
  echo "Setting up the Mil1553 Service"
  cat >"/lib/systemd/system/mil1553.service" <<-EOM
[Unit]
Description=Base JADT Mil1443 Service
Before=nebula.service
After=config_pins_py.service

[Service]
User=root
ExecStart=/opt/nebula/jadt/start.sh
Restart=always
RemainAfterExit=yes
Nice=+12

[Install]
WantedBy=multi-user.target
EOM

  systemctl daemon-reload
  systemctl enable mil1553.service
}

service_msc() {
  system_d_nebula
  system_d_camera
  #  system_d_config_pin
  system_d_python_msc

  log_setup "nebula"
  log_setup "camera"
  log_setup "py"
}

service_mpdb() {
  system_d_nebula
  system_d_mil1553
  #  system_d_config_pin
  system_d_python_mpdb

  log_setup "nebula"
  #  log_setup camera
  log_setup "py"
}

service_msb() {
  system_d_nebula
  #  system_d_config_pin
  system_d_python_msb

  log_setup "nebula"
  #  log_setup camera
  log_setup "py"
}

py_update() {
  mkdir -p /tmp/py_installer
  mkdir -p /opt/nebula/rac-python

  wget -O /tmp/py_installer/py.zip http://$ENDPOINT/py.zip
  unzip -o /tmp/py_installer/py.zip -d /tmp/py_installer

  cp /tmp/py_installer/main /opt/nebula/rac-python/.
  cp -R /tmp/py_installer/configuration /opt/nebula/rac-python/.

  chmod ugo+rwx /opt/nebula/rac-python/main
  chmod ugo+rwx /opt/nebula/rac-python/configuration/config_pins.sh
  chmod ugo+rwx /opt/nebula/rac-python/configuration/config_pins.py
}

cam_update() {
  mkdir -p /tmp/cam_installer
  mkdir -p /opt/nebula/camera
  mkdir -p /opt/nebula/camera/download

  wget -O /tmp/cam_installer/camera.zip http://$ENDPOINT/camera.zip
  unzip -o /tmp/cam_installer/camera.zip -d /tmp/cam_installer

  cp -R /tmp/cam_installer/* /opt/nebula/camera/.

  chmod ugo+rwx /opt/nebula/camera/Camera
  chmod ugo+rwx /opt/nebula/camera/CameraCLI
}

cmd_nebula_start() {
  echo "Looking for startup script..."
  if [ -f "/usr/local/nebula/boot.sh" ]; then
    echo "Found boot.sh file"
    bash /usr/local/nebula/boot.sh
    echo "Finished running the boot.sh"
  else
    echo "boot.sh not found. Skipping."
  fi

  mkdir -p /var/log/nebula

  echo "Starting Nebula..."
  java -jar -Xmx384M -Xms384M -XX:-UseGCOverheadLimit -Djava.library.path=/usr/lib/jni -Djavacan.native.arch=armv7a /usr/local/nebula/sdk-space-core.jar
  PID=""
  SECONDS=0
  while [ -z "$PID" ]; do
    if [ -f $PIDFILE ]; then
      PID=$(cat $PIDFILE)
      echo "SUCCESS: Nebula Started with PID: $PID"
      exit 0
    else
      duration=$SECONDS
      if [ "$(($duration / 60))" -ge "1" ]; then
        echo "FATAL ERROR: Failed to start Nebula. PID=0"
        echo "Exit with code 1"
        exit 1
      fi
    fi
  done
}

case $CMD in

start)
  if [ -f $PIDFILE ]; then
    PID=$(cat /usr/local/nebula/nebula.pid)

    if kill -s 0 $PID; then
      echo "FAILURE: Nebula is already running..."
      exit 0
    else
      echo "PID not running... removing stale PID"
      rm -f /usr/local/nebula/nebula.pid
      cmd_nebula_start
    fi
  else
    cmd_nebula_start
  fi
  ;;

astart)
  /usr/local/nebula/nebula.sh start
  /usr/local/nebula/nebula.sh attach
  ;;

stop)
  echo "Stopping Nebula..."
  if [ -f $PIDFILE ]; then

    echo "Looking for shutdown script..."
    if [ -f "/usr/local/nebula/shutdown.sh" ]; then
      echo "Found shutdown.sh file"
      bash /usr/local/nebula/shutdown.sh
      echo "Finished running the shutdown.sh"
    else
      echo "shutdown.sh not found. Skipping."
    fi

    PID=$(cat /usr/local/nebula/nebula.pid)
    echo "Killing PID: $PID"
    kill $PID
    echo "SUCCESS: Nebula has been stopped"
    exit 0
  else
    echo "FAILURE: Nebula is not running"
    exit 1
  fi
  ;;

rupdate)
  echo "Stopping Nebula..."
  /usr/local/nebula/nebula.sh stop

  echo "Updating Nebula..."
  /usr/local/nebula/nebula.sh update

  echo "Restarting Nebula..."
  /usr/local/nebula/nebula.sh start
  ;;

restart)
  echo "Restarting Nebula..."
  /usr/local/nebula/nebula.sh stop
  PIDN=""
  while [ -z $PIDN ]; do
    if [ ! -f $PIDFILE ]; then
      PIDN="false"
    fi
  done
  /usr/local/nebula/nebula.sh start
  ;;

status)
  if [ -f $PIDFILE ]; then
    PID=$(cat /usr/local/nebula/nebula.pid)
    echo "Nebula is Running with PID $PID"
  else
    echo "Nebula is off"
  fi
  ;;

kill)
  echo "Killing Nebula with force"

  echo "Looking for shutdown script..."
  if [ -f "/usr/local/nebula/shutdown.sh" ]; then
    echo "Found shutdown.sh file"
    bash /usr/local/nebula/shutdown.sh
    echo "Finished running the shutdown.sh"
  else
    echo "shutdown.sh not found. Skipping."
  fi

  if [ -f $PIDFILE ]; then
    PID=$(cat /usr/local/nebula/nebula.pid)
    echo "kill -9 $PID"
    kill -9 $PID
    echo "Nebula Killed with -9"
    rm -f /usr/local/nebula/nebula.pid
    echo "Removed nebula.pid"
  else
    echo "Nebula is not running..."
  fi

  ;;

fix)
  echo "Fixing Nebula Permissions"
  chown -R nebula.debian /usr/local/nebula
  chmod -R ug+rw /usr/local/nebula
  ;;

pysetup)
  py_update
  ;;

camsetup)
  cam_update
  ;;

install)
  echo "Setting Up Dependencies..."

  echo "Adding adaoptopenjdk debian repos..."
  wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
  echo "deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb buster main" | sudo tee /etc/apt/sources.list.d/adoptopenjdk.list

  echo "Upading Package List..."
  apt update

  echo "Installing OpenJDK 8"
  apt-get install -y zip unzip adoptopenjdk-8-hotspot

  echo "Making folder structures..."
  mkdir -p /usr/local/nebula
  mkdir -p /usr/local/nebula/lib
  mkdir -p /usr/local/nebula/bin
  mkdir -p /opt/nebula
  mkdir -p /tmp/nebula
  mkdir -p /var/nebula
  mkdir -p /opt/nebula/camera

  echo "#!/bin/bash" >/usr/local/nebula/boot.sh
  echo "#!/bin/bash" >/usr/local/nebula/shutdown.sh

  echo "Making nebula command..."
  ln -s /usr/local/nebula/nebula.sh /bin/nebula

  echo "Downloading Nebula..."
  wget -O /usr/local/nebula/sdk-space-core.jar http://$ENDPOINT/sdk-space-core.jar

  echo "Downloading Libraries..."
  wget -O /usr/local/nebula/lib.zip http://$ENDPOINT/lib.zip
  unzip /usr/local/nebula/lib.zip -d /usr/local/nebula/lib

  echo "Cleaning up downloads..."
  rm -f /usr/local/nebula/lib.zip

  echo "Setting Up Permissions"
  chown -R debian.debian /usr/local/nebula
  chmod -R ug+rw /usr/local/nebula
  chmod ug+x /usr/local/nebula/*.sh
  ;;

plugin)
  # Pull the manifest and show options.
  mkdir -p /usr/local/nebula/plugins
  manifest=$(wget http://$ENDPOINT/manifest -q -O -)
  readarray -t opts <<<"$manifest"

  pluginmode=1
  while [ $pluginmode == 1 ]; do
    clear
    echo "Please select your plugin to install:"
    echo "===================================="
    for i in "${opts[@]}"; do
      parts=(${i//:/ })
      echo "${parts[0]}: ${parts[1]}"
    done
    echo "exit: Exit plugin mode."

    read -p "Plugin Name: " plugin

    if [ $plugin == "exit" ]; then
      pluginmode=0
      break
    fi

    echo "Attempting to install ${plugin}..."

    for i in "${opts[@]}"; do
      parts=(${i//:/ })
      echo "Checking '${parts[0]}' == '${plugin}'"
      if [ "${parts[0]}" == $plugin ]; then
        echo "Installing $PLUGIN_INSTALL into /usr/local/nebula/plugins"
        wget -O /usr/local/nebula/plugins/${parts[1]}.jar http://$ENDPOINT/${parts[1]}.jar

        echo "Setting Up Permissions"
        chown -R debian.debian /usr/local/nebula
        chmod -R ug+rw /usr/local/nebula
        break
      fi
    done

    read -p "Install More? (Y/n): " keepGoing
    if [ $keepGoing == "n" ]; then
      pluginmode=0
      break
    fi
  done
  ;;

update)
  echo "Pulling latest nebula from Snaju Central Repo..."
  echo "Updating Nebula..."
  wget -O /usr/local/nebula/sdk-space-core.jar http://$ENDPOINT/sdk-space-core.jar

  echo "Updating CLI..."
  wget -O /usr/local/nebula/nebula-cli.jar http://$ENDPOINT/nebula-cli.jar

  echo "Updating Libraries..."
  wget -O /usr/local/nebula/lib.zip http://$ENDPOINT/lib.zip

  echo "Removing old libs..."
  rm -rf /usr/local/nebula/lib/*.jar

  echo "Unpacking New Libs..."
  unzip /usr/local/nebula/lib.zip -d /usr/local/nebula/lib

  echo "Updating Plugins..."
  for i in /usr/local/nebula/plugins/*.jar; do
    echo "Updating $i"
    wget -O $i http://$ENDPOINT/$(basename $i)
  done

#  echo "Updating Python..."
#  py_update
  chown -R debian.debian /opt/nebula/rac-python
  chmod -R ug+rw /opt/nebula/rac-python
#
#  echo "Updating Camera Software"
#  cam_update

  echo "Cleaning up downloads..."
  rm -f /usr/local/nebula/lib.zip

  echo "Setting Up Permissions"
  chown -R debian.debian /usr/local/nebula
  chmod -R ug+rw /usr/local/nebula
  chmod ug+x /usr/local/nebula/*.sh
  ;;

cli)
  java -jar /usr/local/nebula/nebula-cli.jar
  ;;

version)
  cat /usr/local/nebula/.version
  exit
  ;;

upsince)
  cat /usr/local/nebula/.lastboot
  exit
  ;;

pull)
  echo "Updating this script..."
  wget -O /usr/local/nebula/nebula.sh http://$ENDPOINT/nebula.sh
  exit
  ;;

config)
  echo "Opening Nebula Config."
  vim /usr/local/nebula/config.json
  ;;

attach)
  echo "Attaching to Nebula..."
  clear
  tail -f /var/log/nebula/nebula.log
  ;;

patch)
  echo "Looking for patch file $2"

  rm -rf /tmp/nebula/patch/$2
  mkdir -p /tmp/nebula/patch/$2

  wget -O /tmp/nebula/patch/$2.zip http://$ENDPOINT/patch/$2.zip

  unzip /tmp/nebula/patch/$2.zip -d /tmp/nebula/patch/$2

  if [ -f /tmp/nebula/patch/$2/run.sh ]; then
    # Found a patch run.sh file, execute that file now.
    echo "Executing the Patch file run.sh for Patch #$2"

    wall "Stopping Nebula Services..."

    service nebula stop
    service misse_py stop
    service camera stop
    service mil1553 stop
    service mrdl stop
    service uft stop

    wall "Executing Nebula Patch: $2"

    /bin/bash /tmp/nebula/patch/$2/run.sh

    service nebula start
    service misse_py start
    service camera start
    service mil1553 start
    service mrdl start
    service uft start

  else
    echo "No run.sh found for the Patch file... skipping..."
  fi
  ;;

service)
  SUBCMD=$2

  case $SUBCMD in
  msc)
    service_msc
    ;;

  mpdb)
    service_mpdb
    ;;

  msb)
    service_msb
    ;;

  nebula)
    system_d_nebula
    ;;

  config_pin)
    system_d_config_pin
    ;;

  adt)
    system_d_mil1553
    ;;

  camera)
    system_d_camera
    ;;

  msc_py)
    system_d_python_msc
    ;;

  mpdb_py)
    system_d_python_mpdb
    ;;

  msb_py)
    system_d_python_msb
    ;;

  uft)
    system_d_uft
    ;;

  exp_py)
    system_d_python_exp
    ;;

  rac_py)
    system_d_python_rac
    ;;

  *)
    echo "nebula service [template]"
    echo "  msc - All MSC Services"
    echo "  mpdb - All MPDB Services"
    echo "  msb - All MSB Services"
    echo "  nebula - Nebula Service"
    echo "  config_pin - Config Pin Service"
    echo "  adt - AltaDT Service"
    echo "  camera - Camera Service"
    echo "  msc_py - MSC PY Service"
    echo "  mpdb_py - MPDB PY Service"
    echo "  msb_py - MSB PY Service"
    echo "  uft - Snaju UFT System (c) 2021"
    echo "  exp_py - EXP PY Service"
    ;;
  esac

  ;;

psync)
  mkdir -p /var/nebula/pictures
  read -p "Target IP to pull photo from: " APID_TARGET

  # Get the file list
  sftp debian@$APID_TARGET:/var/nebula/pictures <<EOF
  ls
  exit
EOF

  read -p "What is the file name you want to pull?: " FILE_NAME

  sftp debian@$APID_TARGET:/var/nebula/pictures <<EOF
  get $FILE_NAME /var/nebula/pictures/.
  exit
EOF

  echo "File $FILE_NAME synced from $APID_TARGET"
  ;;

*)
  echo "nebula [cmd]"
  echo "    start - Start Nebula without Systemd"
  echo "    stop - Stop Nebula without Systemd"
  echo "    kill - Force Kill Nebula without Systemd"
  echo "    restart - Restart Nebula without Systemd"
  echo "    status - Get the Status Of Nebula"
  echo "    fix - Fixes the File Permissions for Nebula"
  echo "    install - Installs Nebula onto system from Snaju Central Repo"
  echo "    plugin - Enter Nebula Plugin Install Mode."
  echo "    update - Will update Nebula from the Snaju Central Repo and all Plugins"
  echo "    pull - Will download the new version of this nebula script..."
  echo "    attach - Attached to the nebula screen session"
  echo "    upsince - See the Java System Time from the last boot"
  echo "    version - See the version information of the current system"
  echo "Nebula was developed by Snaju LLC."
  ;;

esac
