A collection of scripts
1#!/bin/sh
2# shellcheck disable=SC1090,SC2154
3
4# Usage statement
5usage() {
6 printf '%s\n' "usage: ${0##*/} theme"
7}
8
9# Convert hex colors into rgb
10hex2rgb() {
11 hex=$1
12 hex_r=${hex%????}
13 hex_g=${hex#??}
14 hex_g=${hex_g%??}
15 hex_b=${hex%????}
16 printf '%d,%d,%d' "0x$hex_r" "0x$hex_g" "0x$hex_b"
17}
18
19# Set directory variables
20[ "$THM_CONFIG_DIR" ] \
21 && conf_dir="$THM_CONFIG_DIR" \
22 || conf_dir="${XDG_CONFIG_HOME:-$HOME/.config}/thm"
23[ "$THM_DEST_DIR" ] \
24 && conf_dir="$THM_DEST_DIR" \
25 || dest_dir="${XDG_CACHE_HOME:-$HOME/.cache}/thm"
26
27err() {
28 [ "$*" ] && err_msg="$*" || err_msg="error"
29 printf '%s%b' "${0##*/}: " "$err_msg\n" 1>&2
30 exit 1
31}
32
33# Ensure the theme file exist and source it
34case $1 in
35 -h|h|--help|help ) usage; exit 0 ;;
36 * ) [ "$1" ] || err "missing argument"
37 [ -f "$conf_dir/themes/$1.theme" ] || err "$1: theme not found"
38 theme_file="$conf_dir/themes/$1.theme"
39 . "$theme_file" ;;
40esac
41
42# Create RGB colors from the hex colors
43bg_color_rgb="$(hex2rgb "$bg_color")"
44fg_color_rgb="$(hex2rgb "$fg_color")"
45color0_rgb="$(hex2rgb "$color0")"
46color1_rgb="$(hex2rgb "$color1")"
47color2_rgb="$(hex2rgb "$color2")"
48color3_rgb="$(hex2rgb "$color3")"
49color4_rgb="$(hex2rgb "$color4")"
50color5_rgb="$(hex2rgb "$color5")"
51color6_rgb="$(hex2rgb "$color6")"
52color7_rgb="$(hex2rgb "$color7")"
53color8_rgb="$(hex2rgb "$color8")"
54color9_rgb="$(hex2rgb "$color9")"
55color10_rgb="$(hex2rgb "$color10")"
56color11_rgb="$(hex2rgb "$color11")"
57color12_rgb="$(hex2rgb "$color12")"
58color13_rgb="$(hex2rgb "$color13")"
59color14_rgb="$(hex2rgb "$color14")"
60color15_rgb="$(hex2rgb "$color15")"
61
62# Make sure the dest_dir exist
63[ -d "$dest_dir" ] || { mkdir -p "$dest_dir" || err "failed to create $dest_dir"; }
64
65# Ensure the dest_dir is empty before populating it
66rm "$dest_dir/"*
67
68# Make a file with the name of the new theme
69printf '%s\n' "$theme_file" > "$dest_dir/current_thm"
70
71# Repalce template syntax with the actual colors
72for t in "${conf_dir}/templates/"*".template"
73do
74 file_name=${t##*/}
75 file_name=${file_name%.template}
76
77 sed "
78 s/{bg_color}/$bg_color/g
79 s/{fg_color}/$fg_color/g
80 s/{color0}/$color0/g
81 s/{color1}/$color1/g
82 s/{color2}/$color2/g
83 s/{color3}/$color3/g
84 s/{color4}/$color4/g
85 s/{color5}/$color5/g
86 s/{color6}/$color6/g
87 s/{color7}/$color7/g
88 s/{color8}/$color8/g
89 s/{color9}/$color9/g
90 s/{color10}/$color10/g
91 s/{color11}/$color11/g
92 s/{color12}/$color12/g
93 s/{color13}/$color13/g
94 s/{color14}/$color14/g
95 s/{color15}/$color15/g
96 s/{bg_color.rgb}/$bg_color_rgb/g
97 s/{fg_color.rgb}/$fg_color_rgb/g
98 s/{color0.rgb}/$color0_rgb/g
99 s/{color1.rgb}/$color1_rgb/g
100 s/{color2.rgb}/$color2_rgb/g
101 s/{color3.rgb}/$color3_rgb/g
102 s/{color4.rgb}/$color4_rgb/g
103 s/{color5.rgb}/$color5_rgb/g
104 s/{color6.rgb}/$color6_rgb/g
105 s/{color7.rgb}/$color7_rgb/g
106 s/{color8.rgb}/$color8_rgb/g
107 s/{color9.rgb}/$color9_rgb/g
108 s/{color10.rgb}/$color10_rgb/g
109 s/{color11.rgb}/$color11_rgb/g
110 s/{color12.rgb}/$color12_rgb/g
111 s/{color13.rgb}/$color13_rgb/g
112 s/{color14.rgb}/$color14_rgb/g
113 s/{color15.rgb}/$color15_rgb/g
114 " "$t" > "${dest_dir}/${file_name}"
115done
116
117# Run extra user scripts
118[ -d "$conf_dir/scripts" ] || return 0
119for i in "$conf_dir/scripts/"*
120do
121 [ -x "$i" ] && $i > /dev/null 2>&1 &
122done