#!/bin/mksh
# $MirOS: contrib/hosted/tg/historic/series,v 1.3 2009/10/19 20:43:48 tg Exp $
#-
# Copyright (c) 2004, 2009
# Thorsten Glaser
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un-
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person's immediate fault when using the work as intended.
#-
# mass renaming of files
# Setup
usage() {
print 'Usage: series [-nz] [-u ] -s -t ...'
print ' In filename template, use % character for index.'
exit 1
}
do_mv=mv
let minimum=1
series=ask
let index=1
# Option Processing
while getopts "hns:t:u:z" option; do
case $option in
h) usage ;;
n) do_mv="echo mv" ;;
s) if [[ -z $OPTARG ]]; then
usage
else
let index="$OPTARG"
fi ;;
t) series="$OPTARG" ;;
u) let maxnos="$OPTARG" ;;
z) let minimum=0 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
(( index < minimum )) && usage
while [[ -z $series || $series = ask || $series != *@(%)* ]]; do
print -n 'Name of series (x to exit)? '
read series
[[ $series = x ]] && exit 2
done
# Figure out with how many zeroes to stuff
let number="index + $#"
(( number > maxnos )) || let number=maxnos
let digits=1
(( number > 9 )) && let digits=10
(( number > 99 )) && let digits=100
(( number > 999 )) && let digits=1000
# Do the renaming
for file in "$@"; do
# Generate stuffed number
(( index < 1 )) && let i=1 || let i=index
fnr=$index
while (( i < digits )); do
fnr=0$fnr
let 'i*=10'
done
# Generate filename with number in it
fname="${series%@(%)*}${fnr}${series##*@(%)}"
# Rename and loop
$do_mv "$file" "$fname"
let index+=1
done
exit 0