├── README.md ├── clock ├── toggletouchpad ├── toggletouchpad_error └── vpnmanager /README.md: -------------------------------------------------------------------------------- 1 | # Quality of Life bash scripts 2 | 3 | These are just some bash scripts I wrote for myself to make my life a bit easier. I also decided to write them in bash as a challenge and to learn more about bash. 4 | 5 | You can use the scripts as well if you want. 6 | 7 | Perhaps I'll be adding some more here soon. 8 | 9 | ## Current list of scripts and what they do 10 | 11 | - toggletouchpad - Toggles the touchpad 12 | - vpnmanager - A CLI manager for VPNs 13 | - clock - A CLI clock app 14 | 15 | 16 | ## "Installation" process 17 | 18 | Download this repository. 19 | 20 | ```bash 21 | git clone https://github.com/CodyMarkix/QOLBashScripts 22 | ``` 23 | 24 | Then copy the path to the folder (including the folder!) and add it to path in your .bash/zsh/fish/whateverrc using your favorite editor. 25 | 26 | ```bash 27 | export PATH="$PATH:/path/to/the/cloned/repo" 28 | ``` 29 | 30 | Open a terminal and verify that you have done it successfully by doing: 31 | 32 | ```bash 33 | echo $PATH 34 | ``` 35 | 36 | If you see the path to the cloned repo, well done! 37 | 38 | DEBs / PKGBUILDs / RPMs on my to-do list! 39 | 40 | ## To-do list 41 | 42 | - [ ] Create DEBs / PKGBUILDs / RPMs 43 | - [ ] Add automatic updates (Optional updates of course!) 44 | - [ ] Comment my code (Not doing this along the clock commit as it's 22:37 in the evening and I want to go to bed already) 45 | -------------------------------------------------------------------------------- /clock: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # --------------------------------- 4 | # | | 5 | # | WARNING | 6 | # | | 7 | # | This script is incomplete | 8 | # | and won't work properly | 9 | # --------------------------------- 10 | 11 | firstsetup () { 12 | echo "Preparing first time setup..." 13 | 14 | # Creates the config folder 15 | mkdir -p "$HOME/.config/qolbashscripts/clock" 16 | cd "$HOME/.config/qolbashscripts/clock" || exit 2 17 | 18 | # Creates the actual config file and declares the variable for it 19 | touch config.conf 20 | config_file="$HOME/.config/qolbashscripts/clock/config.conf" 21 | config_folder="$HOME/.config/qolbashscripts/clock" 22 | 23 | # A default config 24 | default_config="# A default config file for 25 | # the clock script. Check out the GitHub repo. 26 | # https://github.com/CodyMarkix/QOLBashScripts 27 | 28 | # The format that the hours, minutes and seconds are displayed in. 29 | # The default/fallback option is 24-hour format 30 | hourformat=24 31 | 32 | # The frames of the animated clock 33 | frame001=\"asciiart/frame1\" 34 | frame002=\"asciiart/frame2\" 35 | frame003=\"asciiart/frame3\" 36 | frame004=\"asciiart/frame4\" 37 | frame005=\"asciiart/frame5\" 38 | frame006=\"asciiart/frame6\" 39 | frame007=\"asciiart/frame7\" 40 | frame008=\"asciiart/frame8\" 41 | frame009=\"asciiart/frame9\" 42 | frame010=\"asciiart/frame10\" 43 | 44 | # If anyone 45 | frame011=\"asciiart/frame11\" 46 | frame012=\"asciiart/frame12\" 47 | frame013=\"asciiart/frame13\" 48 | frame014=\"asciiart/frame14\" 49 | frame015=\"asciiart/frame15\" 50 | frame016=\"asciiart/frame16\" 51 | frame017=\"asciiart/frame17\" 52 | frame018=\"asciiart/frame18\" 53 | frame019=\"asciiart/frame19\" 54 | 55 | # knows how 56 | frame020=\"asciiart/frame20\" 57 | frame021=\"asciiart/frame21\" 58 | frame022=\"asciiart/frame22\" 59 | frame023=\"asciiart/frame23\" 60 | frame024=\"asciiart/frame24\" 61 | frame025=\"asciiart/frame25\" 62 | frame026=\"asciiart/frame26\" 63 | frame027=\"asciiart/frame27\" 64 | frame028=\"asciiart/frame28\" 65 | frame029=\"asciiart/frame29\" 66 | 67 | # to manage 68 | frame030=\"asciiart/frame30\" 69 | frame031=\"asciiart/frame31\" 70 | frame032=\"asciiart/frame32\" 71 | frame033=\"asciiart/frame33\" 72 | frame034=\"asciiart/frame34\" 73 | frame035=\"asciiart/frame35\" 74 | frame036=\"asciiart/frame36\" 75 | frame037=\"asciiart/frame37\" 76 | frame038=\"asciiart/frame38\" 77 | frame039=\"asciiart/frame39\" 78 | 79 | # this better 80 | frame040=\"asciiart/frame40\" 81 | frame041=\"asciiart/frame41\" 82 | frame042=\"asciiart/frame42\" 83 | frame043=\"asciiart/frame43\" 84 | frame044=\"asciiart/frame44\" 85 | frame045=\"asciiart/frame45\" 86 | frame046=\"asciiart/frame46\" 87 | frame047=\"asciiart/frame47\" 88 | frame048=\"asciiart/frame48\" 89 | frame049=\"asciiart/frame49\" 90 | 91 | # please 92 | frame050=\"asciiart/frame50\" 93 | frame051=\"asciiart/frame51\" 94 | frame052=\"asciiart/frame52\" 95 | frame053=\"asciiart/frame53\" 96 | frame054=\"asciiart/frame54\" 97 | frame056=\"asciiart/frame55\" 98 | frame056=\"asciiart/frame56\" 99 | frame057=\"asciiart/frame57\" 100 | frame058=\"asciiart/frame58\" 101 | frame059=\"asciiart/frame59\" 102 | 103 | # message me on reddit - u/Hplr63 :( 104 | frame060=\"asciiart/frame60\" 105 | frame061=\"asciiart/frame61\" 106 | frame062=\"asciiart/frame62\" 107 | frame063=\"asciiart/frame63\" 108 | frame064=\"asciiart/frame64\" 109 | " 110 | # Applying the default config to the config file 111 | echo "$default_config" > "$config_file" 112 | 113 | mkdir -p "$HOME/.config/qolbashscripts/clock/asciiart" 114 | cd "$HOME/.config/qolbashscripts/clock/asciiart" || exit 2 115 | 116 | touch frame1 && touch frame2 && touch frame3 && touch frame4 && touch frame5 && touch frame6 117 | touch frame7 && touch frame8 && touch frame9 && touch frame10 && touch frame11 && touch frame12 118 | 119 | } 120 | 121 | checkmins () { 122 | if [[ "$(date +%M)" == "00" ]] || [[ "$(date +%M)" == "01" ]] || [[ "$(date +%M)" == "02" ]] || [[ "$(date +%M)" == "03" ]] || [[ "$(date +%M)" == "04" ]] || [[ "$(date +%M)" == "05" ]]; then 123 | cat "${config_folder}/asciiart/frame$1" 124 | 125 | elif [[ "$(date +%M)" == "06" ]] || [[ "$(date +%M)" == "07" ]] || [[ "$(date +%M)" == "08" ]] || [[ "$(date +%M)" == "09" ]]; then 126 | cat "${config_folder}/asciiart/frame$2" 127 | 128 | elif [[ "$(date +%M)" == "10" ]] || [[ "$(date +%M)" == "11" ]] || [[ "$(date +%M)" == "12" ]] || [[ "$(date +%M)" == "13" ]] || [[ "$(date +%M)" == "14" ]] || [[ "$(date +%M)" == "15" ]]; then 129 | cat "${config_folder}/asciiart/frame$3" 130 | 131 | elif [[ "$(date +%M)" == "16" ]] || [[ "$(date +%M)" == "17" ]] || [[ "$(date +%M)" == "18" ]] || [[ "$(date +%M)" == "19" ]]; then 132 | cat "${config_folder}/asciiart/frame$4" 133 | 134 | elif [[ "$(date +%M)" == "20" ]] || [[ "$(date +%M)" == "21" ]] || [[ "$(date +%M)" == "22" ]] || [[ "$(date +%M)" == "23" ]] || [[ "$(date +%M)" == "24" ]] || [[ "$(date +%M)" == "25" ]]; then 135 | cat "${config_folder}/asciiart/frame$5" 136 | 137 | elif [[ "$(date +%M)" == "26" ]] || [[ "$(date +%M)" == "27" ]] || [[ "$(date +%M)" == "28" ]] || [[ "$(date +%M)" == "29" ]]; then 138 | cat "${config_folder}/asciiart/frame$6" 139 | 140 | elif [[ "$(date +%M)" == "30" ]] || [[ "$(date +%M)" == "31" ]] || [[ "$(date +%M)" == "32" ]] || [[ "$(date +%M)" == "33" ]] || [[ "$(date +%M)" == "34" ]] || [[ "$(date +%M)" == "35" ]]; then 141 | cat "${config_folder}/asciiart/frame$7" 142 | 143 | elif [[ "$(date +%M)" == "36" ]] || [[ "$(date +%M)" == "37" ]] || [[ "$(date +%M)" == "38" ]] || [[ "$(date +%M)" == "39" ]]; then 144 | cat "${config_folder}/asciiart/frame$8" 145 | 146 | elif [[ "$(date +%M)" == "40" ]] || [[ "$(date +%M)" == "41" ]] || [[ "$(date +%M)" == "42" ]] || [[ "$(date +%M)" == "43" ]] || [[ "$(date +%M)" == "44" ]] || [[ "$(date +%M)" == "45" ]]; then 147 | cat "${config_folder}/asciiart/frame$9" 148 | 149 | elif [[ "$(date +%M)" == "46" ]] || [[ "$(date +%M)" == "47" ]] || [[ "$(date +%M)" == "48" ]] || [[ "$(date +%M)" == "49" ]]; then 150 | cat "${config_folder}/asciiart/frame${10}" 151 | 152 | elif [[ "$(date +%M)" == "50" ]] || [[ "$(date +%M)" == "51" ]] || [[ "$(date +%M)" == "52" ]] || [[ "$(date +%M)" == "53" ]] || [[ "$(date +%M)" == "54" ]] || [[ "$(date +%M)" == "55" ]]; then 153 | cat "${config_folder}/asciiart/frame${11}" 154 | 155 | elif [[ "$(date +%M)" == "56" ]] || [[ "$(date +%M)" == "57" ]] || [[ "$(date +%M)" == "58" ]] || [[ "$(date +%M)" == "59" ]]; then 156 | cat "${config_folder}/asciiart/frame${12}" 157 | fi 158 | } 159 | 160 | mainmenu () { 161 | while : 162 | do 163 | if [[ "$(date +%k)" == "00" ]] || [[ "$(date +%k)" == "12" ]]; then 164 | checkmins 1 2 3 4 5 6 7 8 165 | 166 | elif [[ "$(date +%k)" == "01" ]] || [[ "$(date +%k)" == "02" ]] || [[ "$(date +%k)" == "13" ]] || [[ "$(date +%k)" == "14" ]]; then 167 | checkmins 9 10 11 12 13 14 15 16 168 | 169 | elif [[ "$(date +%k)" == "03" ]] || [[ "$(date +%k)" == "15" ]]; then 170 | checkmins 17 18 19 20 21 22 23 24 171 | 172 | elif [[ "$(date +%k)" == "04" ]] || [[ "$(date +%k)" == "05" ]] || [[ "$(date +%k)" == "16" ]] || [[ "$(date +%k)" == "17" ]]; then 173 | checkmins 25 26 27 28 29 30 31 32 174 | 175 | elif [[ "$(date +%k)" == "06" ]] || [[ "$(date +%k)" == "18" ]]; then 176 | checkmins 33 34 35 36 37 38 39 40 177 | 178 | elif [[ "$(date +%k)" == "07" ]] || [[ "$(date +%k)" == "08" ]] || [[ "$(date +%k)" == "19" ]] || [[ "$(date +%k)" == "20" ]]; then 179 | checkmins 41 42 43 44 45 46 47 48 180 | 181 | elif [[ "$(date +%k)" == "09" ]] || [[ "$(date +%k)" == "21" ]]; then 182 | checkmins 49 50 51 52 53 54 55 56 183 | 184 | elif [[ "$(date +%k)" == "10" ]] || [[ "$(date +%k)" == "11" ]] || [[ "$(date +%k)" == "22" ]] || [[ "$(date +%k)" == "23" ]]; then 185 | checkmins 57 58 59 60 61 62 63 64 186 | fi 187 | 188 | printf "\nCurrent time: %s\nDate: %s\nTime Zone: %s" "$(${clockformat})" "$(date "+%e-%m-%Y")" "UTC $(date +%:z)" 189 | 190 | sleep 1 191 | clear 192 | done 193 | } 194 | 195 | 196 | main () { 197 | if [ -f "$HOME/.config/qolbashscripts/clock/config.conf" ]; then 198 | 199 | printf "[...] Setting config variables\n" 200 | config_file="$HOME/.config/qolbashscripts/clock/config.conf" 201 | config_folder="$HOME/.config/qolbashscripts/clock" 202 | if [[ "$(grep hourformat "${config_file}" | sed 's/hourformat=//g')" == "24" ]]; then 203 | clockformat="date +%k:%M:%S" 204 | elif [[ "$(grep hourformat "${config_file}" | sed 's/hourformat=//g')" == "12" ]]; then 205 | clockformat="date +%I:%M:%S\ %p" 206 | elif [[ "$(grep hourformat "${config_file}" | sed 's/hourformat=//g')" == "unix" ]]; then 207 | clockformat="date +%s" 208 | else 209 | clockformat="date +%k:%M:%S" 210 | fi 211 | printf "[OK] Set config variables\n" 212 | 213 | printf "[...] Registering clock frames\n" 214 | frame1="$(grep frame001 "${config_file}" | sed 's/frame001=//g')" 215 | frame2="$(grep frame002 "${config_file}" | sed 's/frame002=//g')" 216 | frame3="$(grep frame003 "${config_file}" | sed 's/frame003=//g')" 217 | frame4="$(grep frame004 "${config_file}" | sed 's/frame004=//g')" 218 | frame5="$(grep frame005 "${config_file}" | sed 's/frame005=//g')" 219 | frame6="$(grep frame006 "${config_file}" | sed 's/frame006=//g')" 220 | frame7="$(grep frame007 "${config_file}" | sed 's/frame007=//g')" 221 | frame8="$(grep frame008 "${config_file}" | sed 's/frame008=//g')" 222 | frame9="$(grep frame009 "${config_file}" | sed 's/frame009=//g')" 223 | printf "[OK] Registered 9/64 frames\n" 224 | 225 | frame10="$(grep frame010 "${config_file}" | sed 's/frame010=//g')" 226 | frame11="$(grep frame011 "${config_file}" | sed 's/frame011=//g')" 227 | frame12="$(grep frame012 "${config_file}" | sed 's/frame012=//g')" 228 | frame13="$(grep frame013 "${config_file}" | sed 's/frame013=//g')" 229 | frame14="$(grep frame014 "${config_file}" | sed 's/frame014=//g')" 230 | frame15="$(grep frame015 "${config_file}" | sed 's/frame015=//g')" 231 | frame16="$(grep frame016 "${config_file}" | sed 's/frame016=//g')" 232 | frame17="$(grep frame017 "${config_file}" | sed 's/frame017=//g')" 233 | frame18="$(grep frame018 "${config_file}" | sed 's/frame018=//g')" 234 | frame19="$(grep frame019 "${config_file}" | sed 's/frame019=//g')" 235 | printf "[...] Registered 19/64 frames\n" 236 | 237 | frame20="$(grep frame020 "${config_file}" | sed 's/frame020=//g')" 238 | frame21="$(grep frame021 "${config_file}" | sed 's/frame021=//g')" 239 | frame22="$(grep frame022 "${config_file}" | sed 's/frame022=//g')" 240 | frame23="$(grep frame023 "${config_file}" | sed 's/frame023=//g')" 241 | frame24="$(grep frame024 "${config_file}" | sed 's/frame024=//g')" 242 | frame25="$(grep frame025 "${config_file}" | sed 's/frame025=//g')" 243 | frame26="$(grep frame026 "${config_file}" | sed 's/frame026=//g')" 244 | frame27="$(grep frame027 "${config_file}" | sed 's/frame027=//g')" 245 | frame28="$(grep frame028 "${config_file}" | sed 's/frame028=//g')" 246 | frame29="$(grep frame029 "${config_file}" | sed 's/frame029=//g')" 247 | printf "[...] Registered 29/64 frames\n" 248 | 249 | frame30="$(grep frame030 "${config_file}" | sed 's/frame030=//g')" 250 | frame31="$(grep frame031 "${config_file}" | sed 's/frame031=//g')" 251 | frame32="$(grep frame032 "${config_file}" | sed 's/frame032=//g')" 252 | frame33="$(grep frame033 "${config_file}" | sed 's/frame033=//g')" 253 | frame34="$(grep frame034 "${config_file}" | sed 's/frame034=//g')" 254 | frame35="$(grep frame035 "${config_file}" | sed 's/frame035=//g')" 255 | frame36="$(grep frame036 "${config_file}" | sed 's/frame036=//g')" 256 | frame37="$(grep frame037 "${config_file}" | sed 's/frame037=//g')" 257 | frame38="$(grep frame038 "${config_file}" | sed 's/frame038=//g')" 258 | frame39="$(grep frame039 "${config_file}" | sed 's/frame039=//g')" 259 | printf "[...] Registered 39/64 frames\n" 260 | 261 | frame40="$(grep frame040 "${config_file}" | sed 's/frame040=//g')" 262 | frame41="$(grep frame041 "${config_file}" | sed 's/frame041=//g')" 263 | frame42="$(grep frame042 "${config_file}" | sed 's/frame042=//g')" 264 | frame43="$(grep frame043 "${config_file}" | sed 's/frame043=//g')" 265 | frame44="$(grep frame044 "${config_file}" | sed 's/frame044=//g')" 266 | frame45="$(grep frame045 "${config_file}" | sed 's/frame045=//g')" 267 | frame46="$(grep frame046 "${config_file}" | sed 's/frame046=//g')" 268 | frame47="$(grep frame047 "${config_file}" | sed 's/frame047=//g')" 269 | frame48="$(grep frame048 "${config_file}" | sed 's/frame048=//g')" 270 | frame49="$(grep frame049 "${config_file}" | sed 's/frame049=//g')" 271 | printf "[...] Registered 49/64 frames\n" 272 | 273 | frame50="$(grep frame050 "${config_file}" | sed 's/frame050=//g')" 274 | frame51="$(grep frame051 "${config_file}" | sed 's/frame051=//g')" 275 | frame52="$(grep frame052 "${config_file}" | sed 's/frame052=//g')" 276 | frame53="$(grep frame053 "${config_file}" | sed 's/frame053=//g')" 277 | frame54="$(grep frame054 "${config_file}" | sed 's/frame054=//g')" 278 | frame55="$(grep frame055 "${config_file}" | sed 's/frame055=//g')" 279 | frame56="$(grep frame056 "${config_file}" | sed 's/frame056=//g')" 280 | frame57="$(grep frame057 "${config_file}" | sed 's/frame057=//g')" 281 | frame58="$(grep frame058 "${config_file}" | sed 's/frame058=//g')" 282 | frame59="$(grep frame059 "${config_file}" | sed 's/frame059=//g')" 283 | printf "[...] Registered 59/64 frames\n" 284 | 285 | frame60="$(grep frame060 "${config_file}" | sed 's/frame060=//g')" 286 | frame61="$(grep frame061 "${config_file}" | sed 's/frame061=//g')" 287 | frame62="$(grep frame062 "${config_file}" | sed 's/frame062=//g')" 288 | frame63="$(grep frame063 "${config_file}" | sed 's/frame063=//g')" 289 | frame64="$(grep frame064 "${config_file}" | sed 's/frame064=//g')" 290 | printf "[OK] Registered all frames\n" 291 | 292 | clear 293 | mainmenu 294 | 295 | else 296 | firstsetup 297 | echo "a" 298 | fi 299 | } 300 | 301 | main 302 | -------------------------------------------------------------------------------- /toggletouchpad: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | errorscript="$HOME/Coding-Projects/QOLbashscripts/toggletouchpad_error.sh" 4 | 5 | firstsetup () { 6 | printf "Preparing first time setup...\n" 7 | 8 | mkdir -p "$HOME/.config/qolbashscripts/toggletouchpad" 9 | cd "$HOME/.config/qolbashscripts/toggletouchpad" || exit 2 10 | 11 | 12 | printf "%s\n\nPlease enter the id of your touchpad.\nThis has to be done only once.\n" "$(xinput list)" 13 | read -r touchpadid 14 | 15 | touch config.conf 16 | config_file="$HOME/.config/qolbashscripts/toggletouchpad/config.conf" 17 | 18 | default_config="# Default config for the toggletouchpad script. 19 | # Only really necessary for the device id right now 20 | # but I might add more later. 21 | # Check out the GitHub repo https://github.com/CodyMarkix/QOLBashScripts 22 | 23 | touchpadid=${touchpadid} 24 | " 25 | echo "${default_config}" > "${config_file}" 26 | cd "$scriptdir" || return 27 | 28 | echo "Now you can bind this script to whichever keybinding you want!" 29 | } 30 | 31 | toggle () { 32 | if [[ "$(xinput --list-props "${touchpadid}" | grep "Device Enabled")" == " Device Enabled (155): 1" ]]; 33 | then 34 | xinput disable "${touchpadid}" 35 | 36 | elif [[ "$(xinput --list-props "${touchpadid}" | grep "Device Enabled")" == " Device Enabled (155): 0" ]]; 37 | then 38 | xinput enable "$(grep touchpadid "${config_file}" | sed 's/touchpadid=//g')" 39 | else 40 | gnome-terminal -e "${errorscript}" || konsole -e "${errorscript}" || xfce4-terminal -e "${errorscript}" 41 | fi 42 | } 43 | 44 | main () { 45 | scriptdir="$(pwd)" 46 | 47 | if [ -f "$HOME/.config/qolbashscripts/toggletouchpad/config.conf" ]; then 48 | config_file="$HOME/.config/qolbashscripts/toggletouchpad/config.conf" 49 | touchpadid="$(grep touchpadid "${config_file}" | sed 's/touchpadid=//g')" 50 | toggle 51 | else 52 | firstsetup 53 | fi 54 | } 55 | 56 | main 57 | -------------------------------------------------------------------------------- /toggletouchpad_error: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "I have no idea, what went wrong." && read -r CONFIRMATION 3 | echo "$CONFIRMATION" -------------------------------------------------------------------------------- /vpnmanager: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | error () { 4 | # Arrays for the casual errors 5 | declare -A abnormalerrorsarr=( 6 | [01]="Bro, there is no VPN active" 7 | [02]="Hey, did you even write anything?" 8 | [03]="Woah buddy, we're managing VPNs right now" 9 | [04]="Dude, do you wanna choose a valid option?" 10 | [05]="Did you remember to register your VPN connections? (Manage -> Add VPN)" 11 | ) 12 | 13 | # Arrays for normal errors 14 | declare -A normalerrorsarr=( 15 | [01]="No VPN active" 16 | [02]="No choice recieved" 17 | [03]="Not a VPN connection" 18 | [04]="Valid option not selected" 19 | [05]="No valid VPN connections registered. (Manage -> Add VPN)" 20 | ) 21 | # If the config says normal errors are off, then casual errors are enabled. 22 | # Otherwise, normal errors are enabled. 23 | if [[ "$normalerrors" == "off" ]]; then 24 | echo "Error (Code: ${1} - ${abnormalerrorsarr[$1]})" 25 | else 26 | echo "Error (Code: ${1} - ${normalerrorsarr[$1]})" 27 | fi 28 | } 29 | 30 | firstsetup () { 31 | echo "Preparing first time setup..." 32 | 33 | # Creates the config folder 34 | mkdir -p "$HOME/.config/qolbashscripts/vpnmanager" 35 | cd "$HOME/.config/qolbashscripts/vpnmanager" || exit 2 36 | 37 | # Creates the actual config file and declares the variable for it 38 | touch config.conf 39 | config_file="$HOME/.config/qolbashscripts/vpnmanager/config.conf" 40 | 41 | # A default config 42 | default_config="# A default config file for 43 | # this script. Not much here yet. 44 | 45 | # The communication protocol that is used to connect. 46 | current_protocol=udp 47 | 48 | # Switch between normal-looking error messages and more casual ones. 49 | # normal_errors=on for normal-looking, normal_errors=off for casual mode. 50 | normal_errors=on 51 | " 52 | # Applying the default config to the config file 53 | echo "$default_config" > "$config_file" 54 | 55 | clear 56 | } 57 | 58 | # Main menu function 59 | mainmenu () { 60 | printf "What would you like to do?\n\n[0] Connect\n[1] Disconnect\n[2] Manage\n[3] Exit\n\n" 61 | read -r menuchoice 62 | 63 | if [[ "${menuchoice}" == "0" ]]; then 64 | connectmenu 65 | 66 | elif [[ "${menuchoice}" == "1" ]]; then 67 | disconnectmenu 68 | 69 | elif [[ "${menuchoice}" == "2" ]]; then 70 | optionsmenu 71 | 72 | elif [[ "${menuchoice}" == "3" ]]; then 73 | exitfunc 74 | else 75 | echo "" 76 | error "04" 77 | exit 3 78 | fi 79 | } 80 | 81 | # Function for connecting to the VPN 82 | connectmenu () { 83 | # If there is any vpns with the current protocol registered, show the vpn choice menu. 84 | # Otherwise, throw error 05 85 | if [[ -n "$(nmcli con show | grep vpn | grep "$currentprotocol")" ]]; then 86 | printf "\nSelect one of the following:\n%s\n\n" "$(nmcli con show | grep vpn | grep "$currentprotocol")" 87 | read -r vpnchoice 88 | 89 | if [[ -z "${vpnchoice}" ]]; then 90 | error "02" 91 | exit 22 92 | else 93 | nmcli con up "${vpnchoice}" 94 | fi 95 | else 96 | error "05" 97 | exit 98 | fi 99 | } 100 | 101 | disconnectmenu () { 102 | if [[ -z "$(nmcli con show --active | grep vpn | grep "$currentprotocol")" ]]; then 103 | error "01" 104 | exit 3 105 | else 106 | printf "\nSelect one of the following:\n%s\n" "$(nmcli con show --active | grep vpn)\n" 107 | read -r vpnchoicedis 108 | 109 | if [[ -z "${vpnchoicedis}" ]]; then 110 | error "02" 111 | exit 22 112 | else 113 | nmcli con down "${vpnchoicedis}" 114 | fi 115 | fi 116 | } 117 | 118 | optionsmenu () { 119 | 120 | printf "\nOptions:\n\n[0] Add VPN\n[1] Remove VPN\n[2] Change username\n[3] Change password\n[4] Change communication protocol\n[5] Back\n\n" 121 | read -r optionschoice 122 | 123 | if [[ "${optionschoice}" == "0" ]]; then 124 | addvpn 125 | 126 | elif [[ "${optionschoice}" == "1" ]]; then 127 | rmvpn 128 | 129 | elif [[ "${optionschoice}" == "2" ]]; then 130 | chusername 131 | 132 | elif [[ "${optionschoice}" == "3" ]]; then 133 | chpasswd 134 | 135 | elif [[ "${optionschoice}" == "4" ]]; then 136 | chprotocol 137 | 138 | elif [[ "${optionschoice}" == "5" ]]; then 139 | mainmenu 140 | else 141 | echo "" 142 | error "04" 143 | fi 144 | 145 | } 146 | 147 | # All the options 148 | addvpn () { 149 | printf "\n" 150 | read -r -e -p "Enter the file path: " ovpnfile 151 | nmcli con import type openvpn file "${ovpnfile}" 152 | } 153 | 154 | rmvpn () { 155 | printf "\nSelect one of the following:\n%s\n\n" "$(nmcli con show | grep vpn)" 156 | read -r connectionfordelete 157 | 158 | if [[ "${connectionfordelete}" == *".udp"* ]] || [[ "${connectionfordelete}" == *".tcp"* ]]; then 159 | nmcli con delete "${connectionfordelete}" 160 | else 161 | error "03" 162 | fi 163 | } 164 | 165 | chusername () { 166 | printf "\nSelect one of the following:\n%s\n\n" "$(nmcli con show | grep vpn)" 167 | read -r confornamechange 168 | 169 | if [[ -n "${confornamechange}" ]]; then 170 | printf "\n" 171 | read -r -p "Enter the new username: " newusername 172 | 173 | if [[ -n "${newusername}" ]]; then 174 | nmcli con modify "${confornamechange}" vpn.user-name "${newusername}" 175 | else 176 | error "02" 177 | exit 178 | fi 179 | else 180 | error "02" 181 | exit 182 | fi 183 | 184 | optionsmenu 185 | } 186 | 187 | chpasswd () { 188 | printf "\nSelect one of the following:\n%s\n\n" "$(nmcli con show | grep vpn)" 189 | read -r conforpasschange 190 | 191 | printf "\n" 192 | read -r -sp "Enter the new password: " newpassword 193 | nmcli con modify "${conforpasschange}" vpn.secrets "password=${newpassword}" 194 | 195 | optionsmenu 196 | } 197 | 198 | chprotocol () { 199 | printf "\nSelect one of the following:\n\n[0] UDP\n[1] TCP\n[2] Back\n\n" 200 | read -r protocolchoice 201 | 202 | if [[ "${protocolchoice}" == "0" ]]; then 203 | if [[ "${currentprotocol}" == "udp" ]]; then 204 | sed -i 's/current_protocol=udp/current_protocol=udp/' "$HOME/.config/vpnmanager/config.conf" 205 | elif [[ "${currentprotocol}" == "tcp" ]]; then 206 | sed -i 's/current_protocol=tcp/current_protocol=udp/' "$HOME/.config/vpnmanager/config.conf" 207 | fi 208 | elif [[ "${protocolchoice}" == "1" ]]; then 209 | if [[ "${currentprotocol}" == "udp" ]]; then 210 | sed -i 's/current_protocol=udp/current_protocol=tcp/' "$HOME/.config/vpnmanager/config.conf" 211 | elif [[ "${currentprotocol}" == "tcp" ]]; then 212 | sed -i 's/current_protocol=tcp/current_protocol=tcp/' "$HOME/.config/vpnmanager/config.conf" 213 | fi 214 | elif [[ "${protocolchoice}" == "2" ]]; then 215 | optionschoice 216 | else 217 | error "02" 218 | exit 22 219 | fi 220 | 221 | optionsmenu 222 | } 223 | 224 | # The exit function, meant for a normal exit 225 | exitfunc () { 226 | printf "\nExiting...\n" 227 | exit 0 228 | } 229 | 230 | 231 | main () { 232 | # Checking if this is the first time you're running this script 233 | if [ -f "$HOME/.config/qolbashscripts/vpnmanager/config.conf" ]; then 234 | config_file="$HOME/.config/qolbashscripts/vpnmanager/config.conf" # If not, it declares the var for the config file... 235 | 236 | if [[ "$(grep current_protocol "$config_file")" == "current_protocol=udp" ]]; then # ...goes through a bunch of config read -rs... 237 | currentprotocol="udp" 238 | elif [[ "$(grep current_protocol "$config_file")" == "current_protocol=tcp" ]]; then 239 | currentprotocol="tcp" 240 | fi 241 | 242 | if [[ "$(grep normal_errors "$config_file")" == "normal_errors=off" ]]; then 243 | normalerrors="off" 244 | elif [[ "$(grep normal_errors "$config_file")" == "normal_errors=on" ]]; then 245 | normalerrors="on" 246 | else 247 | normalerrors="on" 248 | fi 249 | 250 | mainmenu # ...and runs the main menu function 251 | else 252 | firstsetup # If it is, it goes through the firstsetup function first... 253 | mainmenu # ...and then triggers the main function 254 | fi 255 | } 256 | 257 | main --------------------------------------------------------------------------------