-
Notifications
You must be signed in to change notification settings - Fork 0
/
_dm-helper.sh
executable file
·159 lines (133 loc) · 3.96 KB
/
_dm-helper.sh
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
#!/usr/bin/env bash
# Script name: _dm-helper
# Description: helper-script for the other scripts in the collection
# Dependencies:
# GitLab: https://www.gitlab.com/dwt1/dmscripts
# License: https://www.gitlab.com/dwt1/dmscripts/LICENSE
# Contributors: Simon Ingelsson
# HostGrady
# aryak1
set -euo pipefail
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This is a helper-script it does not do anything on its own."
exit 1
fi
###########################
# Configuration stuff #
###########################
get_local_config() {
# Do some subshell magic finding out where the script we are running
# is located and checking if ../config is a dir relative to the script
echo "$(
cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo ".")")./"
if [[ -d "${PWD}/config" ]]; then
echo "${PWD}/config"
fi
)"
}
get_config() {
local _config_files=()
local _local_conf
_local_conf="$(get_local_config)"
# add User config path
_config_files+=( "${HOME}/.config/dmscripts/config" )
# Add git-repo relative config path (if exits)
[[ -f "${_local_conf}/config" ]] && _config_files+=( "${_local_conf}/config" )
# Add global installed config path
_config_files+=( "/etc/dmscripts/config" )
for conf in "${_config_files[@]}"; do
if [[ -f ${conf} ]]; then
echo "${conf}"
return
break
fi
done
}
# Check if config has updates that should be displayed to the user
check_updated_config() {
local _base_file
local _config_file
_base_file=-1
[[ -f /etc/dmscripts/config ]] && _base_file="/etc/dmscripts/config"
_local_conf="$(get_local_config)"
[[ -f "${_local_conf}/config" ]] && _base_file=${_local_conf}/config
_config_file=$(get_config)
[[ "${_config_file}" == "${_base_file}" ]] && return
_config_file_revision=$(grep "^_revision=" "${_config_file}")
_base_file_revision=$(grep "^_revision=" "${_base_file}")
if [[ ! "${_config_file_revision}" == "${_base_file_revision}" ]] ; then
diff -y "${_config_file}" "${_base_file}" | less
echo "${_config_file} > ${_base_file}"
echo "New revision of the configuration detected, please review and set ${_base_file_revision} in ${_config_file} when done"
fi
}
######################
# Error handling #
######################
# Simple warn function
warn () {
printf 'Warn: %s\n' "$1"
}
# Simple error function
err () {
printf 'Error: %s\n' "$1"
exit 1
}
############################
# Dislay server checks #
############################
# Boiler code for if you want to do something with display servers
#function() {
# case "$XDG_SESSION_TYPE" in
# 'x11') something with x;;
# 'wayland') something with wayland;;
# *) err "Unknown display server";;
# esac
#}
# Function to copy to clipboard with different tools depending on the display server
cp2cb() {
case "$XDG_SESSION_TYPE" in
'x11') xclip -r -selection clipboard;;
'wayland') wl-copy -n;;
*) err "Unknown display server";;
esac
}
grep-desktop() {
case "$XDG_SESSION_TYPE" in
'x11') grep "Name=" /usr/share/xsessions/*.desktop | cut -d'=' -f2;;
'wayland') grep "Name=" /usr/share/wayland-sessions/*.desktop | cut -d'=' -f2 || grep "Name=" /usr/share/xsessions/*.desktop | grep -i "wayland" | cut -d'=' -f2 | cut -d' ' -f1;;
*) err "Unknown display server";;
esac
}
###############
# Parsing #
###############
xmlgetnext () {
local IFS='>'
# we need to mangle backslashes for this to work
# shellcheck disable=SC2162
read -d '<' TAG VALUE
}
parse_rss() {
echo "$1" | while xmlgetnext ; do
case $TAG in
'entry')
title=''
link=''
published=''
;;
'media:title')
title="$VALUE"
;;
'yt:videoId')
link="$VALUE"
;;
'published')
published="$(date --date="${VALUE}" "+%Y-%m-%d %H:%M")"
;;
'/entry')
echo " ${published} | ${link} | ${title}"
;;
esac
done
}