Handle prepending for custom env conf, fix bugs in external conf

master
Quentin Duchemin 2023-10-28 23:13:37 +02:00
parent ea22f2a027
commit 5f304686f7
1 changed files with 28 additions and 14 deletions

View File

@ -19,8 +19,10 @@ POLYBAR_CONFIG="polybar"
ARCH_PACKAGES="bootstrap"
MERGE_DIRS="${I3_CONFIG} ${POLYBAR_CONFIG} ${ARCH_PACKAGES}"
changed_files="$(git --git-dir=$HOME/.cfg/ --work-tree=$HOME diff-tree -r --name-status --no-commit-id HEAD^1 HEAD)"
# Check if a file has been modified and deleted since last commit, if so perform an action
# $1 : relative path starting from the folder with "dest"
# $1 : absolute path of the file
# $2 : command to launch if file has been created of modifed
# $3 : command to launch if file has been deleted
function check_copy() {
@ -29,15 +31,15 @@ function check_copy() {
echo -e "* Running \"$2\"" && eval "$2"
return
fi
echo "$changed_files" | grep --quiet "${1#$HOME/}"
if [ $? == "0" ]; then
echo "$changed_files" | cut -f 1 | grep --quiet "D"
if [ $? == "0" ]; then
changed_file=`grep "${1#$HOME/}" <<< "${changed_files}"`
if [ ! -z "${changed_file}" ]; then
if echo "$changed_file" | grep -q -E "^D"; then
echo "* $1 has been removed"
echo -e "\t* Running \"$3\"" && eval "$3"
else
echo "* Changes detected in $1"
echo -e "* Running \"$2\"" && eval "$2"
fi
if echo "$changed_file" | grep -q -E "^(A|M)"; then
echo "* Changes detected in $(basename $1)"
echo -e "\t* Running \"$2\"" && eval "$2"
fi
else
echo "$1 not modified, no need to copy"
@ -49,6 +51,7 @@ function check_copy() {
# a common base for multiple computer, and per-computer stuff.
# $1 : path of the folder
# $2 : optional current environment
# $3 : if defined, prepend $2 instead of appending
function merge_config() {
echo -e "\n=== Generate ${m} config file ==="
rm -f ${CONFIG}/${m}/${FINAL_CONFIG}
@ -58,8 +61,17 @@ function merge_config() {
# If there is an override folder, merge given env
if [ ! -z ${2} ] && [ -d ${CONFIG}/${m}/${MERGE_CONFIG_FOLDER} ]; then
for f in `find ${CONFIG}/${m}/config.d -type f -path "*${2}"`; do
echo "Found ${f}, add to regular config file"
cat "${f}" >> ${CONFIG}/${m}/${FINAL_CONFIG}
final_path=${CONFIG}/${m}/${FINAL_CONFIG}
if [ ! -z $3 ]; then
echo "Found ${f}, append to regular config file"
cat "${f}" >> ${final_path}
else
echo "Found ${f}, prepend to regular config file"
cat ${f} > /tmp/config
echo -e '\n' >> /tmp/config
cat ${final_path} >> /tmp/config
mv /tmp/config ${final_path}
fi
done
fi
}
@ -75,10 +87,12 @@ CONFIG=${XDG_CONFIG_HOME:-$HOME/.config}
ENV=${1}
changed_files="$(git --git-dir=$HOME/.cfg/ --work-tree=$HOME diff-tree -r --name-status --no-commit-id HEAD^1 HEAD)"
for m in ${MERGE_DIRS}; do
merge_config $m ${ENV}
if [ -f ${m}/above ]; then
merge_config $m ${ENV} true
else
merge_config $m ${ENV}
fi
done
if [ ! -z "$INIT" ]; then
@ -147,7 +161,7 @@ for f in `find ${CONFIG} -type f -name ${DEST_FILENAME}`; do
real_dest=${DEST}/${subfolder}
sudo mkdir -p ${real_dest}
absolute_source=`realpath "${DIR}/${config_file}"`
check_copy "${filename}" "sudo cp ${absolute_source} ${real_dest}" "sudo rm ${real_dest}/${filename}"
check_copy "${absolute_source}" "sudo cp ${absolute_source} ${real_dest}" "sudo rm ${real_dest}/${filename}"
done
done