#!/usr/bin/env bash
INSTDIR=$(echo "$2/opt/rkward" | sed 's#//#/#g')
INSTALLER_USER=$(stat -f '%Su' "${HOME}")
APPDIR=$(echo "$2/Applications/rkward.app" | sed 's#//#/#g')
AGENT="org.macports.kf5-rkward"
RKWARDSHARE="${INSTDIR}/share/rkward"
SCRIPTDIR="${RKWARDSHARE}/macOS"
PLISTSOURCE="${SCRIPTDIR}/${AGENT}.plist"
AGENTSDIR="${HOME}/Library/LaunchAgents"
PLISTTARGET="${AGENTSDIR}/${AGENT}.plist"
UNINSTALLER="${SCRIPTDIR}/uninstall.scpt"
UNINSTALLER_DIR="${HOME}/Applications"
UNINSTALLER_USER_APP="${UNINSTALLER_DIR}/RKWard_uninstall.app"
UNINSTALLER_APP="${SCRIPTDIR}/RKWard_uninstall.app"
OLDRKWARD="$(echo "$2/Applications/RKWard" | sed 's#//#/#g')"

"${INSTDIR}/bin/update-mime-database" -V "${INSTDIR}/share/mime"

if [ -d "${RKWARDSHARE}" ] ; then
  mkdir -p "${SCRIPTDIR}"
  # generate uninstall script
    cat <<EOF > "${UNINSTALLER}"
set RKWardDir to "${INSTDIR}"
set RKWardAppDir to "${APPDIR}"
set appInTrash to POSIX path of (path to trash) & "/rkward.app/Contents/Info.plist"
set appInDir to POSIX path of RKWardAppDir & "/Contents/Info.plist"
set haveRKWard to false
set haveTrash to false
set haveApp to false
-- the next variable is not changed by the script itself but during installation
set manualUninstall to false
tell application "System Events"
  if exists file appInTrash then
    set haveTrash to true
  end if
  if exists folder RKWardDir then
    set haveRKWard to true
  end if
  if exists file appInDir then
    set haveApp to true
  end if
end tell
if haveTrash and haveRKWard then
  if manualUninstall and haveApp then
    set addToDialog to " and " & RKWardAppDir
  else
    set addToDialog to ""
  end if
  try
    display dialog "Most of RKWard's installation resides in " & RKWardDir & ". To completely uninstall RKWard, both " & RKWardAppDir & " and " & RKWardDir & " should be deleted.\n\nIf you choose this option, all files below " & RKWardDir & addToDialog & " will removed permanently (not moved to Trash)!\n\nDo you want to irrevocably remove " & RKWardDir & addToDialog & " now?" buttons {"No, keep for now", "Yes, completely remove"} default button "Yes, completely remove" cancel button "No, keep for now" with title "Remove RKWard completely?" with icon caution
    set {buttonReturned} to {button returned of result}
    if buttonReturned is "Yes, completely remove" then
      try
        do shell script "rm -rf \"" & RKWardDir & "\"" with administrator privileges
        set removeRKWardOK to true
      on error
        set removeRKWardOK to false
        display alert "Failed removing " & RKWardDir & "!"
      end try
      if manualUninstall and haveApp and removeRKWardOK then
        try
          do shell script "rm -rf \"${APPDIR}\""
        end try
      end if
      if removeRKWardOK then
        try
          do shell script "rm -rf \"${UNINSTALLER_USER_APP}\""
        end try
        try
          do shell script "rm \"${PLISTTARGET}\""
          do shell script "launchctl remove \"${AGENT}\""
        end try
      end if
    end if
  end try
end if
EOF
  # create a standalone uninstaller from a modified version without the check for trash
  if ! [ -d "${UNINSTALLER_DIR}" ] ; then
    sudo -u "${INSTALLER_USER}" mkdir -p "${UNINSTALLER_DIR}"
  fi
  sed -e "s/set manualUninstall to false/set manualUninstall to true/g" "${UNINSTALLER}" | sed -e "s/if haveTrash and haveRKWard then/if haveRKWard then/g" | osacompile -o "${UNINSTALLER_APP}"
  sudo -u "${INSTALLER_USER}" cp -r "${UNINSTALLER_APP}" "${UNINSTALLER_DIR}"
  # generate watch job
  cat <<EOF > "${PLISTSOURCE}"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>${AGENT}</string>
    <key>WatchPaths</key>
    <array>
      <string>~/.Trash/rkward.app</string>
    </array>
    <key>ProgramArguments</key>
    <array>
      <string>osascript</string>
      <string>${UNINSTALLER}</string>
    </array>
    <key>KeepAlive</key>
    <false/>
  </dict>
</plist>
EOF
  # link watch job
  if [ -f "${PLISTSOURCE}" ] ; then
    if ! [ -d "${AGENTSDIR}" ] ; then
      sudo -u "${INSTALLER_USER}" mkdir -p "${AGENTSDIR}"
    fi
    if ! [ -f "${PLISTTARGET}" ] ; then
      sudo -u "${INSTALLER_USER}" ln -s "${PLISTSOURCE}" "${PLISTTARGET}"
    fi
    if ! [ $(launchctl list | grep ${AGENT}) ] ; then
      sudo -u "${INSTALLER_USER}" launchctl load -w "${PLISTTARGET}"
    fi
  fi
  # finally remove the old RKWard folder if it's still present
  if [ -d "${OLDRKWARD}" ] ; then
    mv "${OLDRKWARD}" "${HOME}/.Trash/"
  fi
fi
exit 0
