summaryrefslogtreecommitdiff
path: root/dot/local/bin/im
blob: a3eb0fb96c39be597cd111ce6b7863279ac451a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh

# A screen-grab will be saved in $SCREEN_GRAB_DEST_DIR if set, otherwise
# in $HOME/screen_grab/ if it exsts; otherwise $HOME.

default_basename=screen_grab
default_extension=png
date_format=+%F_%H%M%S

chdir_to_destination()
{
        local d
        d=$HOME/$default_basename
        if [ "$SCREEN_GRAB_DEST_DIR" ]
        then cd "$SCREEN_GRAB_DEST_DIR"
        elif [ -d "$d" ]
        then cd "$d"
        else cd
        fi
}

choose_destination()
{
        local stamp basename extension now
        case $# in
                1) ;;
                0) now=$(date "$date_format")
                   DESTINATION=$default_basename.$now.$default_extension
                   return
                   ;;
                *) return 1 ;;
        esac

        case "$1" in
        *.*)
                basename=${1%.*}
                extension=${1##*.}
                ;;
        *)
                basename=$1
                extension=$default_extension
                ;;
        esac
        DESTINATION=$basename.$extension
}

get_out_the_way()
{
        local stamp basename extension destination
        [ -e "$1" ] || return 0

        stamp=$(date -r "$1" "$date_format") || return
        case "$1" in
        *.*)
                basename=${1%.*}
                extension=${1##*.}
                destination=$basename.$stamp.$extension
                ;;
        *)
                destination=$1.$stamp
                ;;
        esac
        [ ! -e "$destination" ] || return
        mv "$1" "$destination"
}

set -e

chdir_to_destination
choose_destination "$@"
get_out_the_way "$DESTINATION"
import "$DESTINATION"
marginate "$DESTINATION"