#!/bin/sh
# Copyright (C) 2000-2016 Synology Inc. All rights reserved.

### This script will be execute when package installed and upgraded.
### Actions before package installed.
### ex. check environment.

res=$(ps -e | grep SyncoveryCL | grep ./ 2>&1)

/usr/syno/bin/synologset1 sys info 0x11100000 "[Syncovery] Status before installation: $res"

echo "[Syncovery] Status before installation:" >>"$SYNOPKG_PKGDEST"/log.txt
echo "$res" >>"$SYNOPKG_PKGDEST"/log.txt

if [ -d /var/packages/SyncoveryGuardian ]
 then
echo Syncovery Guardian needs to be uninstalled first >>"$SYNOPKG_PKGDEST"/log.txt 
echo Syncovery Guardian needs to be uninstalled first. It is included in the new Syncovery and no longer a separate package. >>"$SYNOPKG_TEMP_LOGFILE"
/usr/syno/bin/synologset1 sys info 0x11100000 "[Syncovery] Syncovery Guardian needs to be uninstalled first."

/usr/syno/bin/synodsmnotify @administrators "Uninstall Guardian First" "Syncovery Guardian needs to be uninstalled first. It is included in the new Syncovery and no longer a separate package."

echo "$res" >>"$SYNOPKG_PKGDEST"/log.txt
exit 1
fi


   if [ "$(pidof SyncoveryGuardian)" ]
   then
      echo "preinst: Guardian Running" >>"$SYNOPKG_PKGDEST"/log.txt
#      killall SyncoveryGuardian >>"$SYNOPKG_PKGDEST"/log.txt 2>&1

      res=$(/var/packages/SyncoveryGuardian/scripts/start-stop-status stop >>"$SYNOPKG_PKGDEST"/log.txt 2>&1)

      echo "Guardian Stopped Hopefully" >>"$SYNOPKG_PKGDEST"/log.txt
      echo "Guardian" >/tmp/syncoveryguardianstatus 2>"$SYNOPKG_PKGDEST"/log.txt
   else
      echo "preinst: Guardian Not Running" >>"$SYNOPKG_PKGDEST"/log.txt
      rm /tmp/syncoveryguardianstatus  >>"$SYNOPKG_PKGDEST"/log.txt 2>&1
   fi

exit 0
