/scripts/package_archive.sh

https://code.google.com/p/camelbox/ · Shell · 133 lines · 100 code · 13 blank · 20 comment · 18 complexity · 0c7066272722654aa964536b8d811c49 MD5 · raw file

  1. #!/bin/sh
  2. # simple script to package a directory full of files
  3. # usage:
  4. # time sh ~/Documents/src/camelbox-svn/scripts/package.sh package1 package2
  5. ## SCRIPT VARIABLES
  6. TIMESTAMP=$(TZ=GMT date +%Y.%j | tr -d '\n')
  7. ARCHIVE_DIR="../archives"
  8. PKGLISTS_DIR="share/pkglists"
  9. BASENAME=$(which basename)
  10. #RELEASE_TIMESTAMP=$(TZ=GMT date +%Y.%j.%H%m | tr -d '\n')
  11. if [ -z $BASENAME ]; then
  12. echo "ERROR: basename binary required for this script"
  13. echo "Please install the basename binary and rerun this script"
  14. exit 1
  15. fi
  16. SCRIPT_NAME=$($BASENAME $0)
  17. ## FUNCTIONS
  18. function find_first_free_filename () {
  19. local FILE_DIR=$1
  20. local FILE_NAME=$2
  21. local FILE_EXT=$3
  22. local FILE_COUNTER=1
  23. while [ -f $FILE_DIR/$FILE_NAME.$TIMESTAMP.$FILE_COUNTER.$FILE_EXT ];
  24. do
  25. if [ "x${VERBOSE}" = "xtrue" ]; then
  26. echo "$FILE_DIR/$FILE_NAME.$TIMESTAMP.$FILE_COUNTER.$FILE_EXT exists"
  27. fi
  28. FILE_COUNTER=$(($FILE_COUNTER + 1))
  29. done
  30. echo "Output file will be: $FILE_NAME.$TIMESTAMP.$FILE_COUNTER.$FILE_EXT"
  31. FREE_FILENAME="$FILE_DIR/$FILE_NAME.$TIMESTAMP.$FILE_COUNTER.$FILE_EXT"
  32. } # function find_first_free_filename()
  33. function show_usage () {
  34. echo "Usage:"
  35. echo " -h show this help text"
  36. echo " -v verbose output from 'tar'"
  37. echo
  38. echo "To generate packages from a list of directories:"
  39. echo " ${SCRIPT_NAME} dir1 dir2 dir3"
  40. echo "To generate packages from a list of directories, with verbose output:"
  41. echo " ${SCRIPT_NAME} -v dir1 dir2 dir3"
  42. echo "Redirect STDERR to STDOUT to capture the output from 'tar'"
  43. echo
  44. echo "(Package list files need *NIX forward slashes for path separation)"
  45. } # function show_usage ()
  46. ## MAIN SCRIPT
  47. while getopts hv VARLIST
  48. do
  49. case $VARLIST in
  50. v) VERBOSE="true";;
  51. h) SHOW_HELP="true";;
  52. esac
  53. done # while getopts
  54. shift $(expr $OPTIND - 1)
  55. if [ "x${SHOW_HELP}" = "xtrue" ]; then show_usage; exit 1; fi
  56. if [ "x${VERBOSE}" = "xtrue" ]; then
  57. TAR_CMD="tar -cv *"
  58. else
  59. TAR_CMD="tar -c *"
  60. fi
  61. # parse all of the archive files
  62. for ARCHIVE_NAME in "$@"
  63. do
  64. if [ -d $ARCHIVE_NAME ]; then
  65. if [ ! -d $ARCHIVE_NAME/$PKGLISTS_DIR ]; then
  66. mkdir -p $ARCHIVE_NAME/$PKGLISTS_DIR
  67. fi # if [ ! -d pkgfiles ]; then
  68. echo "Creating archive package '${ARCHIVE_NAME}'"
  69. if [ "x${VERBOSE}" = "xtrue" ]; then
  70. echo "Checking for existing ${ARCHIVE_NAME}.${TIMESTAMP} files"
  71. fi
  72. find_first_free_filename $ARCHIVE_DIR $ARCHIVE_NAME "tar.lzma"
  73. CURRENT_DIR=$PWD
  74. cd $ARCHIVE_NAME
  75. echo "# Package list for: $FREE_FILENAME" \
  76. | sed '{s|\.\.\/archives\/||;}' > $PKGLISTS_DIR/$ARCHIVE_NAME.txt
  77. SYSNAME=$(uname -s | tr -d '\n')
  78. if [ "x$SYSNAME" = "xDarwin" ]; then
  79. # Mac OS X, lzma from the MacPorts tree
  80. find . | sed '{/^\.$/d; s/\.\\//; s/\\/\//g;}' \
  81. >> $PKGLISTS_DIR/$ARCHIVE_NAME.txt
  82. if [ "x${VERBOSE}" = "xtrue" ]; then
  83. tar -cv * | lzma -z -c > ../$ARCHIVE_DIR/$FREE_FILENAME
  84. else
  85. tar -c * | lzma -z -c 2>/dev/null \
  86. > ../$ARCHIVE_DIR/$FREE_FILENAME
  87. fi # if [ "x${VERBOSE}" = "xtrue" ]
  88. if [ $? -ne 0 ]; then
  89. echo "ERROR: tar/lzma exited with an error code of $?"
  90. exit 1
  91. fi # if [ $? -ne 0 ]
  92. else
  93. # Windows, lzma from the lzma SDK
  94. xfind . | sed '{ /^\.$/d; s/\.\\//; s/\\/\//g;}' \
  95. >> $PKGLISTS_DIR/$ARCHIVE_NAME.txt
  96. if [ "x${VERBOSE}" = "xtrue" ]; then
  97. tar -cv * | lzma e -si ../$ARCHIVE_DIR/$FREE_FILENAME
  98. else
  99. tar -c * | lzma e -si ../$ARCHIVE_DIR/$FREE_FILENAME 2>nul:
  100. fi # if [ "x${VERBOSE}" = "xtrue" ]
  101. if [ $? -ne 0 ]; then
  102. echo "ERROR: tar/lzma exited with an error code of $?"
  103. exit 1
  104. fi # if [ $? -ne 0 ]
  105. fi # if [ "x$SYSNAME" == "xDarwin" ]
  106. cd $CURRENT_DIR
  107. else
  108. echo "ERROR: Directory ${ARCHIVE_NAME} does not exist"
  109. exit 1
  110. fi # if [ -d $ARCHIVE_NAME ]
  111. done
  112. # - read in a list of directories
  113. # - create a log file
  114. # - enter a directory
  115. # - read the output directory, verify there's not a file with the same name;
  116. # if so, bump up the version #
  117. # - tar it, lzma it, command output to log, file output to user's output
  118. # directory
  119. # - next directory
  120. # - no errors? delete logfile