#!/bin/sh

# Possible installation states:
# 1. Re-installation with previous DSM 7-based installation
# 2. Installation with possible DSM 7-based installation in stasis.
# 3. New install

Error=0

# 1. Reinstallation with previous DSM 7-based TwonkyServer in statis
if [    -d "$SYNOPKG_PKGDEST_VOL/@apphome/$SYNOPKG_PKGNAME/Twonky Server" ] && \
   [ "$(ls "$SYNOPKG_PKGDEST_VOL/@apphome/$SYNOPKG_PKGNAME/Twonky Server")" != "" ]; then

  # ok
  exit 0
fi

# 2. Installation with previous DSM-7 installation somewhere in statis?
for i in /volume*
do
  if [    -d "$i/@apphome/$SYNOPKG_PKGNAME/Twonky Server" ] && \
     [ "$(ls "$i/@apphome/$SYNOPKG_PKGNAME/Twonky Server")" != "" ] && \
     [ "$SYNOPKG_PKGDEST_VOL" != "$i" ]; then

    ExistingVolume="$(echo $i | sed -e 's;/volume;Volume ;')"

    # Instructions for user
    {
      echo "<br><strong style='color:red'>ERROR:   "
      echo "Found an existing Twonky Server installation on $ExistingVolume.</strong><br><br>"
      echo "<b>Select $ExistingVolume as the installation volume and try again.</b>"
    } > $SYNOPKG_TEMP_LOGFILE

    Error=1
  fi
done

# Exit if error and show user
[ $Error -gt 0 ] && exit $Error

# 3. Default condition: New Installation  (No existing installation anywhere)
rm -f "$SYNOPKG_TEMP_LOGFILE"
exit 0
