1# $Id: pmv-examples.sh,v 1.3 2003/12/12 06:30:12 ianb Exp $
2
3# This file contains usefule bourne shell functions that use pmv.
4# To have these available for use, put commands to source this file
5# in your startup files.
6# For instance, debian users using bash would add to either
7# /etc/profile or ~/.bash_profile the following (without the leading '#') :
8
9# . /usr/share/doc/pmv/pmv-examples
10
11# replace the pathname with the pathname to this file on your system.
12# or, you could simply insert the contents of this file into your startup script.
13# Any unhappy csh users out there, let me know, and I'll do a csh version.
14# This file assumes pmv is in your PATH.
15
16# Available commands:
17# pmv-stdswap  - Swaps round fields in filenames delimited by " - ".
18# pmv-space2_  - converts all spaces in filename to underscores.
19# pmv-_2space  - Converts all underscores in filename to spaces.
20# pmv-fixcaps  - Crude attempt at capitalising filenames.
21# pmv-fixcase  - Much better attempt at capitalising filenames.
22#                Needs the Text::Autoformat module from CPAN.
23# pmv-number   - Numbers filenames sequentially.
24# pmv-deepen   - converts dirs from "artist - album" to "artist/album"
25# pmv-flatten  - converts dirs from "artist/album" to "artist - album"
26# To use these commands, just use: pmv-command filenames
27# eg pmv-fixcase *.mp3
28
29
30# This function swaps between the filename formats:
31#  "Artist - Album - 01 - Track.mp3"
32# and
33#  "01 - Artist - Album - Track.mp3"
34# If you run it a few times, it will do the same, backwards.
35function pmv-stdswap
36{
37	pmv '$name=~s/(.*) - (.*) - (.*) - (.*)/$3 - $1 - $2 - $4/;' "$@"
38}
39
40# Converts all spaces in filenames to underscores.
41function pmv-space2_
42{
43	pmv 's/\s/_/g;' "$@"
44}
45
46# Converts all underscores in filenames to spaces.
47function pmv-_2space
48{
49	pmv 's/_/ /g;' "$@"
50}
51
52# Number all supplied files increasing sequentially from 01
53# The number is prepended to the filename, so
54# "foo.mp3" becomes "01 - foo.mp3"
55# The filename is otherwise untouched.
56# This is often a useful starting point for completely disorganised filenames,
57# before using freedbtofilename, etc.
58function pmv-number
59{
60	pmv 'unless(defined($c)) { $c="00"; } $c++;$name=~s/^/$c - /;' "$@"
61}
62
63# Crude attempt to capitalise filenames.
64# Doesn't handle brackets and other things very well.
65function pmv-fixcaps
66{
67	pmv  '$name=join(" ",map({ucfirst(lc($_));} split(/\s+/,$name)));' "$@"
68}
69
70# Much better attempt to capitalise filenames
71# This uses Damian Conway's incredibly smart Text::Autoformat module from CPAN
72# (http://www.cpan.org/). It uses "highlight" case, ie  all "important" words
73# are capitalised. If you want to change the behaviour, find the section
74# that says {case=>"highlight"} and change it as follows:
75# {case=>"title"}     - Capitalise every word.
76# {case=>"sentence"}  - Capitalise the first word.
77# {case=>"upper"}     - Upper-case everything.
78# {case=>"lower"}     - Lower-case everything.
79function pmv-fixcase
80{
81	pmv -b 'use Text::Autoformat;' '$name=~/(^[^\.]+\.?$)|(?:(.*)(\..+))/; $f=(defined($2))?$2:$1;$ext=$3 ;@a=split(/ - /,$f);$name=join(" - ",map {autoformat($_,{case=>"highlight"});} @a);$name=~s/\n//g;if(defined($ext)){$name.="$ext";}' "$@"
82}
83
84# Convert directories of the form "artist/album" to "artist - album"
85# Note use of -b to include a module and -e to delete directories after moving
86function pmv-flatten
87{
88    pmv -b 'use Cwd;' -e 'for my $del (@del){rmdir $del;}' '$d=$_;unless(-d $d) {die("$d: not a directory\n");}$d=Cwd::abs_path($d);($top,$mid,$last)=($d=~/(.+)\/(.+)\/(.+)/) ;push(@del,$mid);$_="$top/$mid - $last";' "$@"
89}
90
91# Convert directories of the form "artist - album" to "artist/album"
92function pmv-deepen
93{
94    pmv 'if(-d && /(.*) - (.*)/) { mkdir $1; $_="$1/$2";}' "$@"
95}
96
97# prepends a datestamp in the form YYYYMMDD (eg 20031211)
98# (which will sort chronologically)
99function pmv-datestamp
100{
101	pmv -b '$d=`date +%Y%d%m`;chomp($d);' '$name = "$d.$name";' "$@"
102}
103