#!/bin/sh
#
# Usage:  $0 "Message-code" "Message-arg-1" "Message-arg-2" ....
# Lang:   English

# Message code  (alphabetical sort by name)
case "$1" in


  # Have ACL but still can't read "Application Support" or is empty
  "M-ACL-Failure-Get-Help")
    {
      echo "<strong>ERROR:</strong>Cannot read '$SYNOPKG_PKGDEST_VOL/Plex/Library/Application Support'<br>"
      echo "<br>"
      echo "Please seek additional help in our forums to resolve this problem."
    } >> $SYNOPKG_TEMP_LOGFILE
    ;;

# Preinstall/Postinstall - DSM 6 upgrade - Do not have Full Control of the shared fold
  "M-ACL-Failure-Plex-Share")
    {
      echo "<br><p style='color:red'>ERROR: <strong>PlexMediaServer</strong> does not have <strong>Full Control</strong> of existing <strong>Plex</strong> share.</p><br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Postinstall - DSM 6 upgrade - Check if ready - still don't not have full control of all subdirs
  "M-ACL-Failure-Plex-Subdirs")
    {
      echo "<br><p style='color:red'>ERROR: <strong>PlexMediaServer</strong> does not have full control of all subfolders in <strong>Plex</strong> share.</p><br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Pre/Post install remedy steps
  "M-ACL-Procedure")
    {
      echo "Please perform the following steps:<br><br>"
      echo " 1. Cancel this installation. Uninstall the package.<br>"
      echo " 2. Open <strong>Control Panel</strong> - <strong>Shared Folder</strong><br>"
      echo " 3. Select the existing <strong>Plex</strong> share and click <strong>EDIT</strong><br>"
      echo " 4. Click the <strong>Permissions</strong> tab<br>"
      echo " 5. Change the dropdown from <strong>Local Users</strong> to <strong>System internal user</strong><br>"
      echo " 6. For user '<strong>PlexMediaServer</strong>' click the <strong>Custom</strong> checkbox<br>"
      echo " 7. Check the <strong>Administration, Read, & Write</strong> group checkboxes for the <strong>PlexMediaServer</strong> user<br>"
      echo " 8. Click <strong>Done</strong> to close the permission setting.<br>"
      echo "    Confirm 'PlexMediaServer' is assigned 'Full Control' permission<br>"
      echo " 9. Click <strong>Apply to this folder, sub-folders, and files <p style='color:red'>(IMPORTANT)</p></strong><br>"
      echo "10. Click <strong>Save</strong> to confirm the new permissions."
      echo "    Expect 2 minutes / 1000 items indexed to complete.<br>"
      echo "11. Reinstall the Plex Media Server package<br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Postinstall - DSM 6 upgrade - Final pre-migration check - Something interrupted / interfered with ACL setting by FileStation
  "M-Cannot-Migrate-ACL-Incomplete")
    {
      echo "<strong>PlexMediaServer cannot migrate your existing server information.</strong><br>"
      echo "<br><p style='color:red'>ERROR: <strong>PlexMediaServer</strong> does not yet have <strong>Full Control</strong> of all subfolders in <strong>Plex</strong> share.</p><br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Claim with token but no token string provided.
  "M-Claim-No-Token-Provided")
    {
      echo "<br><strong>ERROR: Claim Token not provided.  Please try again.</strong>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Could not get Claim token
  "M-Could-Not-Claim")
    {
      echo "<strong>ERROR:  Could not complete dialog with Plex.tv.  Unable to claim.</strong><br><br>"
      echo "Either installation time was too long or difficulties communicating with Plex.tv exist.<br><br>"
      echo "Please try again or seek assistance in our Support Forum.<br>"
    } > $SYNOPKG_TEMP_LOGFILE
    ;;

  # Claim Successful
  "M-Claim-Successful")
    {
      echo "<br>PlexMediaServer successfully claimed by \"$2\"."
    } > $SYNOPKG_TEMP_LOGFILE
    ;;

  # Unexpected error(s) changing ownership of files
  "M-Error-Changing-Ownership")
    {
      echo "<strong>ERROR:  Unexpected errors changing ownership.</strong><br>"
      echo "<br><strong>Migration has failed. Processing stops.</strong><br>"
      echo "Please see the migration log, '$2', for details<br>"
      echo "Please consult our forums for assistance.<br>"
    }  >> $SYNOPKG_TEMP_LOGFILE
    ;;

  # Could not fix fixed path subtitle links
  "M-Error-Updating-Subtitles")
    {
      echo "<strong>ERROR:  Unexpected errors updating subtitle links.</strong><br>"
      echo "<br><strong>Migration has failed. Processing stops.</strong><br>"
      echo "Please see the migration log, '$2', for details<br>"
      echo "Please consult our forums for assistance.<br>"
    }  >> $SYNOPKG_TEMP_LOGFILE
    ;;

  #  Post Install/Upgrade/Migration - Installation complete media permissions reminder
  "M-Install-Complete-Check-Permissions")           # (dsm-migration-tool)
    {
      echo "<br><strong><p style='color:blue'><big><b>Installation Successful!</big></p>"
      echo "<br><p style='color:blue'>Note: If Plex cannot access your media, verify user <strong>PlexMediaServer</strong> is granted permission in <strong>Control Panel</strong>.</p><br>"
      echo "Set access to your media share(s) by performing the following steps:<br><br>"
      echo "1. Open <strong>Control Panel</strong> and select <strong>Shared Folder</strong><br>"
      echo "2. Select the share which contains your media and click <strong>Edit</strong><br>"
      echo "3. Click the <strong>Permissions</strong> tab<br>"
      echo "4. Change the dropdown from <strong>Local Users</strong> to <strong>System internal user</strong><br>"
      echo "5. Check the <strong>Read/Write</strong> checkbox for the <strong>PlexMediaServer</strong> user<br>"
      echo "6. Click <strong>Save</strong> to confirm the new permissions<br>"
      echo "7. Repeat steps 2-6 for each share you want Plex Media Server to access<br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Postinstall/Postupgrade - LogPathname cannot be written to
  "M-LogPath-No-Write-Access")
    {
      echo "<br><strong>ERROR:  Cannot write to '$2'. Check pathname & permissions.  Please try again.</strong>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  # Postinstall/PostUpgrade - Log option selected but no path given
  "M-LogPath-No-Path-Specified")
    {
      echo "<br><strong>ERROR: Log option selected but no path given. Please try again.</strong>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

  #  Migration - Cleaning completed
  "M-Migration-Cleaned")
    echo "Cleaned '$2'.<br>"  >> $SYNOPKG_TEMP_LOGFILE
    ;;

  # Migration-tool MigrationStarted    # Start user error log (in case it's needed)
  "M-Migration-Started")
    {
      echo "<strong>Plex Media Server - DSM 6 -> 7 upgrade on $(date).</strong><br><br>"
    }  >> $SYNOPKG_TEMP_LOGFILE
    ;;

  # Postinstall - Migration successful
  "M-Migration-Successful")
    {
      echo "Migrated existing Plex Media Server installation from '$2'.<br>"
      echo "The migration log is available for review: $SYNOPKG_PKGDEST_VOL/Plex/Migration.log<br>"
    } >> $SYNOPKG_TEMP_LOGFILE

    echo "=== $(date) === Completed:  Migrate Plex Media Server" >>"$SYNOPKG_PKGDEST_VOL/Plex/Migration.log"
    ;;

  # Unexpected errror (Linux error code) during migration
 "M-Migration-System-Error")
    {
      echo "ERROR: Unable to migrate existing \"$2\" to DSM 7.  System error $3."
      echo "       Migration log file: "$4""
    } >> $SYNOPKG_TEMP_LOGFILE
    ;;

  # Preinstall/Postinstall - DSM 6 upgrade - Do not have Full Control of the shared fold
  "M-Preinstall-Check-Failure")
    {
      echo "<strong>ERROR: Preinstallation checks have failed.</strong><br>"
      echo "<br><p style='color:red'>ERROR: <strong>PlexMediaServer</strong> does not have <strong>Full Control</strong> of existing <strong>Plex</strong> share.</p><br>"
    } >> "$SYNOPKG_TEMP_LOGFILE"
    ;;

