MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.2 KiB

#!/usr/bin/env miaou-bash
# CONSTANTS
readonly AZERTY_ZONES=(
'Europe/Paris'
'Indian/Reunion'
'Indian/Mayotte'
'America/Cayenne'
'America/Guadeloupe'
'America/Martinique'
'Pacific/Noumea'
'Pacific/Tahiti'
)
readonly REQUIRED_PKGS=(file jq vim git)
readonly BACKUP='ORIGINAL'
# FUNCTIONS
function usr_bin_miaou_bash {
local source_bin="$MIAOU_BASH_DIR/bin/miaou-bash"
local dest_bin="/usr/bin/miaou-bash"
if ! [[ -f "$dest_bin" ]]; then
cp -f "$source_bin" "$dest_bin"
echo "/usr/bin/miaou-bash created"
elif ! cmp -s "$source_bin" "$dest_bin"; then
cp -f "$source_bin" "$dest_bin"
echo "/usr/bin/miaou-bash updated"
fi
}
function install_required_packages {
. "$MIAOU_BASH_DIR"/tools/pkg_add "${REQUIRED_PKGS[@]}"
}
function rm_bashrc_skeleton {
if [[ -e /etc/skel/.bashrc ]]; then
rm /etc/skel/.bashrc
echo "skeleton .bashrc removed!"
fi
}
function link_etc {
local filename="$1" subfolder="${2:-}"
local link_name="$filename"
[[ -v LINK_NAME ]] && link_name="$LINK_NAME"
local source="$MIAOU_BASH_DIR/etc"
local destination="/etc"
[[ -n "$subfolder" ]] && source+="/${subfolder}" && destination+="/${subfolder}"
if [[ -L "$destination/$link_name" ]]; then
if [[ "$(readlink "$destination/$link_name")" != "$source/$filename" ]]; then
ln -sf "$source/$filename" "$destination/$link_name"
echo "link $source/$link_name updated"
fi
else
if [[ -f "$destination/$link_name" ]]; then
mv "$destination/$link_name" "$destination/$link_name.$BACKUP"
echo "$destination/$link_name.$BACKUP saved!"
fi
ln -s "$source/$filename" "$destination/$link_name"
echo "link $source/$link_name created"
fi
}
function arch_vimrc {
mkdir -p /etc/vim
if ! grep -q '/etc/vim/vimrc.local' /etc/vimrc; then
cat >>/etc/vimrc <<-EOF
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
EOF
echo "vimrc source /etc/vim/vimrc.local if any"
fi
}
function link_config_files {
[[ "$DISTRO_LIKE" == 'arch' ]] && arch_vimrc
link_etc vimrc.core vim
link_etc vimrc.miaou vim
link_etc vimrc.rookie vim
if in_azerty_zone 2>/dev/null; then
link_etc vimrc.azerty vim
LINK_NAME=vimrc.local link_etc vimrc.local.azerty vim
else
link_etc vimrc.local vim
fi
link_etc bash.bashrc
link_etc inputrc # last command required to detect successful installation
}
function force_reload {
bind -f /etc/inputrc 2>/dev/null # arch: dismiss harmless warning
exec bash
}
function in_azerty_zone {
[[ -L /etc/localtime ]] || return 1
local zone
zone=$(readlink /etc/localtime)
zone="${zone#/usr/share/zoneinfo/}" # remove prefix
for i in "${AZERTY_ZONES[@]}"; do [[ "$zone" == "$i" ]] && return; done
return 1
}
function fix_terminfo_ghostty {
if ! infocmp xterm-ghostty >/dev/null 2>&1; then
tic -x "$MIAOU_BASH_DIR"/assets/terminfo/ghostty 2>/dev/null
echo 'terminfo ghostty succesfully compiled'
fi
}
function ghostty_global_config {
! command -v ghostty >/dev/null && return
# ghostty exists
[[ ! -d /etc/ghostty ]] && mkdir -p /etc/ghostty
local global_config=/etc/ghostty/config
local asset_config="$MIAOU_BASH_DIR"/assets/ghostty/config
local change=false
if [[ ! -f "$global_config" ]]; then
cp "$asset_config" "$global_config"
echo 'ghostty global config created'
change=true
elif ! cmp -s "$asset_config" "$global_config"; then
cp "$asset_config" "$global_config"
echo 'ghostty global config replaced'
change=true
fi
if [[ ! -f /etc/skell/.config/ghostty/config ]]; then
# ghostty config skeleton for new users
mkdir -p /etc/skel/.config/ghostty
echo "config-file = $global_config" >/etc/skel/.config/ghostty/config
fi
# change ghostty configuration for any normal user UID>=1000
mapfile -t normal_users < <(awk -F: '$3 >= 1000 && $1 != "nobody" { print $1 }' /etc/passwd)
for user in "${normal_users[@]}"; do
local user_home
user_home=$(getent passwd "$user" | cut -d: -f6)
local user_ghostty_config="$user_home"/.config/ghostty/config.ghostty
if [[ ! -f "$user_ghostty_config" ]]; then
sudo -u "$user" -- mkdir -p "$user_home"/.config/ghostty
echo -e "# global config\nconfig-file = /etc/ghostty/config\n" |
sudo -u "$user" -- tee "$user_ghostty_config" >/dev/null
echo "new predefined ghostty config file for user: $user"
elif ! grep -q 'config-file = /etc/ghostty/config' "$user_ghostty_config"; then
# prepend on top
echo -e "# global config\nconfig-file = /etc/ghostty/config\n" |
sudo -u "$user" -- tee "$user_ghostty_config".tmp >/dev/null
cat "$user_ghostty_config" |
sudo -u "$user" -- tee -a "$user_ghostty_config".tmp >/dev/null
rm "$user_ghostty_config"
sudo -u "$user" mv "$user_ghostty_config".tmp "$user_ghostty_config"
echo "modify pre-existant ghostty config file for user: $user"
fi
done
}
# MAIN
source "$MIAOU_BASH_DIR/lib/functions.bash"
assert_root
usr_bin_miaou_bash
DISTRO_LIKE=$(fetch_distro_like)
install_required_packages
rm_bashrc_skeleton
fix_terminfo_ghostty
ghostty_global_config
link_config_files
force_reload