#! /bin/bash -u # Last edited on 2009-04-08 03:57:45 by stolfilocal usage="$0 [ -suffix {SUFFIX} ] < FILES.dir > ICONS.dir" # Reads a list of file names from standard input. If each {FILENAME} # read is an icon-like name, outputs {FILENAME} itself. Otherwise, # looks for an icon-like version of {FILENAME}, in the same directory; # if such exists, outputs the name of that icon file. Otherwise # outputs {FILENAME}{SUFFIX}.png (where the {SUFFIX} defaults to # "-icon") # # An `icon-like name' must have one of the following extensions # .png .PNG .jpg .JPG .jpeg .JPEG # possibly with ".gz" or ".Z" appedned; and its name minus extension # must end in one of "-icon" "_icon" "-thumb" "_thumb" "-i" "_i". cmd=${0##*/} function error() { echo "${cmd}: $1" >&2; echo "usage: ${usage}" >&2; echo "(unknown)"; exit 1; } suffix="-icon" while [ $# -gt 0 ]; do case "$1" in -suffix ) if [ $# -lt 2 ]; then error "missing suffix"; fi suffix="$2"; shift; shift ;; -* ) error "unrecognized option $1" ;; * ) break;; esac; done if [ $# != 0 ]; then error "excess parameters «$*»"; fi while read -r fname; do # Get extension and root name: uname="${fname}" case "${uname}" in *.gz | *.Z ) uname="${uname%.*}" ;; esac; case "${uname}" in *.png | *.PNG | *.jpg | *.JPG | *.jpeg | *.JPEG ) uname="${uname%.*}" case ${uname} in *_icon | *-icon | *-thumb | *_thumb | *-i | *_i ) # image is already an icon in right format echo "${fname}"; continue;; esac ;; *.* ) uname="${uname%.*}" case ${uname} in *_icon | *_thumb | *_i ) uname="${uname%_*}" ;; *-icon | *-thumb | *-i ) uname="${uname%-*}" ;; esac ;; *) ;; esac # Check if icon already exists: # Note the "." to avoid empty list troubles (bash 'e uma boshta) icons=( $(shopt -s nullglob; echo . ${uname}{-,_}{icon,thumb,i}.{png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF}{,.gz,.Z}* ) ) if [ ${#icons[@]} -gt 1 ]; then echo "${icons[1]}"; continue; else echo "${uname}${suffix}.png"; continue; fi; done