dotfiles/.init_config.sh

138 lines
4.6 KiB
Bash
Raw Normal View History

#!/bin/bash
# Environments
LAPTOP="laptop"
DESKTOP="desk"
ENV_EXT_REGEXP=".*\.${LAPTOP}|.*\.${DESKTOP}"
# Special filename for external config path
2019-02-24 21:20:30 +01:00
DEST_FILENAME="dest"
# Special configuration filename
BASE_CONFIG="config_common"
MERGE_CONFIG_FOLDER="config.d"
FINAL_CONFIG="config"
# Special directories with per-environment configuration merge
I3_CONFIG="i3"
POLYBAR_CONFIG="polybar"
ARCH_PACKAGES="bootstrap"
MERGE_DIRS="${I3_CONFIG} ${POLYBAR_CONFIG} ${ARCH_PACKAGES}"
# Check if a file has been modified and deleted since last commit, if so perform an action
# $1 : path of file
# $2 : command to launch if file has been created of modifed
# $3 : command to launch if file has been deleted
function check_copy() {
# When initializing, always copy files
if [ ! -z ${INIT} ]; then
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
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
else
echo "$1 not modified, no need to copy"
fi
}
# Generates a `config` file in $1, by merging a required $1/{MERGE_CONFIG_FOLDER} file
# with an optional `$1/config.d/$2` file. This is useful for having
# a common base for multiple computer, and per-computer stuff.
# $1 : path of the folder
# $2 : optional current environment
function merge_config() {
echo -e "\n=== Generate ${m} config file ==="
rm -f ${CONFIG}/${m}/${FINAL_CONFIG}
# Copy common config
cp ${CONFIG}/${m}/${BASE_CONFIG} ${CONFIG}/${m}/${FINAL_CONFIG}
echo "${CONFIG}/${m}/${FINAL_CONFIG} created"
# 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}
done
fi
}
function usage() {
echo "${0}: manage package installation, environment divergence and external location for config, see README."
echo "usage: [WM_RESTART=1] [INIT=1] $0 [${ENVIRONMENTS}]"
exit 0
}
2019-02-24 21:20:30 +01:00
# Use XDG_CONFIG_HOME if defined, default otherwise
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}
2019-01-26 19:24:25 +01:00
done
if [ ! -z "$INIT" ]; then
echo -e "* Installing base Arch packages..."
yay -Syu --needed --noconfirm - < ${CONFIG}/${ARCH_PACKAGES}/${FINAL_CONFIG}
echo -e "* Changing shell to ZSH..."
chsh -s $(which zsh)
# https://man.archlinux.org/man/rofi-dmenu.5
echo -e "* Symlink dmenu → rofi..."
sudo ln -s /usr/bin/rofi /usr/bin/dmenu
echo -e "* Enabling NetworkManager..."
sudo systemctl enable --now NetworkManager
2023-02-19 00:49:30 +01:00
# So that "Open in Terminal" works in Nemo
gsettings set org.cinnamon.desktop.default-applications.terminal exec i3-sensible-terminal
# For screenshots
pip install pychee
fi
if [ ! -z ${ENV} ]; then
echo -e "\n=== Create symlink to *.${ENV} regular files ==="
# Take all specific files and create a symlink pointing to it without the extension
# This file will be used by applications
for f in `find ${CONFIG} -type f -name "*.${ENV}"`; do
echo "${f%.*}${f}"
ln -sf ${f} ${f%.*}
done
# Take care of config files at home level, outside ${CONFIG} (e.g. Xresources)
for f in `find ${HOME} -maxdepth 1 -mindepth 1 -type f -name "*.${ENV}"`; do
echo "${f%.*}${f}"
ln -sf ${f} ${f%.*}
done
fi
2019-02-24 21:20:30 +01:00
echo -e "\n=== Copy changed configuration which resides in outer directory ==="
2019-02-24 21:20:30 +01:00
# Destination files are regular files
for f in `find ${CONFIG} -type f -name ${DEST_FILENAME}`; do
DIR=`dirname "${f}"`
DEST=`cat ${f}`
# Get all files, either regular files or symlinks, which are not the destination file nor specific desktop/laptop files
# This is because if desktop/laptop files exist, they already have a symlink pointing to them at this stage
for config_file in `find ${DIR} -regextype emacs -mindepth 1 -type f -not \( -regex "*.${ENV_EXT_REGEXP}" -o -name "${DEST_FILENAME}" \)`; do
2019-02-24 21:20:30 +01:00
# Now copy to destination
sudo mkdir -p ${DEST}
check_copy "${config_file}" "sudo cp ${config_file} ${DEST}" "sudo rm ${DEST}/${config_file##*/}"
2019-02-24 21:20:30 +01:00
done
done
if [ ! -z "${WM_RESTART}" ]; then
echo -e "\n=== Restart graphical environment ==="
# Reload i3 configuration
i3-msg reload
# Restart picom due to this bug https://github.com/yshui/picom/issues/166
killall picom; picom &>/dev/null &
fi
2020-01-24 11:57:08 +01:00