#!/bin/sh
# For useful installer-specific environmental variables see
# http://developer.apple.com/documentation/DeveloperTools/Conceptual/SoftwareDistribution/Install_Operations/chapter_8_section_3.html

function create_directory
{
   if [ ! -e "$1" ] ; then
      # -p will create intermediate directories if needed.
      mkdir -p "$1"
   fi
}

function unload_and_delete
{
   if [ -e "$1" ]; then
      ( launchctl unload "$1" || echo );
      # There are 2 separate launchds, one running as root, one as the current user.
      # sudo ... talks to the one running as root.
      # We talk to both to be sure we unloaded the plist.
      ( sudo launchctl unload "$1" || echo );
      rm "$1"
   fi
}

# Eventually, we should probably take this function out of the script, since it will not be relevant.
function remove_beta_files
{
   unload_and_delete ~/Library/LaunchAgents/com.techsmith.enterprise.uploader.plist
   unload_and_delete ~/Library/LaunchAgents/com.techsmith.relay.uploader.plist
   unload_and_delete /Library/LaunchAgents/com.techsmith.camtasiarelay.uploader.plist
   
   rm -Rf ~/Library/Application\ Support/TechSmith/Relay
}

function fixup_permissions_and_owner_for_uploader_log
{
   if [ -e "$1" ]; then
      chmod 644 "$1"
      chown nobody "$1"
   fi
}

#
# fixup the permissions of our app-bundle
# moving the .app to a "U:" drive will do this
# $1 will be the path to our Camtasia Relay.app bundle.
function fixup_permissions_for_bundle
{
   #give everything "base" permissions
   chmod -R 644 "$1"

   #make executable files executable
   chmod +x "$1"/Contents/MacOS/"Camtasia Relay"
   chmod +x "$1"/Contents/Resources/Uploader
   chmod +x "$1"/Contents/Resources/InstallerHelper

   #make all directories executable
   # http://movabletripe.com/archive/recursively-chmod-directories-only/
   find "$1" -type d -exec chmod +x {} \;
}

# fix up permissions for our application bundle
fixup_permissions_for_bundle "$1"

# Cleanup scripts
remove_beta_files

unload_and_delete /Library/LaunchDaemons/com.techsmith.camtasiarelay.uploader.plist
rm -Rf "/Library/Application Support/TechSmith/Camtasia Relay/Controller"

# Now create the new directories and copy everything over from the bundle
create_directory /Users/Shared/TechSmith/Camtasia\ Relay/Queued
create_directory /Users/Shared/TechSmith/Camtasia\ Relay/Recordings
create_directory /Users/Shared/TechSmith/Camtasia\ Relay/Processing
create_directory /Users/Shared/TechSmith/Camtasia\ Relay/Capturing
create_directory /Users/Shared/TechSmith/Camtasia\ Relay/Saved
chmod -R 777 /Users/Shared/TechSmith 

create_directory /Library/Logs/TechSmith/Camtasia\ Relay
chmod 777 /Library/Logs/TechSmith/Camtasia\ Relay

INSTALLED_UPLOADER_DIRECTORY='/Library/Application Support/TechSmith/Camtasia Relay'
create_directory "$INSTALLED_UPLOADER_DIRECTORY"
create_directory "$INSTALLED_UPLOADER_DIRECTORY"/Controller
chmod 777 "$INSTALLED_UPLOADER_DIRECTORY"/Controller

# $1 is the path to the installer bundle.
cp -f "$1"/Contents/Resources/Uploader "$INSTALLED_UPLOADER_DIRECTORY"/RelayUploader
cp -f "$1"/Contents/Resources/Uploader.plist "$INSTALLED_UPLOADER_DIRECTORY"/Controller/Uploader.plist
if [ -e "$INSTALLED_UPLOADER_DIRECTORY"/Controller/controllers.xml ]; then
   rm "$INSTALLED_UPLOADER_DIRECTORY"/Controller/controllers.xml
fi

create_directory /Library/LaunchDaemons/
UPLOADER_LAUNCHD_PLIST='/Library/LaunchDaemons/com.techsmith.camtasiarelay.uploader.plist'
cp -f "$1"/Contents/Resources/com.techsmith.camtasiarelay.uploader.plist "$UPLOADER_LAUNCHD_PLIST"
#if we don't set the permissions to 644, launchtl will complain:
#   launchctl: Dubious permissions on file (skipping): /Library/LaunchDaemons/com.techsmith.camtasiarelay.uploader.plist
chmod 644 "$UPLOADER_LAUNCHD_PLIST"
if [ -e "$UPLOADER_LAUNCHD_PLIST" ]; then
   sudo launchctl unload "$UPLOADER_LAUNCHD_PLIST";
   sudo launchctl load "$UPLOADER_LAUNCHD_PLIST"
fi

# if there are old versions of the logfile, we make sure their owner is set up so we can read/write them
fixup_permissions_and_owner_for_uploader_log "~/Library/Logs/TechSmith/Camtasia Relay/RelayUploader.log"
fixup_permissions_and_owner_for_uploader_log "~/Library/Logs/TechSmith/Camtasia Relay/RelayUploaderRESTCall.log"


echo "scriptfinished"
