#!/bin/bash
#############################################################################
## Particularly useful with the 'UseGZfile' feature of Hiawatha webserver, ##
## this simply crawls one or more directories recursively (or PWD if null) ##
## and GZips relevant files *individually* for variable content encoding.  ##
#############################################################################

# CPU 'nice' priority level (19 to -20, 19 being the lowest):
CPU_NICE="10"
# Max number of threads to use (leave blank for auto-detection):
THREAD_MAX=""

if [ -z $THREAD_MAX ] ; then
        if [ `uname -a | grep -c Linux` = 1 ] ; then
                THREAD_MAX=`cat /proc/cpuinfo | grep -c processor`
        elif [ `uname -a | egrep -c '(BSD|Darwin)'` = 1 ] ; then
                THREAD_MAX=`sysctl -a | grep hw.ncpu | grep -v "=" | cut -d " " -f2`
        fi
elif  (( "$THREAD_MAX" >= "2" )) ; then
        THREAD_MAX="$THREAD_MAX"
else
        THREAD_MAX="2"
fi
find "${@}" -not -name '*[.][Gg][Zz]' -not -name "robots.txt" -not -name '*.[Bb][Zz]2' \
-not -name '*.7[Zz]' -not -name '*.[Zz][Ii][Pp]' -not -name '*.[Rr][Aa][Rr]' \
-not -name '*.[Dd][Ee][Bb]' -not -name '*.[Rr][Pp][Mm]' -not -name '*.[Pp][Nn][Gg]' \
-not -name '*.[Jj][Pp][Gg]' -not -name '*.[Jj][Pp][Ee][Gg]' -not -name '*.[Gg][Ii][Ff]' \
-not -name '*.[Ii][Cc][Oo]' -not -name '*.[Pp][Ll]' -not -name '*.[Pp][Hh][Pp]' \
-not -name '*.[Pp][Yy]' -not -name '*.[Rr][Bb]' -not -name '*.[Ss][Hh][Tt][Mm][Ll]' \
-not -name '*.[Cc][Gg][Ii]' -not -name '*.[Pp][Hh][Aa][Rr]' -not -name '.*' \
-not -name '*.[Ee][Oo][Tt]' -not -name '*.[Ww][Oo][Ff][Ff]' \
-not -name '*.[Ww][Oo][Ff][Ff]2' -not -name '*.[Xx][Zz]' \
-not -name '*.[Ss][Hh]' -not -type d -print0 | xargs -0 -n 1 -P \
${THREAD_MAX} nice -n ${CPU_NICE} bash -c 'gzip "$@" -9 -c > "$@".gz' --