Last active
July 7, 2021 08:21
-
-
Save s-n-g/f5c3ecc8ee6a0ec67124c5ff26836fc5 to your computer and use it in GitHub Desktop.
A script to help you preserve config files customization from being overwritten by system updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# (C) S. Georgaras <sng@hellug.gr>, 2021 | |
# | |
# This program is free software: you can redistribute it and/or modify it | |
# under the terms of the GNU General Public License as published by the | |
# Free SoftwareFoundation, either version 3 of the License, or (at your | |
# option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, but | |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
# for more details. | |
# | |
# You should have received a copy of the GNU General Public License along | |
# with this program. If not, see http://www.gnu.org/licenses/. | |
# | |
# | |
# Changelog | |
# | |
# v 0.1 - 2021-05-27 | |
# Initial release on forum.maboxlinux.org | |
# v 0.2 - 2021-07-06 | |
# Second release as a gist | |
# Fully rewritten, adding config file, etc. | |
# v 0.3 - 2021-07-07 | |
# Adding commands section (the ability to execute user defined | |
# commands before the script terminates) | |
function usage(){ | |
echo "fix-openbox-rc-xml v. 0.3" | |
echo "(C) S. Georgaras <sng@hellug.gr>, 2021" | |
echo | |
echo "A script to help you preserve config files customization" | |
echo "from being overwritten by system updates." | |
echo | |
} | |
function help(){ | |
usage | |
echo "Usage: fix-openbox-rc-xml [options]" | |
echo | |
echo "Available options are:" | |
grep ') #' "$0" | grep -v grep | sed -e 's/^ //' -e 's/|/, /' -e 's/) #/\n /' | |
echo | |
echo "Config file: ~/.config/fix-openbox-rc-xml/config" | |
echo "Default templates dir: ~/.config/fix-openbox-rc-xml/templates" | |
} | |
function long_help(){ | |
usage | |
cat << _end_of_text | |
History | |
======= | |
This script started on a Mabox Linux system running openbox window manager, | |
as an attempt to both accept a rc.xml (openbox main configuration file) | |
update and preserve user customization. | |
Currently, this is no longer a problem on Mabox Linux, so the script has | |
evolved in a different way: trying to preserve user customization on any | |
configuration file that will be updated system wide, but has kept the same | |
name it had before. | |
The idea is to keep user customization separate, always update user | |
configuration files (with updated system wide configuration files), and | |
"insert" user customization to the updated configuration files. | |
How it all works | |
================ | |
The script reads a configuration file, which will instruct it on which | |
config files it will work on and what to do with them. | |
The config file default location is ~/.config/fix-openbox-rc-xml/config | |
The config file defines several variables: | |
FILES | |
This is a bash array which contains user customization files. | |
These files (called source or template files) are stored in the "templates" | |
directory. They contain a starting and an ending token which enclose | |
the actual customization. | |
The "templates" directory default location is | |
~/.config/fix-openbox-rc-xml/templates | |
CONFIG_FILES | |
This is a bash array which contains the config files to work on. These | |
files are also called target files. | |
START_TOKEN | |
END_TOKEN | |
These are two bash arrays which define the starting and ending tokens. As | |
stated previously, these two tokens will enclose user custom content. That | |
means that: | |
1. The tokens must be unique and different to each other. | |
2. They must be different to any content both the template file and the | |
target configuration file may contain. | |
The best way to accomplish that is to have comments act as tokens. | |
Since configuration files are most of the times written in some kind of | |
programming or markup language, valid language comments are perfectly | |
suitable for the job. | |
Token example for .bashrc | |
------------------------- | |
START_TOKEN=("# Start of bash aliases") | |
END_TOKEN=("# End of bash aliases") | |
Template file example for .bashrc | |
--------------------------------- | |
# Start of bash aliases | |
alias w=whoami | |
alias u='uname -a' | |
alias x='xdg-open .' | |
# End of bash aliases | |
INSERT_TOKEN | |
This is a bash array which contains both the action (mode character) | |
and the literal search string within the target configuration file. | |
The literal string (not a rgexp) is a unique string to be matched within | |
the target configuration file. When matched, the contents of the source | |
(template) file will be inserted at the line before or after the one | |
matched. | |
The mode character is one of the following: | |
B Insert before matched line | |
A Insert after matched line | |
^ Prepend template file contents. The template file will be added at | |
the beginning of the target configuration file. In this case the | |
literal string is not used. | |
$ Append template file contents. The template file will be added at | |
the end of the target configuration file. In this case the literal | |
string is not used. | |
Putting it all together | |
======================= | |
Ok, so we know what to do, but how do we do it? | |
Let's see how the default configuration file is constructed. If you don't | |
already have it, just run | |
fix-openbox-rc-xml -cc | |
to create it. Remember its location: | |
~/.config/fix-openbox-rc-xml/config | |
The main thing is to know what we need to do; in this case we have to | |
preserve two parts of the rc.xml file (user config), so that whenever the | |
file is updated, we can insert these two parts back to it. | |
Part 1 consists of application setting for openbox and part two consists | |
of keyboard settings. | |
First thing we need to do is write the full path of the rc.xml file (our | |
target configuration file) inside the CONFIG_FILES array, twice. | |
Second thing we do is move (copy and delete) both parts to one separate | |
file each. These files will be out template files. | |
Third thing is to declare out start and end tokens for each of the parts. The | |
tokens I have selected are a bit long and too descriptive, but they work all | |
the same. Take a look at your config file (lines starting with START_TOKEN | |
and END_TOKEN). | |
The first entry in the START_TOKEN and the first entry in the END_TOKEN | |
arrays correspond to the first part (first template file). | |
The second entry in the START_TOKEN and the second entry in the END_TOKEN | |
arrays correspond to the second part (second template file), and so on... | |
Having selected the tokens, we just insert them to the two template files; | |
the START_TOKEN should be the first line of the file and the END_TOKEN | |
should be the last one. | |
Next, we save both our template files (I have used the names | |
openbox-applications.xml and openbox-keyboard.xml respectively) and copy | |
these filenames at the FILES array of the configuration file (just insert | |
the filename not the path). | |
The last thing to do is find where to insert the template files. | |
Now, we know that rc.xml is a XML file, and we have two blocks of text that | |
need to be placed in the applications and the keyboard sections respectively. | |
We also know that the "</applications>" and "</keyboard>" tags of the XML | |
file are unique, so we can use them to insert our text there. | |
And since these are the ending tags for the corresponding sections, we | |
will insert our text BEFORE these tags. | |
Consequently, the INSERT_TOKEN for each file would be | |
B</applications> | |
and | |
B</keyboard> | |
Note: If we wanted our text to be inserted at the begining of each section, | |
we would use the following tokens: | |
A<applications> | |
and | |
A<keyboard> | |
At this point we are ready to check our configuration. we just type | |
fix-openbox-rc-xml -ls | |
and we should see something like this: | |
Navigating to "/home/user/.config/fix-openbox-rc-xml/templates" ... done | |
Looking for "openbox-applications.xml" ... found | |
Looking for "openbox-keyboard.xml" ... found | |
Source files found: 2 | |
Config listing | |
============== | |
Config file: /home/user/.config/fix-openbox-rc-xml/config | |
Templates dir: /home/user/.config/fix-openbox-rc-xml/templates | |
============== | |
Template: openbox-applications.xml | |
Config file: /home/user/.config/openbox/rc.xml | |
Start token: <!-- fix-openbox-rc-xml: Start of applications modifications - DO NOT DELETE THIS LINE --> | |
End token: <!-- fix-openbox-rc-xml: End of applications modifications - DO NOT DELETE THIS LINE --> | |
Insert token: B</applications> | |
Template: openbox-keyboard.xml | |
Config file: /home/user/.config/openbox/rc.xml | |
Start token: <!-- fix-openbox-rc-xml: Start of keyboard modifications - DO NOT DELETE THIS LINE --> | |
End token: <!-- fix-openbox-rc-xml: End of keyboard modifications - DO NOT DELETE THIS LINE --> | |
Insert token: B</keyboard> | |
Running | |
fix-openbox-rc-xml | |
will insert both parts (template files) into our rc.xml. | |
The Commands Section | |
==================== | |
The last bash array in the configuration file (called COMMAND), introduces | |
the ability to execute arbitary bash commands right before the script | |
terminates. | |
For example, the default theme used by Mabox Linux is "Mabox-superdesk", | |
but I prefer to use "Vertex-Maia-Dark". | |
I know the themes name appears only once in the configuration file, so I | |
can just use sed to substitute it with the prefered one. | |
Then, since changing the rc.xml file on a openbox environment, would not | |
take effect until openbox actually reads this changed file, I just add a | |
second command ("sleep 2; openbox --reconfigure") to have it done. | |
I hope this covers almost anything... | |
There are a couple of things I have not mentioned here, but running | |
fix-openbox-rc-xml -h | |
will guide you through it all. | |
Sorry for my English, I am a bit rusty (if anyone would like to rewrite | |
this section, he would be welcomed to do so :) ) | |
Enjoy! | |
PS. Licensing info | |
================== | |
This program is free software: you can redistribute it and/or modify it | |
under the terms of the GNU General Public License as published by the Free | |
SoftwareFoundation, either version 3 of the License, or (at your option) | |
any later version. | |
This program is distributed in the hope that it will be useful, but WITHOUT | |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
more details. | |
You should have received a copy of the GNU General Public License along | |
with this program. If not, see http://www.gnu.org/licenses/. | |
_end_of_text | |
} | |
function read_config(){ | |
if [ -e "${CONFIG}" ] | |
then | |
validate_config | |
else | |
echo "Config file not found!" | |
echo | |
echo -n "Do you want me to create a default config file for you? (Y/n) " | |
read ans | |
if [ -z "$ans" ] || [ "$ans" = "y" ] || [ "ans" = "Y" ] | |
then | |
create_config | |
else | |
echo | |
echo "Please create \"~/.config/fix-openbox-rc-xml/config\" or use the \"-cc\"" | |
echo "command line option to create the default config file, and try again." | |
exit 1 | |
fi | |
fi | |
} | |
function validate_config(){ | |
local L_FILES | |
local L_OUT_FILES | |
local L_STATR_TOKEN | |
local L_END_TOKEN | |
local COUNT | |
local token | |
local ch | |
. "${CONFIG}" || exit 1 | |
L_FILES="${#FILES[@]}" | |
L_OUT_FILES="${#CONFIG_FILES[@]}" | |
L_STATR_TOKEN="${#START_TOKEN[@]}" | |
L_END_TOKEN="${#END_TOKEN[@]}" | |
L_INSERT_TOKEN="${#INSERT_TOKEN[@]}" | |
if [ -d "${BASEDIR}" ] | |
then | |
if [ ! -w "$BASEDIR" ] | |
then | |
echo "Error in config file!!!" | |
echo " The dir \"${BASEDIR}\" is not writable..." | |
exit 1 | |
fi | |
else | |
mkdir -p "BASEDIR" 2>/dev/null | |
ans=$? | |
if (( ans != 0 )) | |
then | |
echo "Cannot create dir: \"${BASEDIR}\"..." | |
exit 1 | |
fi | |
fi | |
COUNT=0 | |
while [ ! -z "${CONFIG_FILES[$COUNT]}" ] | |
do | |
[ -s "${CONFIG_FILES[$COUNT]}" ] || { | |
echo "Error in config file!!!" | |
echo " The config file \"${CONFIG_FILES[$COUNT]}\" does not exist!" | |
exit 1 | |
} | |
((COUNT++)) | |
done | |
if [ "$L_FILES" -ne "$L_OUT_FILES" ] || \ | |
[ "$L_FILES" -ne "$L_STATR_TOKEN" ] || \ | |
[ "$L_FILES" -ne "$L_END_TOKEN" ] || \ | |
[ "$L_FILES" -ne "$L_INSERT_TOKEN" ] | |
then | |
echo "Error in config file!!!" | |
echo " Not all fields have been declared..." | |
exit 1 | |
fi | |
COUNT=0 | |
while [ ! -z "${INSERT_TOKEN[$COUNT]}" ] | |
do | |
token="${INSERT_TOKEN[$COUNT]}" | |
ch=${token:0:1} | |
if [ "$ch" != "B" ] && [ "$ch" != "A" ] && \ | |
[ "$ch" != "$" ] && [ "$ch" != "^" ] | |
then | |
echo "Error in config file!!!" | |
echo " INSERT TOKEN \"${INSERT_TOKEN[$COUNT]}\" does not start with a valid character." | |
exit 1 | |
fi | |
((COUNT++)) | |
done | |
} | |
function create_config(){ | |
mkdir -p ~/.config/fix-openbox-rc-xml 2>/dev/null || { | |
echo "Error crating config dir: \"~/.config/fix-openbox-rc-xml\"" | |
echo "Please make sure you have write permission in ~/.config" | |
echo "and that the filesystem is not full and try again." | |
exit 1 | |
} | |
cat << _end_of_text > ~/.config/fix-openbox-rc-xml/config | |
############################################################################# | |
# # | |
# fix-openbox-rc-xml config file # | |
# # | |
############################################################################# | |
# These are the template files (i.e source files) | |
# which hold your customizations | |
FILES=( \\ | |
openbox-applications.xml \\ | |
openbox-keyboard.xml \\ | |
) | |
# These are the config files (i.e. target files) | |
# you want to insert the corresponding source file | |
CONFIG_FILES=( \\ | |
~/.config/openbox/rc.xml \\ | |
~/.config/openbox/rc.xml \\ | |
) | |
# These are the tokens (i.e. comments in the language your config | |
# file is written in.) | |
# | |
# For each source file (and target file) you must have a START TOKEN | |
# and an END TOKEN. Any customization you may want to apply must reside | |
# between the START TOKEN LINE and the END TOKEN LINE. | |
# | |
# First we have the START TOKENS | |
START_TOKEN=( \\ | |
"<!-- fix-openbox-rc-xml: Start of applications modifications - DO NOT DELETE THIS LINE -->" \\ | |
"<!-- fix-openbox-rc-xml: Start of keyboard modifications - DO NOT DELETE THIS LINE -->" \\ | |
) | |
# And finally the END TOKENS | |
END_TOKEN=( \\ | |
"<!-- fix-openbox-rc-xml: End of applications modifications - DO NOT DELETE THIS LINE -->" \\ | |
"<!-- fix-openbox-rc-xml: End of keyboard modifications - DO NOT DELETE THIS LINE -->" \\ | |
) | |
# This is the way to declare where the template (source) file will | |
# be inserted in the config (target) file. | |
# | |
# Each line consists of a token character and a string within the | |
# default config (target) file. The string (when used) must be a | |
# unique string within the config (target) file. | |
# | |
# The token character can be: | |
# B : Insert the source file BEFORE the line of the target file | |
# that contains the string | |
# A : Insert the source file AFTER the line of the target file | |
# that contains the string | |
# ^ : Insert the source file at the begining of the target file | |
# In this case, no string must be declared | |
# $ : Append the source file to the end of the target file | |
# In this case, no string must be declared | |
# Example of the last two cases: | |
# INSERT_TOKEN=( \\ | |
# "$" | |
# "^" | |
# ) | |
INSERT_TOKEN=( \\ | |
"B</applications>" \\ | |
"B</keyboard>" \\ | |
) | |
# Commands section | |
# This section operates indipentetaly from all previous sections. | |
# You just write down the commands you want to be executed before the | |
# script terminates. All commands are executed through the bash shell. | |
COMMMAND=( \\ | |
"sed -i 's/Mabox-superdesk/Vertex-Maia-Dark/' ~/.config/openbox/rc.xml" \\ | |
"sleep 2; opebox --reconfigure" \\ | |
) | |
_end_of_text | |
echo | |
echo "Your default config file has been created as:" | |
echo " \"~/.config/fix-openbox-rc-xml/config\"" | |
echo "Please edit this file to your liking before using it..." | |
exit | |
} | |
function validate_source(){ | |
[ -z ${LIST_CONFIG} ] || return | |
echo -n " Looking for start token ..." | |
grep -F "${START_TOKEN[$1]}" "${FILES[$1]}" >/dev/null && { | |
echo "found" | |
} || { | |
print_source_token_not_found_end_exit $1 | |
} | |
echo -n " Looking for end token ..." | |
grep -F "${END_TOKEN[$1]}" "${FILES[$1]}" >/dev/null && { | |
echo "found" | |
} || { | |
print_source_token_not_found_end_exit $1 | |
} | |
} | |
function print_source_token_not_found_end_exit(){ | |
echo "not found" | |
echo | |
echo "Please fix your \"${FILES[$1]}\" file and try again" | |
exit 1 | |
} | |
function validate_all_target(){ | |
local COUNT | |
COUNT=0 | |
echo | |
echo "Validating target files..." | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
if [ "${ARR[$COUNT]}" = "found" ] | |
then | |
validate_target $COUNT | |
fi | |
((COUNT++)) | |
done | |
} | |
function validate_target(){ | |
local CHECK | |
echo " Looking for \"${FILES[$1]}\" tokens" | |
echo -n " in \"${CONFIG_FILES[$1]}\" ... " | |
grep -F "${START_TOKEN[$1]}" "${CONFIG_FILES[$1]}" >/dev/null && ST_TOK_FOUND+=(1) || ST_TOK_FOUND+=(0) | |
grep -F "${END_TOKEN[$1]}" "${CONFIG_FILES[$1]}" >/dev/null && END_TOK_FOUND+=(1) || END_TOK_FOUND+=(0) | |
CHECK=$((ST_TOK_FOUND[$1]+END_TOK_FOUND[$1])) | |
if (( CHECK == 1 )) | |
then | |
echo "error" | |
echo | |
echo "The starting token: \"${START_TOKEN[$1]}\" was" | |
echo -n " " | |
(( ST_TOK_FOUND[$1] == 0 )) && echo -n "NOT " | |
echo "found in \"${CONFIG_FILES[$1]}\"." | |
echo "The ending token: \"${END_TOKEN[$1]}\" was" | |
echo -n " " | |
(( END_TOK_FOUND[$1] == 0 )) && echo -n "NOT " | |
echo "found in \"${CONFIG_FILES[$1]}\"." | |
echo | |
echo "Please correct the file and try again." | |
exit 1 | |
elif (( CHECK == 0 )) | |
then | |
echo "not found" | |
TOK+=(0) | |
else | |
echo "found" | |
TOK+=(1) | |
fi | |
} | |
function create_empty_templates(){ | |
local COUNT | |
local T_FOUND | |
local ans | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
if [ -s "${FILES[$COUNT]}" ] | |
then | |
T_FOUND=1 | |
break | |
fi | |
done | |
if [ ! -z "${T_FOUND}" ] | |
then | |
echo | |
echo "Templates found:" | |
ls -la | grep -v '^d' | grep -v 'total ' | sed 's/^/ /' | |
echo "Do you really want to overwrite them?" | |
echo -n "Type \"yes\" to continue: " | |
read ans | |
if [ "${ans}" != "yes" ];then exit;fi | |
echo | |
fi | |
echo "Creating empty templates..." | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
echo -n " Template: ${FILES[$COUNT]}" | |
echo "${START_TOKEN[$COUNT]}" > "${FILES[$COUNT]}" | |
echo "${END_TOKEN[$COUNT]}" >> "${FILES[$COUNT]}" | |
echo 'done' | |
((COUNT++)) | |
done | |
} | |
function get_source_files(){ | |
local COUNT | |
COUNT=0 | |
for aFile in "${FILES[@]}" | |
do | |
echo -n " Looking for \"${aFile}\" ... " | |
if [ -s "${aFile}" ] | |
then | |
echo found | |
ARR+=("found") | |
((FOUND_FILES++)) | |
validate_source $COUNT | |
else | |
echo "not found" | |
ARR+=("not found") | |
fi | |
((COUNT++)) | |
done | |
echo -n "Source files found: ${FOUND_FILES}" | |
if (( FOUND_FILES == 0 )) | |
then | |
echo ", nothing to do here..." | |
exit 1 | |
else | |
echo | |
fi | |
} | |
function revert_all_files(){ | |
#set -x | |
local COUNT | |
COUNT=0 | |
echo | |
echo "Removing files' customization..." | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
if [ "${ARR[$COUNT]}" = "found" ] | |
then | |
revert_file $COUNT | |
fi | |
((COUNT++)) | |
done | |
} | |
function revert_file(){ | |
echo -n " File: \"${CONFIG_FILES[$1]}\" ... " | |
if (( TOK[$COUNT] == 1 )) | |
then | |
sed "/${START_TOKEN[$1]}/,/${END_TOKEN[$1]}/d" "${CONFIG_FILES[$1]}" > "${CONFIG_FILES[$1]}".tmp | |
ans=$? | |
if (( ans == 0 )) | |
then | |
mv "${CONFIG_FILES[$1]}".tmp "${CONFIG_FILES[$1]}" | |
echo "done" | |
else | |
echo "failed" | |
echo " The original file is left intact." | |
echo " You can find the result of text removal in \"${CONFIG_FILES[$1]}.tmp\"." | |
fi | |
else | |
echo "not applicable" | |
fi | |
} | |
function insert_files(){ | |
local token | |
local ch | |
local str | |
local ans | |
local LINE | |
local NEXT_LINE | |
if (( FOUND_FILES > 0 )) | |
then | |
echo | |
echo "Inserting files..." | |
COUNT=0 | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
if [ "${ARR[$COUNT]}" = "found" ] | |
then | |
echo -n " ${FILES[$COUNT]} ... " | |
token="${INSERT_TOKEN[$COUNT]}" | |
ch=${token:0:1} | |
if [ "$ch" = "B" ] | |
then | |
# insert before | |
str="${token:1}" | |
LINE=$(grep -F -n "$str" "${CONFIG_FILES[$COUNT]}" | sed 's/:.*//') | |
NEXT_LINE="${LINE}" | |
((LINE--)) | |
echo -n "before line ${LINE} ... " | |
do_sed ${LINE} ${NEXT_LINE} ${COUNT} | |
ans=$? | |
elif [ "$ch" = "A" ] | |
then | |
# insert after | |
str="${token:1}" | |
LINE=$(grep -F -n "$str" "${CONFIG_FILES[$COUNT]}" | sed 's/:.*//') | |
NEXT_LINE="${LINE}" | |
((NEXT_LINE++)) | |
echo -n "after line ${LINE} ... " | |
do_sed ${LINE} ${NEXT_LINE} ${COUNT} | |
ans=$? | |
elif [ "$ch" = "^" ] | |
then | |
# insert before | |
echo -n "prepending ... " | |
cat "${FILES[$COUNT]}" "${CONFIG_FILES[$COUNT]}" > "${CONFIG_FILES[$COUNT]}".tmp | |
ans=$? | |
else | |
# append | |
echo -n "appending ... " | |
cat "${CONFIG_FILES[$COUNT]}" "${FILES[$COUNT]}" > "${CONFIG_FILES[$COUNT]}".tmp | |
ans=$? | |
fi | |
# check result of creation of tmp file | |
if (( ans != 0 )) | |
then | |
print_insert_error $COUNT | |
fi | |
mv "${CONFIG_FILES[$COUNT]}.tmp" "${CONFIG_FILES[$COUNT]}" 2>/dev/null | |
ans=$? | |
(( ans != 0 )) && print_insert_error $COUNT | |
echo "done" | |
else | |
echo "not found" | |
fi | |
((COUNT++)) | |
done | |
else | |
echo ", nothing to do here..." | |
exit 1 | |
fi | |
} | |
function do_sed(){ | |
sed -n "1,${1}p" "${CONFIG_FILES[$3]}" > "${CONFIG_FILES[$3]}".tmp | |
sed -n "${2}"',$p' "${CONFIG_FILES[$3]}" > "${CONFIG_FILES[$3]}".$$ | |
cat "${FILES[$3]}" >> "${CONFIG_FILES[$3]}".tmp | |
cat "${CONFIG_FILES[$3]}".$$ >> "${CONFIG_FILES[$3]}".tmp | |
rm "${CONFIG_FILES[$3]}".$$ | |
} | |
function print_insert_error(){ | |
if (( ans != 0 )) | |
then | |
echo "failed" | |
echo "Error inserting file!" | |
echo " \"${FILES[$1]}\" -> \"${CONFIG_FILES[$1]}\"" | |
rm /tmp/"${FILE[$1]}".$$ 2>/dev/null | |
exit 1 | |
fi | |
} | |
function list_config(){ | |
local COUNT | |
COUNT=0 | |
echo " | |
Config listing | |
============== | |
Config file: ${CONFIG} | |
Templates dir: ${BASEDIR} | |
==============" | |
while [ ! -z "${FILES[$COUNT]}" ] | |
do | |
echo "Template: ${FILES[$COUNT]}" | |
echo " Config file: ${CONFIG_FILES[$COUNT]}" | |
echo " Start token: ${START_TOKEN[$COUNT]}" | |
echo " End token: ${END_TOKEN[$COUNT]}" | |
echo " Insert token: ${INSERT_TOKEN[$COUNT]}" | |
echo | |
((COUNT++)) | |
done | |
} | |
function execute_commands(){ | |
local COUNT | |
if [ "${#COMMMAND[@]}" -eq 0 ]; then exit;fi | |
echo | |
echo "Executing commands..." | |
COUNT=0 | |
while [ "${#COMMMAND[@]}" -gt "${COUNT}" ] | |
do | |
echo " Command: \"${COMMMAND[$COUNT]}\"" | |
bash -c "${COMMMAND[$COUNT]}" | |
((COUNT++)) | |
done | |
} | |
############################################################################# | |
# Script starts here # | |
############################################################################# | |
CONFIG=~/.config/fix-openbox-rc-xml/config | |
BASEDIR=~/.config/fix-openbox-rc-xml/templates | |
[ -d "${BASEDIR}" ] || mkdir -p "${BASEDIR}" 2>/dev/null | |
while true; do | |
case "$1" in | |
-cc|--create-config) # create / force recreate our default config file | |
rm ~/.config/fix-openbox-rc-xml/config 2>/dev/null | |
create_config | |
exit | |
;; | |
-ct|--create-templates) # create empty template files based on current config | |
CREATE_TEMPLATES=1 | |
shift | |
;; | |
-ls|--list) # list existing config | |
LIST_CONFIG=1 | |
shift | |
;; | |
-oc|--open-config-dir) # open config dir (using xdg-open) | |
xdg-open ~/.config/fix-openbox-rc-xml | |
exit | |
;; | |
-ot|--open-templates-dir) # open templates dir (using xdg-open) | |
xdg-open "${BASEDIR}" | |
exit | |
;; | |
-r|--revert) # revert config files to default (remove customization) | |
JUST_REVERT=1 | |
shift | |
;; | |
-c|--config) # use this config file instead of the default | |
shift | |
if [ -s "$1" ] | |
then | |
CONFIG="$1" | |
BASEDIR=$(dirname "$1")/templates | |
if [ ! -d "${BASEDIR}" ] | |
then | |
echo "Config error" | |
echo " The dir \"${BASEDIR}\" does not exist..." | |
exit 1 | |
fi | |
else | |
echo "Config error" | |
echo " The config file \"$1\" does not exist..." | |
exit 1 | |
fi | |
shift | |
;; | |
-lh|--long-help) # print long help and exit | |
long_help | |
exit | |
;; | |
-h|--help) # print this help screen and exit | |
help | |
exit | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
read_config | |
echo -n "Navigating to \"${BASEDIR}\" ... " | |
cd "${BASEDIR}" 2>/dev/null && echo "done" || { | |
echo failed | |
exit 1 | |
} | |
declare -a ARR | |
declare -a ST_TOK_FOUND | |
declare -a END_TOK_FOUND | |
declare -a TOK | |
FOUND_FILES=0 | |
get_source_files | |
[ -z "${LIST_CONFIG}" ] || { | |
list_config | |
exit | |
} | |
[ -z "${CREATE_TEMPLATES}" ] || { | |
create_empty_templates | |
exit | |
} | |
validate_all_target | |
revert_all_files | |
[ -z "${JUST_REVERT}" ] && { | |
insert_files && execute_commands | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment