PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

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