#!/bin/bash # config SITE_DIR='./site' WWW_DIRS='./www ./public_html .' DEFAULT_COMMANDS='doRemoveCache doRemoveMerge doMoveHtaccess' # do not touch this WWW_DIR='' VERBOSE='' COMMANDS='' CL_DEFAULT='\E[0m' CL_BLACK='\E[30m' CL_RED='\E[31m' CL_GREEN='\E[32m' CL_YELLOW='\E[33m' CL_BLUE='\E[34m' CL_WHILE='\E[37m' # $message $color cecho () { local message=${1:-''} local color=${2:-$CL_WHILE} echo -ne "$color" echo -ne "$message" echo -ne $CL_DEFAULT return } for path in $WWW_DIRS ; do if [[ -d "$path/_lib" ]] && [[ -f "$path/index.php" ]] ; then WWW_DIR=$path break fi done if [ -d "$SITE_DIR" ] ; then echo -n else cecho "Error: Can not find SITE_DIR: '$SITE_DIR'\n" $CL_RED exit fi if [ -d "$WWW_DIR" ] ; then echo -n else cecho "Error: Can not find WWW_DIR. Search paths: '$WWW_DIRS'\n" $CL_RED exit fi doHelp() { echo 'Version: 2011-04-12' echo "Usage: $(basename $0) [options...]" echo '-rc, --remove-cache' echo " remove '$SITE_DIR/tmp/cache/zend_cache*', '$SITE_DIR/tmp/tpl_c/*.php'" echo '-rp, --remove-pack' echo " remove '$WWW_DIR/js/*.pack.js', '$WWW_DIR/css/*.pack.css' files" echo '-rm, --remove-merge' echo " remove '$WWW_DIR/js/*.pack.merge.js', '$WWW_DIR/css/*.pack.merge.css' files" echo '-mh, --move-htaccess' echo " move '$WWW_DIR/.htaccess_live' to '$WWW_DIR/.htaccess'" echo '-v explain what is being done' echo '--help show this help' echo } # Outpus text using "cecho" only if VERBOSE turned on verbose() { if [[ -n "$VERBOSE" ]] ; then cecho "$@" fi } # $1-path $2-mask checkRemoved() { local result=`find $1 -type f -name $2` verbose "\n" if [[ -z $result ]] ; then cecho 'successfully' $CL_GREEN else cecho 'failed' $CL_RED fi } # $1-path $2-mask $3-message doRemove() { if [[ -d $1 ]] ; then local output=`find $1 -type f -name $2 -exec rm $VERBOSE -f {} \;` if [[ -n $output ]] ; then verbose "\n" echo -ne $CL_YELLOW echo "$output" echo -ne $CL_DEFAULT fi checkRemoved "$1" "$2" else cecho "fail (Can not find directory $1)" $CL_RED fi echo } doRemoveCache() { local message1='Removing Zend Cache ' cecho "$message1" doRemove "$SITE_DIR/tmp/cache/" 'zend_cache*' "$message1" local message2='Removing Smarty Cache ' cecho "$message2" doRemove "$SITE_DIR/tmp/tpl_c/" '*.php' "$message2" } doRemovePack() { local message1='Removing *.pach.js ' cecho "$message1" doRemove "$WWW_DIR/js/" '*.pack.js' "$message1" local message2='Removing *.pack.css ' cecho "$message2" doRemove "$WWW_DIR/css/" '*.pack.css' "$message2" } doRemoveMerge() { local message1='Removing *.merge.js ' cecho "$message1" doRemove "$WWW_DIR/js/" '*.pack.merge.js' "$message1" local message2='Removing *.merge.css ' cecho "$message2" doRemove "$WWW_DIR/css/" '*.pack.merge.css' "$message2" } doMoveHtaccess() { cecho "Renaming .htaccess_live " if [[ -f "$WWW_DIR/.htaccess_live" ]] ; then verbose "\n" echo -ne $CL_YELLOW if [[ -f "$WWW_DIR/.htaccess" ]] ; then mv -T $VERBOSE -f "$WWW_DIR/.htaccess" "$WWW_DIR/.htaccess_old" fi mv -T $VERBOSE -f "$WWW_DIR/.htaccess_live" "$WWW_DIR/.htaccess" echo -ne $CL_DEFAULT fi if [[ -f "$WWW_DIR/.htaccess" ]] ; then cecho 'successfully' $CL_GREEN fi echo } # processing parameters for opt in $@ ; do case $opt in ( '--help' ) shift doHelp exit ;; ( '-v' | '--verbose' ) shift VERBOSE='-v' ;; ( '-rc' | '--remove-cache' ) shift COMMANDS="$COMMANDS doRemoveCache" ;; ( '-rp' | '--remove-pack' ) shift COMMANDS="$COMMANDS doRemovePack" ;; ( '-rm' | '--remove-merge' ) shift COMMANDS="$COMMANDS doRemoveMerge" ;; ( '-mh' | '--move-htaccess' ) shift COMMANDS="$COMMANDS doMoveHtaccess" ;; ( * ) shift cecho "Unknown option $opt\n" $_RED exit ;; esac done # processing commands if [[ -z "$COMMANDS" ]] ; then COMMANDS=$DEFAULT_COMMANDS fi for cmd in $COMMANDS ; do $cmd done