dev.stuconnolly.com / svn / safaritabs

  1. #! /bin/ksh
  2.  
  3. ## $Id: trim-application.sh 224 2011-08-13 20:39:45Z stuart $
  4. ##
  5. ## Author:      Stuart Connolly (stuconnolly.com)
  6. ##              Copyright (c) 2011 Stuart Connolly. All rights reserved.
  7. ##
  8. ## Paramters:   -p -- The path to the application that is to be trimmed
  9. ##              -d -- Remove unnecessary files (i.e. .DS_Store files, etc) (optional).
  10. ##              -n -- Trim nib files (i.e. remove .info.nib, classes.nib, data.dependency and designable.nib) (optional).
  11. ##              -s -- Strip debug symbols from application binary (optional).
  12. ##              -t -- Compress tiff images using LZW compression (optional).
  13. ##              -f -- Remove framework headers (optional).
  14. ##              -r -- Remove resource forks (optional).
  15. ##              -a -- All of above optional options. Equivalent to '-d -n -s -t -f -r'.
  16. ##
  17. ## Description: Trims an application bundle of unnecessary files and resources that are generally not required and otherwise
  18. ##              waste disk space.
  19.  
  20. usage()
  21. {
  22.     echo "Usage: `basename $0` -p application_path [-d -n -s -t -f -r]"
  23.     exit 1
  24. }
  25.  
  26. while getopts ":p:dnstra" OPTION
  27. do
  28.     case $OPTION in
  29.         p) APP_PATH="$OPTARG";;
  30.         d) REMOVE_FILES=1;;
  31.         n) TRIM_NIBS=1;;
  32.         s) STRIP_DEBUG=1;;
  33.         t) COMPRESS_TIFF=1;;
  34.         f) REMOVE_F_HEADERS=1;;
  35.         r) REMOVE_RSRC=1;;
  36.         a) REMOVE_FILES=1;
  37.            TRIM_NIBS=1;
  38.            STRIP_DEBUG=1;
  39.            COMPRESS_TIFF=1;
  40.            REMOVE_F_HEADERS=1;
  41.            REMOVE_RSRC=1;;
  42.         *) echo 'Unrecognised argument'; usage;;
  43.     esac
  44. done
  45.  
  46. if [ $# -eq 0 ]
  47. then
  48.     echo 'Illegal number of arguments. I need the path to an application.'
  49.     usage
  50. fi
  51.  
  52. if [ ! -d "$APP_PATH" ]
  53. then
  54.     echo "Invalid application path. Application at path '${APP_PATH}' doesn't seem to exist."
  55.     usage
  56. fi
  57.  
  58. if [ ! -w "$APP_PATH" ]
  59. then
  60.     echo "Error: Application at path '${APP_PATH}' is not writeable."
  61.     usage
  62. fi
  63.  
  64. if [ $# -lt 2 ]
  65. then
  66.     echo 'Illegal number of arguments. I need at least one trim option.'
  67.     usage
  68. fi
  69.  
  70. printf "Trimming application bundle '`basename $APP_PATH`' at '${APP_PATH}'...\n\n"
  71.  
  72. # Remove unnecessary files
  73. if [ $REMOVE_FILES ]
  74. then
  75.     printf 'Removing unnecessary files...\n'
  76.  
  77.     find "$APP_PATH" \( -name '.DS_Store' -or -name 'pbdevelopment.plist' -type f \) | while read FILE; do; printf "\tRemoving file: ${FILE}\n"; rm "$FILE"; done;
  78. fi
  79.  
  80. # Trim nibs
  81. if [ $TRIM_NIBS ]
  82. then
  83.     printf '\nTrimming nibs...\n'
  84.  
  85.     find "$APP_PATH" \( -name 'info.nib' -or -name 'classes.nib' -or -name 'data.dependency' -or -name 'designable.nib' -type f \) | while read FILE; do; printf "\tRemoving nib file: ${FILE}\n"; rm "$FILE"; done;
  86. fi
  87.  
  88. # Strip debug symbols
  89. if [ $STRIP_DEBUG ]
  90. then
  91.     printf '\nStripping debug symbols...\n'
  92.  
  93.     find "${APP_PATH}/Contents/MacOS" -type f | while read FILE; do; printf "\tStripping binary: ${FILE}\n"; /Developer/Library/PrivateFrameworks/DevToolsCore.framework/Versions/A/Resources/pbxcp -resolve-src-symlinks -strip-debug-symbols "$FILE" '/tmp'; mv "/tmp/`basename $FILE`" "$FILE"; done;
  94. fi
  95.  
  96. # Compress tiff images
  97. if [ $COMPRESS_TIFF ]
  98. then
  99.     printf '\nCompressing tiff images...\n'
  100.  
  101.     find "$APP_PATH" \( -name "*.tif" -or -name "*.tiff" \) | while read FILE; do; printf "\tCompressing tiff: ${FILE}\n"; tiffutil -lzw "$FILE" -out "${FILE}.out" 2> /dev/null; mv "${FILE}.out" "$FILE"; done;
  102. fi
  103.  
  104. # Remove framework headers
  105. if [ $REMOVE_F_HEADERS ]
  106. then
  107.     printf '\nRemoving framework headers...\n'
  108.    
  109.     FRAMEWORK_PATH="${APP_PATH}/Contents/Frameworks"
  110.    
  111.     if [ -d "$FRAMEWORK_PATH" ]
  112.     then
  113.         find "$FRAMEWORK_PATH" \( -name "*.h" -type f \) | while read FILE; do; printf "\tRemoving header: ${FILE}\n"; rm "$FILE"; done;
  114.     fi
  115. fi
  116.  
  117. # Remove resource forks
  118. if [ $REMOVE_RSRC ]
  119. then
  120.     printf '\nRemoving resource forks...\n'
  121.    
  122.     find "$APP_PATH" -type f | while read FILE; do if [ -s "${FILE}/rsrc" ]; then; printf "\tRemoving reource: ${FILE}/rsrc\n"; cp /dev/null "${FILE}/rsrc"; fi; done;
  123. fi
  124.  
  125. printf "\nTrimming application bundle '`basename $APP_PATH`' complete\n"
  126.  
  127. exit 0
  128.