~skiqqy/bin

bin/setwp -rwxr-xr-x 2.9 KiB
c0954778Stephen Cochrane Prog on deploy 2 days ago
                                                                                
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Usage: setwp -h
# Author: Skiqqy
# This script allows the setting of a static wallpaper, or cycling through a set of wallpapers.

# Variables
set -B
t=600
args=( "$@" )

usage () {
cat << EOF
Usage: setwp [options] [file|dir]

h: Shows this message.
d: Use the pictures in the specified directory, cycle with time t (default 10 min).
t: Set time t to a specific value.
z: Make feh zoom in on the wallpapers.
f: Specify a single picture to use.
p: Show the path to the picture in the bottem left corner of the picture.
EOF
}

# Set a wallpaper with text in the bottem left corner
# Usage swptext /path/to/picure "text"
swptext () {
	rm -f /tmp/setwp_text.png
	convert "$1" -gravity SouthWest -annotate +1+3 "$2" /tmp/setwp_text.png

	if [ -z "$zoom" ]
	then
		opt=scale
	else
		opt=fill
	fi
	feh --bg-"$opt" /tmp/setwp_text.png
}

swp () {
	if [ -z "$zoom" ]
	then
		opt=scale
	else
		opt=fill
	fi

	feh --bg-"$opt" "$1"
}

# cycle /path/to/dir
cycle () {
	d=${1%/}
	if [ $(echo "$t < 1" | bc) -eq 1 ]
	then
		echo "Invalid time used: t was '$t', now set to '600'"
		t=600
	fi
	for ((;;))
	do
		for file in $d/{*.png,*.jpg,*.jpeg}
		do
			if [ -z "$path" ]
			then
				swp "$file"
			else
				swptext "$file" "$(realpath "$file")"
			fi
			sleep $t
			if [ ! $? -eq 0 ]
			then
				echo "An error occured in cycle, did you set time to a valid number?"
				exit 1
			fi
		done
	done
}

# Downloads and sets a wallpaper, provides an option to save permanently.
# Usage: download "$url"
download ()
{
	ftype=$(basename "$1" | cut -d\. -f 2)
	file=/tmp/"testwp.$ftype"
	curl -s "$1" > "$file"

	if ! swp "$file" > /dev/null 2>&1
	then
		echo "An error occured, is this a valid picture link?"
		exit $?
	fi

	read -rp "Save? Y/N: " input
	[[ ! "$input" =~ ^(y|Y)(es)?$ ]] && exit # Exit if we not saving

	read -rp "Name (Empty for random name): " name
	[ -z "$name" ] && name=$(tr -dc 0-9A-Za-z < /dev/random | head -c 15)
	read -rep "Path (Empty for curr dir): " save
	[ -z "$save" ] && save=.
	save=$(realpath "${save%/}")

	if [ -f "$save/$name.$ftype" ]
	then
		read -rp "This file already exist! Proceed? Y/N: " input
		[[ "$input" =~ ^(n|N)(o)?$ ]] && echo "Save aborted." && exit 0
	fi

	echo "Saving! $save/$name.$ftype"
	mv "$file" "$save/$name.$ftype"
	exit
}

main ()
{
	last=$((${#args[@]} - 1))
	[ "$last" -eq -1 ] && usage && exit 1
	file=${args["$last"]}
	zoom=
	path=

	while getopts "hd:t:zf:p" opt
	do
		case $opt in
			h)
				usage
				exit 0
				;;
			t)
				t="$OPTARG"
				;;
			z)
				zoom=1
				;;
			f|d)
				file="$OPTARG"
				;;
			p)
				path=1
				;;
			*)
				echo "Invalid arg"
				exit 1
				;;
		esac
	done

	[[ "$file" =~ ^http[s]?:// ]] && download "$file" # Check that it is a url

	[ -z "$file" ] && echo "Invalid file, see -h" && exit 1

	if [ -d "$file" ]
	then
		cycle "$file"
	elif [ -f "$file" ]
	then
		if [ -z "$path" ]
		then
			swp "$file"
		else
			swptext "$file" "$(realpath "$file")"
		fi
	else
		echo "<ERROR> Please check -h"
		exit 1
	fi
}

main "$@"