Unverified Commit 2ce0970d authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Refactor

parent 139328df
Loading
Loading
Loading
Loading
+56 −44
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -u

# Functions
VERSION="4"
HEADER="VirtualDSM Agent:"

function checkNMI {

  local nmi
  nmi=$(cat /proc/interrupts | grep NMI | sed 's/[^1-9]*//g')

  if [ "$nmi" != "" ]; then

    echo "$HEADER Received shutdown request through NMI.."

    /usr/syno/sbin/synoshutdown -s > /dev/null
    exit

  fi

}
# Functions

finish() {

@@ -27,41 +13,22 @@ finish() {

}

trap finish SIGINT SIGTERM
function checkNMI {

ts=$(date +%s%N)
checkNMI
  local nmi
  nmi=$(cat /proc/interrupts | grep NMI | sed 's/[^1-9]*//g')

VERSION="4"
echo "$HEADER starting v$VERSION.."
  if [ "$nmi" != "" ]; then

# Install packages 
    echo "$HEADER Received shutdown request through NMI.."

first_run=false
    /usr/syno/sbin/synoshutdown -s > /dev/null
    finish

for filename in /usr/local/packages/*.spk; do
  if [ -f "$filename" ]; then
    first_run=true
  fi
done

if [ "$first_run" = true ]; then
  for filename in /usr/local/packages/*.spk; do
    if [ -f "$filename" ]; then

      BASE=$(basename "$filename" .spk)
      BASE="${BASE%%-*}"

      echo "$HEADER Installing package ${BASE}.."

      /usr/syno/bin/synopkg install "$filename" > /dev/null
      /usr/syno/bin/synopkg start "$BASE" > /dev/null &

      rm "$filename"
}

    fi
  done
else
function downloadUpdate {

  TMP="/tmp/agent.sh"
  rm -f "${TMP}"
@@ -89,6 +56,51 @@ else
  else
    echo "$HEADER update error, curl error: $?"
  fi
}

function installPackages {

  for filename in /usr/local/packages/*.spk; do
    if [ -f "$filename" ]; then

      BASE=$(basename "$filename" .spk)
      BASE="${BASE%%-*}"

      echo "$HEADER Installing package ${BASE}.."

      /usr/syno/bin/synopkg install "$filename" > /dev/null
      /usr/syno/bin/synopkg start "$BASE" > /dev/null &

      rm "$filename"

    fi
  done
}

trap finish SIGINT SIGTERM

ts=$(date +%s%N)
checkNMI

echo "$HEADER started v$VERSION.."

# Install packages 

first_run=false

for filename in /usr/local/packages/*.spk; do
  if [ -f "$filename" ]; then
    first_run=true
  fi
done

if [ "$first_run" = true ]; then
  
  installPackages

else

  downloadUpdate
  
fi