summaryrefslogtreecommitdiff
path: root/scripts/shot
blob: 67ad84944c4736c3859e4e3a85c1f5101478ddba (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
75
76
#!/bin/sh

help() {
cat << EOF
${0}: Wrapper script to take screenshots

options:
 -d [File] 	Write to File
 -s 		Select the area with the cursor
 -h 		Print this message and exit

NOTE: save directory is ~/pic/screenshots
EOF
}

err() {
	for line in "$@"; do
		echo "${line}" >&2
	done

	exit 1
}

run() {
	if ! ${1} > /dev/null 2>&1; then
		err "${2}"
	fi

	[ -n "${3}" ] && echo "${3}"
	exit 0
}

if ! command -v scrot > /dev/null 2>&1; then
	err "${0}: scrot must be installed"
fi

default_dir="${HOME}"/pics/screenshots

s_flg=""
while getopts "d:sh" option; do
	case "${option}" in
	d) input="${OPTARG}" ;;

	s) s_flg="1" ;;

	h) help; exit 0 ;;

	*) err "Try '${0} -h' for help" ;;

	esac
done

shift $((OPTIND - 1))

[ ${#} != 0 ] && err "${0}: Invalid usage" "Try '${0} -h' for help."

if [ -z "${input}" ]; then
	outfile="${default_dir}"/%b%d::%H%M%S.png
else
	if [ -d "${input}" ]; then
		outfile="${input}"/%b%d::%H%M%S.png
	else
		outfile="${input}"
	fi
fi

if [ ! -e "$(dirname "${outfile}")" ]; then
	mkdir -pv "$(dirname "${outfile}")" > /dev/null 2>&1 \
	|| err "${0}: Failed to create directory"
fi

if [ "${s_flg}" = "1" ]; then
	run "scrot -zs ${outfile}" "Failed to screenshot"
else
	run "scrot -z ${outfile}" "Failed to screenshot"
fi