#!/bin/bash # config SITE_DIR='./site' WWW_DIRS='./www ./public_html .' # do not touch this WWW_DIR='' 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: 2012-02-17' echo "Usage: $(basename $0) [options...]" echo '-c, --remove-cache' echo " remove cache '$SITE_DIR/tmp/cache/zend_cache*'" echo "-t, --remove-tpl-cache" echo " remove template cahce '$SITE_DIR/tmp/tpl_c/*.php'" echo '-p, --remove-pack' echo " remove packs '$WWW_DIR/js/*.pack.js', '$WWW_DIR/css/*.pack.css' files" echo '-m, --remove-merge' echo " remove merges '$WWW_DIR/js/*.pack.merge.js', '$WWW_DIR/css/*.pack.merge.css' files" echo '--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 message='Removing Zend Cache ' cecho "$message" doRemove "$SITE_DIR/tmp/cache/" 'zend_cache*' "$message" } doRemoveTplCache() { local message='Removing Smarty Cache ' cecho "$message" doRemove "$SITE_DIR/tmp/tpl_c/" '*.php' "$message" } 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 command line arguments VERBOSE='' OPT_REMOVE_CACHE='' OPT_REMOVE_TPL_CACHE='' OPT_REMOVE_PACK='' OPT_REMOVE_MERGE='' OPT_MOVE_HTACCESS='' while getopts ":ctpmv-:" optchar; do case "${optchar}" in '-') case "${OPTARG}" in 'help') doHelp exit 0 ;; 'remove-cache') OPT_REMOVE_CACHE=1 ;; 'remove-tpl-cache') OPT_REMOVE_TPL_CACHE=1 ;; 'remove-pack') OPT_REMOVE_PACK=1 ;; 'remove-merge') OPT_REMOVE_MERGE=1 ;; 'move-htaccess') OPT_MOVE_HTACCESS=1 ;; *) cecho "Unknown option ${OPTARG}\n" $_RED exit 1 ;; esac;; 'c') OPT_REMOVE_CACHE=1 ;; 't') OPT_REMOVE_TPL_CACHE=1 ;; 'p') OPT_REMOVE_PACK=1 ;; 'm') OPT_REMOVE_MERGE=1 ;; 'v') VERBOSE='-v' ;; *) cecho "Unknown option ${OPTARG}\n" $_RED exit 1 ;; esac done # executing commands if [[ -n $OPT_REMOVE_CACHE ]] ; then doRemoveCache fi if [[ -n $OPT_REMOVE_TPL_CACHE ]] ; then doRemoveTplCache fi if [[ -n $OPT_REMOVE_PACK ]] ; then doRemovePack fi if [[ -n $OPT_REMOVE_MERGE ]] ; then doRemoveMerge fi if [[ -n $OPT_MOVE_HTACCESS ]] ; then doMoveHtaccess fi