~ubuntu-branches/ubuntu/saucy/pictor/saucy-proposed

« back to all changes in this revision

Viewing changes to pictor-rename

  • Committer: Package Import Robot
  • Author(s): Dustin Kirkland
  • Date: 2013-03-21 23:59:14 UTC
  • mfrom: (2.1.22)
  • Revision ID: package-import@ubuntu.com-20130321235914-oyxxvo1uhvrphpcj
Tags: 2.24-0ubuntu1
* debian/control, debian/pictor-unload.install, debian/pictor-
  unload.manpages, pictor-rename, pictor-rename.1, pictor-unload:
  - depend on fdupes, to remove duplicate files
  - depend on libimage-exiftool-perl for exiftool
  - add a new tool, pictor-rename, and its manpage
    + rename pictures with a prepended timestamp, YYYY-MM-DD_HH-MM-SS__*
    + use dashes instead of colons in time for compatibility with
      Mac OS X and Windows
  - drastically simplify pictor-unload
    + move naming logic to pictor-rename
    + use fdupes to sensibly remove duplicate images
    + drop card unmounting logic
* debian/control, pictor-rename:
  - bump standards

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#
 
3
#    pictor-rename: a script for renaming pictures sensibly,
 
4
#                   prepending a timestamp YYYY-MM-DD_HH-MM-SS__*
 
5
#                   on each filename
 
6
#    Copyright (C) 2013 Dustin Kirkland <dustin.kirkland@gmail.com>
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as published by
 
10
#    the Free Software Foundation, version 3 of the License.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
 
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
get_prefix() {
 
22
        local mod d t
 
23
        _RET=
 
24
        if [ ! -f "$1" ]; then
 
25
                return
 
26
        fi
 
27
        mod=$(exiftool -CreateDate -d %Y-%m-%d_%H-%M-%S__ "$1" | sed -e "s/.*: //") 2>/dev/null
 
28
        if echo "${mod}" | grep -qs "^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}__"; then
 
29
                # First, try to get create date from image headers
 
30
                _RET="${mod}"
 
31
        else
 
32
                # Otherwise, use file modification timestamp
 
33
                mod=$(stat -c %y "${i}")
 
34
                mod=${mod%%.*}
 
35
                d=${mod%% *}
 
36
                t=${mod##* }
 
37
                t=$(echo "${t}" | sed -e "s/:/-/g")
 
38
                _RET="${d}_${t}__"
 
39
        fi
 
40
}
 
41
 
 
42
repair() {
 
43
        if [ "$2" != "$3" ]; then
 
44
                mv "$2" "$3"
 
45
        fi
 
46
}
 
47
 
 
48
progress() {
 
49
        echo $1 $2 | awk '{printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b[%s/%s] %0.0f%%", $1, $2, 100 * $1 / $2}'
 
50
}
 
51
 
 
52
echo "INFO: Repairing file names..."
 
53
j=0
 
54
for i in $@; do
 
55
        j=$((j+1))
 
56
        file=$(basename "${i}")
 
57
        dir=$(dirname "${i}")
 
58
        if [ "${file}" = "." ] || [ "$file" = ".." ]; then
 
59
                progress ${j} $#
 
60
                continue
 
61
        elif [ ! -f "${dir}/${file}" ]; then
 
62
                progress ${j} $#
 
63
                continue
 
64
        elif echo "${file}" | grep -qs "^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}__"; then
 
65
                progress ${j} $#
 
66
                continue
 
67
                # TODO: Support --force, to update existing prefix
 
68
                #newfile=${file##*__}
 
69
        else
 
70
                newfile="${file}"
 
71
        fi
 
72
        get_prefix "${i}"
 
73
        prefix="${_RET}"
 
74
        dest="${dir}/${prefix}${newfile}"
 
75
        repair "${prefix}" "${dir}/${file}" "${dest}"
 
76
        progress ${j} $#
 
77
done
 
78
echo