Add fixes for setup script

Use `$0` instead of `pwd` command
- pwd will cause errors if the script is launched from some other
  directory.

False positive with `[[ -f "$ROFI_DIR/config.rasi" ]]`
- This may give a false successful installation message if the user's
  original 'config.rasi' is still in the directory.
- Add `set -eu` which will cause the script to exit if there are any
  errors to prevent data loss.
- Add `trap` command to print error message on script exit.

Some more fixes/changes
- Check if XDG user direcotries are set.
- Simplify script by replacing `if` statements with `||` and `case`.
This commit is contained in:
Mudskipper875 2021-10-24 09:09:00 +05:30
parent aba9eb3d1a
commit 1f12de8e12
No known key found for this signature in database
GPG Key ID: 76F38911FC47C88F

View File

@ -3,37 +3,28 @@
# Install script for rofi themes
# Dirs
DIR=`pwd`
FONT_DIR="$HOME/.local/share/fonts"
ROFI_DIR="$HOME/.config/rofi"
DIR="${0%/*}"
FONT_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/fonts"
ROFI_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/rofi"
set -eu
exit_msg="[!] Failed to install.\n"
trap 'echo -e $exit_msg' EXIT
# Install Fonts
install_fonts() {
echo -e "\n[*] Installing fonts..."
if [[ -d "$FONT_DIR" ]]; then
cp -rf $DIR/fonts/* "$FONT_DIR"
else
mkdir -p "$FONT_DIR"
cp -rf $DIR/fonts/* "$FONT_DIR"
fi
[[ -d "$FONT_DIR" ]] || mkdir -p "$FONT_DIR"
cp -r "$DIR"/fonts/* "$FONT_DIR"
}
# Install Themes
install_themes() {
if [[ -d "$ROFI_DIR" ]]; then
echo -e "[*] Creating a backup of your rofi configs..."
mv "$ROFI_DIR" "${ROFI_DIR}.old"
{ mkdir -p "$ROFI_DIR"; cp -rf $DIR/$RES/* "$ROFI_DIR"; }
else
{ mkdir -p "$ROFI_DIR"; cp -rf $DIR/$RES/* "$ROFI_DIR"; }
fi
if [[ -f "$ROFI_DIR/config.rasi" ]]; then
echo -e "[*] Successfully Installed.\n"
exit 0
else
echo -e "[!] Failed to install.\n"
exit 1
mv -f "$ROFI_DIR" "${ROFI_DIR}.old"
fi
cp -r "$DIR/$RES" "$ROFI_DIR"
}
# Main
@ -50,18 +41,16 @@ main() {
read -p "[?] Select Option : "
if [[ $REPLY == "1" ]]; then
RES='1080p'
install_fonts
install_themes
elif [[ $REPLY == "2" ]]; then
RES='720p'
install_fonts
install_themes
else
echo -e "\n[!] Invalid Option, Exiting...\n"
exit 1
fi
case $REPLY in
1) RES='1080p' ;;
2) RES='720p' ;;
*) exit_msg="\n[!] Invalid Option, Exiting...\n"; exit 1 ;;
esac
install_fonts
install_themes
exit_msg="[*] Successfully Installed.\n"
exit 0
}
main