+\e[1mOPTIONS:\e[0m
+ -a|--all
+ Convert all image files in DIRECTORY to webp. This is the default mode.
+ This is mutually exclusive with -s|--scan.
+
+ -s|--scan
+ Scan all files with .php or .html extension in DIRECTORY for <img> tags
+ and convert source images to webp. This is mutually exclusive with
+ -a|--all.
+
+ -o|--overwrite
+ Convert all files even if the output filename exists (the default is to
+ ignore files for which a webp already exists). When calculating space
+ savings of files which overwrite existing ones, the saving is relative to
+ the source file, not the file that is overwritten.
+
+ -q|--quiet
+ Do not print any output (apart from errors). Mutually exclusive with
+ -v|--verbose.
+
+ -v|--verbose
+ Print extra output for each file. Mutually exclusive with -q|--quiet.
+
+ -d|--dry
+ Dry run - search for files and attempt to convert them but do not make any
+ permanent changes. Used for checking possible space savings.
+
+ -h|--help
+ Print this help message and exit.
+
+\e[1mDIRECTORY:\e[0m
+ Directory in which to search for PHP or HTML files (in -s|--scan mode) or
+ image files (in -a|--all mode).
+
+Converts JPG and PNG files to WebP using ImageMagick's convert(1). Optionally
+searches for source file in a directory or searches through HTML/PHP files in
+a directory to find references to source files.\n"
+
+usageshort="\e[1mUSAGE:\e[0m
+ $(basename "$0") [MODE] [OPTIONS] [DIRECTORY]
+
+For more information, see \e[34m$(basename "$0") --help\e[0m\n"
+
+modehint="\x1b[31m-s|--scan and -a|--all cannot be specified simultaneously\x1b[0m\n\n$usageshort"
+verbosityhint="\x1b[31m-q|--quiet and -v|--verbose cannot be specified simultaneously\x1b[0m\n\n$usageshort"
+dryhint="\x1b[33mDry run mode\x1b[0m\n"
+overwritehint="\x1b[33mOverwriting existing files\x1b[0m\n"
+invalidhint="\x1b[31mInvalid argument $1\x1b[0m\n\n$usageshort"
+
+if (( $# > 5)); then
+ printf "$usageshort"
+ exit 1
+fi
+
+mode=-1 # -1: initial, 0: all (default), 1: scan
+overwrite=0
+dry=0
+verbosity=-1 # -1: initial, 0: quiet, 1: normal, 2: verbose
+directory=""
+
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -a|--all)
+ if [[ $mode == 1 ]]; then
+ printf "$modehint"
+ exit 1
+ fi
+ mode=0
+ shift
+ ;;
+ -s|--scan)
+ if [[ $mode == 0 ]]; then
+ printf "$modehint"
+ exit 1
+ fi
+ mode=1
+ shift
+ ;;
+ -o|--overwrite)
+ overwrite=1
+ shift
+ ;;
+ -q|--quiet)
+ if [[ $verbosity == 2 ]]; then
+ printf "$verbosityhint"
+ exit 1
+ fi
+ verbosity=0
+ shift
+ ;;
+ -v|--verbose)
+ if [[ $verbosity == 0 ]]; then
+ printf "$verbosityhint"
+ exit 1
+ fi
+ verbosity=2
+ shift
+ ;;
+ -d|--dry)
+ dry=1
+ shift
+ ;;
+ -h|--help)
+ printf "$usagelong"
+ exit
+ ;;
+ *)
+ if [[ "$directory" = "" && "$1" != -* ]]; then
+ directory=$1
+ shift
+ else
+ printf "$invalidhint"
+ exit 1
+ fi
+ ;;
+ esac
+done
+
+# Set mode to 0 (all) if not specified
+if [[ $mode == -1 ]]; then
+ mode=0
+fi
+
+# Set directory to . if not specified
+if [[ "$directory" = "" ]]; then
+ directory="."
+fi
+
+# Set verbosity to 1 if not specified
+if [[ $verbosity == -1 ]]; then
+ verbosity=1
+fi
+
+# Indicate that we are running in dry mode
+if [[ $verbosity > 0 && $dry == 1 ]]; then
+ printf "$dryhint"
+fi
+
+# Indicate that we will overwrite
+if [[ $verbosity > 0 && $overwrite == 1 ]]; then
+ printf "$overwritehint"
+fi
+
+savings=()
+savings_total=0
+found_count=0