#!/bin/ksh

# EditAppInfo.ksh
# Mon Nov 17 14:41:56 PST 2008
# Chieh Cheng
# http://www.CynosureX.com/

# GNU General Public License (GPL) Version 2, June 1991

  textEditor="/Applications/TextEdit.app/Contents/MacOS/TextEdit"

  set -A appDirs "/Applications/" "/Applications/AppleScript/" "/Applications/Automator.app/Contents/Resources/" "/Applications/Dictionary.app/Contents/SharedSupport/" "/Applications/Mail.app/Contents/" "/Applications/Safari.app/Contents/" "/Applications/Utilities/" "/Applications/Utilities/Directory Utility.app/Contents/Resources/" "/Applications/Utilities/Java/" "/Applications/iTunes.app/Contents/Resources/" "/Library/Application Support/Microsoft/HV1.0/" "/Library/Application Support/Microsoft/MAU2.0/" "/Library/Application Support/Microsoft/MAU2.0/Microsoft AutoUpdate.app/Contents/MacOS/" "/Library/Application Support/Microsoft/MERP2.0/" "/Library/Documentation/" "/Library/Image Capture/Devices/" "/Library/Receipts/Office2008_en_autoupdate.pkg/Contents/Resources/" "/Library/Scripts/ColorSync/" "/System/Library/ColorSync/Calibrators/" "/System/Library/Components/CharacterPalette.component/Contents/SharedSupport/" "/System/Library/Components/Ink.component/Contents/SharedSupport/" "/System/Library/Components/KeyboardViewer.component/Contents/SharedSupport/" "/System/Library/CoreServices/" "/System/Library/CoreServices/Dock.app/Contents/Resources/" "/System/Library/CoreServices/Language Chooser.app/Contents/MacOS/" "/System/Library/CoreServices/ManagedClient.app/Contents/Resources/" "/System/Library/CoreServices/RemoteManagement/" "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/" "/System/Library/CoreServices/RemoteManagement/AppleVNCServer.bundle/Contents/Support/" "/System/Library/CoreServices/SystemUIServer.app/Contents/Resources/" "/System/Library/CoreServices/iSight Updater.localized/" "/System/Library/Extensions/IOSerialFamily.kext/Contents/PlugIns/InternalModemSupport.kext/Contents/Resources/" "/System/Library/Extensions/SoftRAID.kext/Contents/Resources/" "/System/Library/Filesystems/AppleShare/" "/System/Library/Filesystems/webdav.fs/Support/" "/System/Library/Frameworks/AddressBook.framework/Versions/A/Resources/" "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/Resources/" "/System/Library/Frameworks/CalendarStore.framework/Versions/A/Resources/" "/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/Print.framework/Versions/A/Plugins/" "/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/SpeechRecognition.framework/Versions/A/Resources/" "/System/Library/Frameworks/InstantMessage.framework/" "/System/Library/Frameworks/PubSub.framework/Versions/A/Resources/" "/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/" "/System/Library/Frameworks/QuickLook.framework/Versions/A/Resources/" "/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/" "/System/Library/Frameworks/SecurityFoundation.framework/Versions/A/Resources/" "/System/Library/Frameworks/SyncServices.framework/Versions/A/Resources/" "/System/Library/Frameworks/Tk.framework/Versions/8.4/Resources/" "/System/Library/Image Capture/Automatic Tasks/" "/System/Library/Image Capture/Devices/" "/System/Library/Image Capture/Support/" "/System/Library/Image Capture/Tools/" "/System/Library/Input Methods/" "/System/Library/Input Methods/KoreanIM.app/Contents/" "/System/Library/Input Methods/Kotoeri.app/Contents/Support/" "/System/Library/Input Methods/SCIM.app/Contents/" "/System/Library/Input Methods/TCIM.app/Contents/" "/System/Library/Perl/Extras/5.8.8/darwin-thread-multi-2level/auto/Wx/" "/System/Library/PreferencePanes/UniversalAccessPref.prefPane/Contents/Resources/" "/System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/DotMacSyncManager.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/EAP8021X.framework/Support/" "/System/Library/PrivateFrameworks/FamilyControls.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/ScreenReader.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/SyncServicesUI.framework/Versions/A/Resources/" "/System/Library/PrivateFrameworks/YahooSync.framework/Versions/A/Resources/" "/System/Library/ScriptingAdditions/" "/System/Library/Services/" "/System/Library/Speech/Recognizers/AppleSpeakableItems.SpeechRecognizer/Contents/Resources/" "/usr/libexec/" "/usr/share/java/Tools/"

  scriptName=`basename "$0"`

  usage ()
  {
    echo "  Usage:   ${scriptName} \"Application 1\" [ . . . \"Application N\" ]"
  }

if [ -f "${textEditor}" ]
then

  if [ $# -gt 0 ]
  then
    for application
    do
      numOfDirs=${#appDirs[*]}
      pos=0
      while [ ${pos} -lt ${numOfDirs} ]
      do
        dir="${appDirs[${pos}]}"
        infoFile="${dir}/${application}.app/Contents/Info.plist"
        if [ -f "${infoFile}" ]
        then
          echo "Found ${application} in ${dir} . . ."
          break
        else
          echo "Unable to find ${application} in ${dir} . . ."
        fi
        (( pos = pos + 1 ))
      done

      if [ -f "${infoFile}" ]
      then
        progName=`basename "$0" ".sh"`
        backupFile="${dir}/${application}.app/Contents/Info.${progName}"
        if [ ! -f "${backupFile}" ]
        then
          sudo cp "${infoFile}" "${backupFile}"
        fi
        if [ -f "${backupFile}" ]
        then
          sudo "${textEditor}" "${infoFile}"
          echo "Done."
        else
          echo "${backupFile} exists and is not a regular file."
          echo "Something is wrong and you'll have to look into it."
          echo "${scriptName} cannot continue."
        fi
      else
        echo "${application} not found in any of the application directories."
        echo "${scriptName} cannot continue."
      fi
    done
  else
    usage
  fi

else
  echo "TextEdit is not installed on this computer. ${scriptName} cannot continue."
fi
