#!/usr/local/bin/bash #Site path DIR="/path/site.ru" #Default all enable #Optimization disable for files array: html_block_file_list=(wmail_a23456fabc11223.html google12fafa3f3f3f4567.html) css_block_file_list=() js_block_file_list=() jpg_block_file_list=() gif_block_file_list=() png_block_file_list=() #Optimization disable for dir array: html_block_dir_list=() jpg_block_dir_list=() gif_block_dir_list=() png_block_dir_list=() #CSS and JS optimization enabled only in this folder array: css_allow_dir_list=(/path/site.ru/CSS) js_allow_dir_list=(/path/site.ru/JSC) DIRF=$( find ${DIR} -type f -name '*.gif' \ -o -name '*.jpg' \ -o -name '*.png' \ -o -name '*.css' \ -o -name '*.js' \ -o -name '*.html' ) for file in $DIRF do exten=${file##*.} filename="${file##*/}" #file name dirname="${file%/*}" #dir name case "$exten" in "jpg") if (( ${#jpg_block_dir_list[@]}>0 )); then #DIR block list check for a in "${jpg_block_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then continue 2 #go to next fi done fi if (( ${#jpg_block_file_list[@]}>0 )); then #File block list check for a in "${jpg_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi jpegoptim --all-progressive --strip-all -ft $file echo "$file -OK" ;; "gif") if (( ${#gif_block_dir_list[@]}>0 )); then #DIR block list check for a in "${gif_block_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then continue 2 #go to next fi done fi if (( ${#gif_block_file_list[@]}>0 )); then #File block list check for a in "${gif_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi gifsicle -b -O2 $file echo "$file -OK" ;; "png") if (( ${#png_block_dir_list[@]}>0 )); then #DIR block list check for a in "${png_block_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then continue 2 #go to next fi done fi if (( ${#png_block_file_list[@]}>0 )); then #File block list check for a in "${png_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi FUSER=$( ls -ld $file | awk 'NR==1 {print $3}' ) FGROUP=$( ls -ld $file | awk 'NR==1 {print $4}' ) optipng -o5 -strip all -force $file chown $FUSER:$FGROUP $file #chown usd because optipng change user echo "$file -OK" ;; "html") if (( ${#html_block_dir_list[@]}>0 )); then #DIR block list check for a in "${html_block_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then continue 2 #go to next fi done fi if (( ${#html_block_file_list[@]}>0 )); then #File block list check for a in "${html_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi tidy5 -utf8 -w 0 -n -b -q -omit -m $file echo "$file -OK" ;; "css") if (( ${#css_block_file_list[@]}>0 )); then #File block list check for a in "${css_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi if (( ${#css_allow_dir_list[@]}>0 )); then #DIR block list check for a in "${css_allow_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then yuicompressor --charset utf-8 -v -o '.css$:.css' $file echo "$file -OK" continue 2 fi done fi ;; "js") if (( ${#js_block_file_list[@]}>0 )); then #File block list check for a in "${js_block_file_list[@]}"; do if [ "$a" == "$filename" ]; then continue 2 #go to next fi done fi if (( ${#js_allow_dir_list[@]}>0 )); then #DIR block list check for a in "${js_allow_dir_list[@]}"; do if [ "$a" == "$dirname" ]; then yuicompressor --charset utf-8 --nomunge -v -o '.js$:.js' $file echo "$file -OK" continue 2 fi done fi ;; *) #do nothind ;; esac done #Apache restart apachectl restart exit 0