<?xml version='1.0' standalone='yes'?>

<!DOCTYPE PLUGIN [
<!ENTITY name      "dynamix.s3.sleep">
<!ENTITY author    "Bergware">
<!ENTITY version   "2016.08.26">
<!ENTITY launch    "Settings/SleepSettings">
<!ENTITY pluginURL "https://raw.githubusercontent.com/rgls/dynamix/master/unRAIDv6/&name;.plg">
<!ENTITY source    "/boot/config/plugins/&name;/&name;">
<!ENTITY MD5       "f5a55e1b831964482a5688d2c6472ac0">
]>

<PLUGIN name="&name;" author="&author;" version="&version;" launch="&launch;" pluginURL="&pluginURL;" min="6.1.9">

<CHANGES>
##&name;

###&version;
- fixed folders and files permissions
- added minimum unRAID version 6.1.9

###2016.8.19
- updated s3_sleep script to version 3.0.3 (changed HDD activity check to allow RAID controllers, courtesy Michael A.)
- changed sleep icon

###2016.06.22
- updated s3_sleep script to version 3.0.2 (include SCSI attached disks in array list)

###2016.06.20
- fixed regression error when running unRAID v6.1.9

###2016.06.19
- changed "System in Sleep mode" message to middle of footer (showNotice)

###2015.12.14
- added unRAID version check. Only version 6.1 or higher is allowed
- updated consistency of PLG file

###2015.12.13
- changed .txz file naming to support consistent package upgrading
- updated rc.s3_sleep script (start | stop | restart)

###2015.09.23
- correction in PLG file

###2015.09.16
- corrected preRun and postRun scripts calling with immediate sleep
- added debug information with immediate sleep
- updated s3_sleep script to version 3.0.1

###2015.08.18b
- corrected script location installation in PLG file

###2015.08.18a
- moved location of pre-sleep and post-sleep commands to scripts folder (please re-apply settings)

###2015.08.18
- corrected regression error is sleep button

###2015.08.13
- adhere new security policy in unRAID v6.1 (please reboot after upgrade)

###2015.08.08
- make plugin compatible with unRAID version 6.0 and 6.1

###2015.06.27
- introduced new logo (thanks Zonediver)
- removed utilities page (now standard included with Dynamix webGui)
- removed Reset button (now controlled by Dynamix webGui)
- changed behavior of Default button (immediate reset)
- changed behavior of enable/disable fields
- allowed pre- and post- commands when Sleep button is pressed

###2015.04.28
- added Reset button on settings page
- fixed enabling/disabling of buttons

###2015.04.11
- included online help information

###2015.02.14
- fixed s3_sleep startup after system reboot

###2015.01.22
- version update for webGui 2015.01.21

###2014.12.07
- new s3_sleep version 3.0.0
- fixed addition of sleep settings
- fixed unassigned devices

###2014.12.03
- fixed sleep button not added to Array Operation page

###2014.11.28
- initial release for unRAID v6
</CHANGES>

<!--
Copyright 2016, Bergware International
Dynamix S3 Sleep - Manual and automatic sleep mode
-->

<!-- PRE-INSTALL SCRIPT -->
<FILE Run="/bin/bash">
<INLINE>
cfg="/boot/config/plugins/dynamix/dynamix.cfg"

