-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·171 lines (139 loc) · 3.83 KB
/
setup.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
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
# Author: Abid Sulaiman
# Title: Setup Script for Sound Control
# Description:
# Script to setup Sound Control Script
# - Set default sink and sources.
# - Set speaker and headphone port.
set -eo pipefail
id_sink=0
id_source=1
id_speaker=0
id_headphone=1
string_sink_source=("sink" "source")
string_speaker_headphone=("speaker" "headphone")
# Change argument separator
IFS=$'\n'
show_help() {
cat <<EOF
Setup Script for Sound Control Script.
Usage: $(basename $0)
EOF
exit 1
}
set_new_default() {
local string=${string_sink_source[$1]}
# Get list of sinks/sources.
local list=($(pactl list short ${string}s | cut -f 1-2))
local list_count=${#list[@]}
# Select which sink/source to use as default sink/source.
while true; do
echo "Please select one of the ${string}s to be the default $string?"
for item in ${list[@]}; do
echo "$item"
done
echo -n "Your choice (0-$(($list_count - 1)))> "
read REPLY
local re='^[0-9]$'
if ! [[ $REPLY =~ $re && $REPLY -lt $list_count ]]; then
echo
echo "Invalid choice"
else
local choice=$(echo ${list[$REPLY]} | cut -f 2)
break
fi
done
# Set default sink/source.
echo "Setting default $string to $choice"
pactl set-default-${string} $choice
default_sink_source[$1]=$choice
}
ask_default() {
local string=${string_sink_source[$1]}
local default=${default_sink_source[$1]}
while true; do
echo "Would you like to use "$default" as the default $string?"
echo -n "Your choice (y/N)> "
read REPLY
if ! [[ $REPLY =~ ^y|N$ ]]; then
echo # blank line
echo "Invalid choice"
elif [[ $REPLY =~ ^N$ ]]; then
echo # blank line
set_new_default $1
break
else
break
fi
done
echo # blank line
}
assign_speaker_headphone() {
# Check how many ports available for the default sink.
local ports=($(
pactl list sinks |
sed -n "/Name: ${default_sink_source[$id_sink]}$/,/Active Port/{/Ports/,//p}" |
sed -E '1d;$d;s/^\s+([a-z-]+):.*$/\1/'
))
local port_count=${#ports[@]}
if [[ ${#ports[@]} -eq 0 ]]; then
echo "This sink have no port. This must be some kind of error"
exit 1
elif [[ ${#ports[@]} -eq 1 ]]; then
echo "This sink have only one (1) port. This program might not benefit you."
exit 1
fi
# Assign speaker and headphone.
for str in ${string_speaker_headphone[@]}; do
while true; do
echo "Which port should be set as $str port?"
local ct=0
for p in ${ports[@]}; do
echo "$((ct++)) $p"
done
echo -n "Your choice (0-$(($port_count - 1)))> "
read REPLY
local re='^[0-9]$'
if ! [[ $REPLY =~ $re && $REPLY -lt $port_count ]]; then
echo
echo "Invalid choice"
else
local choice=${ports[$REPLY]}
echo "Setting $str to $choice"
speaker_headphone+=($choice)
break
fi
done
echo # blank line
done
}
# Check if argument given.
[[ -n $1 ]] && show_help
# Check if pactl command exist.
if ! which pactl >/dev/null; then
echo "pactl is not installed"
exit 1
fi
# Get default sink and source.
default_sink_source=($(pactl info | sed -nE -e '/Sink/,/Source/p'))
default_sink_source=("${default_sink_source[@]##*: }")
# Set default sink and source.
ask_default $id_sink
ask_default $id_source
# Assigned which one is speaker and headphone.
assign_speaker_headphone
echo "Default Sink: ${default_sink_source[$id_sink]}"
echo "Default Source: ${default_sink_source[$id_source]}"
echo # blank line
echo "Speaker Port: ${speaker_headphone[$id_speaker]}"
echo "Headphone Port: ${speaker_headphone[$id_headphone]}"
echo # blank line
# Replace config inside script.
sed -i -E "
s/^(outputSink=).*\$/\1\"${default_sink_source[$id_sink]}\"/;
s/^(inputSource=).*\$/\1\"${default_sink_source[$id_source]}\"/;
s/^(speakerPort=).*\$/\1\"${speaker_headphone[$id_speaker]}\"/;
s/^(headphonePort=).*\$/\1\"${speaker_headphone[$id_headphone]}\"/;
" ./sound-ctl.sh
echo "Setup finished successfully"
exit 0