summaryrefslogtreecommitdiffstats
path: root/gomfload.sh
blob: d3963f97fe95e53b9749e9da51b51466d831fda9 (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
77
78
79
80
81
82
83
84
85
#!/bin/sh

# This software is released into public domain.
# It is provided "as is", without warranties or conditions of any kind.
# Anyone is free to use, modify, redistribute and do anything with this software.

# requires: POSIX sh, curl, tr

host="${GOMF_HOST:-https://example.com/}"

prog="$(basename "$0")"
stdin=1

usage() {
	printf "Usage: %s < file\n" "$prog"
	printf "       command | %s\n" "$prog"
	printf "       %s file1 file2 file3 ...\n" "$prog"
	exit
}

upload() {
	file=''
	if [ $# -ge 1 ]; then
		filename="$(printf "%s" "$(basename "$1")" | tr -c -d 'a-zA-Z0-9_.+ -')"
		if [ -n "$filename" ]; then
			file=";filename=$filename"
		fi
	fi
	url="$(curl --progress-bar -F "files[]=@-$file" "${host%/}/upload.php?output=gyazo")"
	case "$url" in
		http*)
			printf "%s\n" "$url"
			;;
		?*)
			printf "%s: error: %s\n" "$prog" "$url" >&2
			return 1
			;;
		'')
			return 1
			;;
	esac
}

upload_file() {
	stdin=0
	url="$(upload "$1" < "$1")"
	if [ $? -ne 0 ]; then
		exit 1
	fi
	printf "%s: %s\n" "$1" "$url"
}

while [ $# -gt 0 ]; do
	case "$1" in
		-h|--help)
			usage
			;;

		--)
			shift
			break
			;;

		-?*)
			printf "%s: unrecognized option '%s'\n" "$prog" "$1" >&2
			exit 2
			;;

		*)
			upload_file "$1"
			;;
	esac

	shift
done

while [ $# -gt 0 ]; do
	upload_file "$1"
	shift
done

if [ "$stdin" -ne 0 ]; then
	upload
	exit $?
fi