add_key(){
  key=$(echo $2|cut -d'=' -f1)
  section=$(sed -n "/^\[$1\]/,/^\[/p" $cfg)
  if [[ -z $section ]]; then
    echo -e "[$1]\n$2" >>$cfg
  else
    if [[ $(echo $section|grep "$key="|wc -l) -eq 0 ]]; then
      sed -i "$(sed -n "/^\[$1\]/,/^\[/=" $cfg|tail -1) i $2" $cfg
    fi
  fi
}

# Add sleep settings
if [[ -e $cfg ]]; then
  add_key 'confirm' 'sleep="1"'
  add_key 'display' 'sleep="plugins/&name;/Sleep.php"'
else
  echo -e "[confirm]\nsleep=\"1\"" >$cfg
  echo -e "[display]\nsleep=\"plugins/&name;/Sleep.php\"" >>$cfg
fi

# Stop service
/usr/local/emhttp/plugins/&name;/scripts/rc.s3sleep stop 2>/dev/null

# Remove old 'source' packages
MD5=$(md5sum &source;.txz 2>/dev/null|grep -Po '^\S+')
if [[ $MD5 != &MD5; ]]; then
  rm -f &source;*.txz
fi
</INLINE>
</FILE>

<!-- SOURCE PACKAGE -->
<FILE Name="&source;.txz" Run="upgradepkg --install-new --reinstall">
<URL>https://raw.githubusercontent.com/rgls/dynamix/master/archive/&name;.txz</URL>
<MD5>&MD5;</MD5>
</FILE>

<!-- WORKAROUND -->
<FILE Name="/tmp/start_service" Mode="0770">
<INLINE>
#!/bin/bash
/usr/local/emhttp/plugins/&name;/scripts/rc.s3sleep start
</INLINE>
</FILE>

<!-- POST-INSTALL SCRIPT -->
<FILE Run="/bin/bash">
<INLINE>
cfg=&source;.cfg
if [[ ! -e $cfg ]]; then
  cfg=/usr/local/emhttp/plugins/&name;/default.cfg
fi

# string decoder
urldecode(){
  echo -e "$(sed 's/+/ /g;s/%0D//g;s/%/\\x/g')"
}

# get unRAID key
getkey(){
  if [[ -e /proc/mdcmd ]]; then
    local key=$1
    grep -Po "^$key=\K.*" /proc/mdcmd
  fi
}

# Create preRun and postRun files (if existing)
bash="#!/bin/bash\n"
bin=/usr/local/emhttp/plugins/&name;/scripts
preRun=$(grep -Po '^preRun="\K[^"]+' $cfg)
if [[ -n $preRun ]]; then
  echo "${bash}${preRun}"|urldecode >$bin/preRun
  chmod u+x $bin/preRun
fi
postRun=$(grep -Po '^postRun="\K[^"]+' $cfg)
if [[ -n $postRun ]]; then
  echo "${bash}${postRun}"|urldecode >$bin/postRun
  chmod u+x $bin/postRun
fi

# Start service
enable=$(grep -Po '^service="\K[^"]+' $cfg)
conf=/etc/s3_sleep.conf
if [[ $enable -eq 1 ]]; then
  if [[ ! -e $conf ]]; then
    grep -Po '^options="\K.*' $cfg|sed -n 'G;s:"\n$::;p' >$conf
  fi
  if [[ $(getkey mdState) == STARTED ]]; then
    at -M -f /tmp/start_service now 2>/dev/null
  fi
fi
rm -f /tmp/start_service

# Update file access mode
chmod 644 $(awk '/\.(htm|php|js|page|css|png|plg|gif|cfg|md)$/ {print "/"$0}' /var/log/packages/&name;)

echo ""
echo "-----------------------------------------------------------"
echo " Plugin &name; is installed."
echo " This plugin requires Dynamix webGui to operate"
echo " Copyright 2016, Bergware International"
echo " Version: &version;"
echo "-----------------------------------------------------------"
echo ""
</INLINE>
</FILE>

<!-- REMOVE SCRIPT -->
<FILE Run="/bin/bash" Method="remove">
<INLINE>
# Delete sleep settings
sed -i '/^sleep=/d' /boot/config/plugins/dynamix/dynamix.cfg

# Stop service
/usr/local/emhttp/plugins/&name;/scripts/rc.s3sleep stop

# Remove plugin related files
rm -rf /boot/config/plugins/&name;

# Uninstall the 'source' package
removepkg &name;
</INLINE>
</FILE>

</PLUGIN>