# - Preinstall
  # Upgradeable DSM 6 installation but is not on SYNOPKG_DEST_VOL  ($2 = ExistingVolume, $3 = PKGDEST_VOL)
  "M-Preinstall-Migrate-Wrong-Volume")
    {
      echo "<br><strong style='color:red'>ERROR:  Incorrect installation volume.</strong><br><br>"
      echo "Found an upgradable Plex Media Server installation on '$2'.<br><br>"
      echo "<strong>Select '$2' as the installation volume and try again<br>"
      echo "or delete it from the 'Plex' share to start over.</strong>"
    } >> $SYNOPKG_TEMP_LOGFILE
    ;;

  # Existing DSM 7 installation in statis but is not on SYNOPKG_DEST_VOL
  "M-Preinstall-Wrong-Volume")
    {
      echo "<br><strong style='color:red'>ERROR:  Incorrect installation volume.</strong><br><br>"
      echo "Found an existing Plex Media Server installation on $2.<br><br>"
      echo "<strong>Select $2 as the installation volume and try again.</strong>"
    } > $SYNOPKG_TEMP_LOGFILE
    ;;

  # No error text for message
  *)
    {
      echo "Internal package error.  No text for error code '$1'.  Please report this error."
    } >> $SYNOPKG_TEMP_LOGFILE
    ;;

esac
