Skip to content

Commit

Permalink
Refactor the code + remove bashisms
Browse files Browse the repository at this point in the history
Co-authored-by: nnyyxxxx <[email protected]>
  • Loading branch information
adamperkowski and nnyyxxxx committed Sep 13, 2024
1 parent c4b5966 commit 8c7c2f1
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions tabs/applications-setup/shell-aliases.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
#!/bin/sh
#!/bin/sh -e

. ../common-script.sh

shells=(bash zsh fish tcsh ksh nu) # Supported shells
configs=(.bashrc .zshrc config.fish .tcshrc .kshrc config.nu) # Shell config filenames
aliases=("alias linutil=\"curl -fsSL https://christitus.com/linux | sh\"" # Alias structures for different shell configs
"alias linutil=\"curl -fsSL https://christitus.com/linux | sh\""
"alias linutil \"curl -fsSL https://christitus.com/linux | sh\""
"alias linutil \"curl -fsSL https://christitus.com/linux | sh\""
"alias linutil='curl -fsSL https://christitus.com/linux | sh'"
"alias linutil = curl -fsSL https://christitus.com/linux | sh")

for ((i=0; i<${#shells[@]}; i++)); do
if command_exists ${shells[$i]}; then
echo "Found ${shells[$i]}. Adding the alias..."
config_file="$HOME/${configs[$i]}"
if [ ! -f "$config_file" ]; then
if [ "${shells[$i]}" == "fish" ] || [ "${shells[$i]}" == "nu" ]; then # Change config dirs for specific shells
config_dir="$XDG_CONFIG_HOME/${shells[$i]}"
mkdir -p "$config_dir"
config_file="$config_dir/${configs[$i]}"
elif [ "${shells[$i]}" == "bash" ] && [ -f "/etc/skel/.bashrc" ]; then # Default distro-specific config for bash
cp "/etc/skel/.bashrc" "$config_file"
else
touch "$config_file"
fi
check_shells() {
config_home="$HOME/.config"

for shell in bash zsh fish tcsh ksh nu; do # Supported shells
if command_exists "$shell"; then
setup_alias "$shell"
fi
if ! grep -Fxq "${aliases[$i]}" "$config_file"; then # Check if alias already exists
echo "${aliases[$i]}" >> "$config_file"
echo "Alias added."
done
}

setup_alias() {
shell="$1"
config_file=""
alias_line=""

case "$shell" in # Different config files & syntax for shells
bash|zsh|ksh)
config_file="$HOME/.${shell}rc"
alias_line="alias linutil='curl -fsSL https://christitus.com/linux | sh'"
;;
fish)
config_file="$config_home/fish/config.fish"
alias_line="alias linutil 'curl -fsSL https://christitus.com/linux | sh'"
;;
tcsh)
config_file="$HOME/.tcshrc"
alias_line="alias linutil 'curl -fsSL https://christitus.com/linux | sh'"
;;
nu)
config_file="$config_home/nushell/config.nu"
alias_line="alias linutil = curl -fsSL https://christitus.com/linux | sh"
;;
esac

if [ ! -f "$config_file" ]; then
if [ "$shell" = bash ] && [ -f "/etc/skel/.bashrc" ]; then # Default distro-specific config for bash
cat "/etc/skel/.bashrc" > "$config_file"
echo "Copied default config to $config_file."
else
echo "Alias already exists."
echo "Creating $config_file ."
touch "$config_file"
fi
echo
fi
done

if grep "^$alias_line$" "$config_file" > /dev/null; then
echo "Alias already exists in $config_file."
else
echo "$alias_line" >> "$config_file"
echo "Alias added to $config_file."
fi
}

check_shells

0 comments on commit 8c7c2f1

Please sign in to comment.