Browse Source

Make dotfiles.sh POSIX compliant

master
Riyyi 6 years ago
parent
commit
2f5c9274de
  1. 95
      dotfiles.sh

95
dotfiles.sh

@ -1,19 +1,19 @@
#!/bin/bash #!/bin/sh
if [ "$(dirname $0)" != "." ]; then if [ "$(dirname "$0")" != "." ]; then
echo "Please run this script from the directory it resides." echo "Please run this script from the directory it resides."
exit exit
fi fi
PACKAGE_FILE="./packages" PACKAGE_FILE="./packages"
FILES=$(find . \( -path ./.git -o \ FILES="$(find . \( -path ./.git -o \
-path ./dotfiles.sh -o \ -path ./dotfiles.sh -o \
-path $PACKAGE_FILE -o \ -path $PACKAGE_FILE -o \
-path ./README.md -o \ -path ./README.md -o \
-path ./screenshot.png \) -prune -o -type f -print) -path ./screenshot.png \) -prune -o -type f -print)"
if [ "$1" == "help" ] || [ "$1" == "" ]; then help() {
B=$(tput bold) B=$(tput bold)
N=$(tput sgr0) N=$(tput sgr0)
@ -46,73 +46,96 @@ ${B}COMMANDS${N}
Store the list of all the installed packages on the system. Store the list of all the installed packages on the system.
${B}packages install${N} ${B}packages install${N}
Install all the core packages of the list. Install all the core packages of the stored list.
EOF EOF
}
elif [ "$1" == "list" ]; then list() {
for f in $FILES; do for f in $FILES; do
echo $f echo "${f#??}"
done done
}
elif [ "$1" == "get" ] && [ "$2" != "" ]; then get() {
FILE=$(readlink -f $2) if [ "$1" = "" ]; then
FILE_CUT_HOME="$(echo $FILE | sed -nr 's/^\/home\/'$USER'\/(.*)$/\1/p')" echo "Missing argument <filepath>"
return
fi
FILE=$(readlink -f "$1")
FILE_CUT_HOME="$(echo "$FILE" | sed -nr 's/^\/home\/'"$USER"'\/(.*)$/\1/p')"
# /home/<user>/ # /home/<user>/
if [ -n "$FILE_CUT_HOME" ]; then if [ -n "$FILE_CUT_HOME" ]; then
mkdir -p $(pwd)/$(dirname $FILE_CUT_HOME) mkdir -p "$(pwd)/$(dirname "$FILE_CUT_HOME")"
cp $FILE $(pwd)/$FILE_CUT_HOME cp "$FILE" "$(pwd)/$FILE_CUT_HOME"
# / # /
else else
mkdir -p $(pwd)/$(dirname $FILE) mkdir -p "$(pwd)/$(dirname "$FILE")"
cp $FILE $(pwd)/$FILE cp "$FILE" "$(pwd)/$FILE"
fi
}
pullpush() {
if [ "$1" = "" ]; then
return
fi fi
elif [ "$1" == "pull" ] || [ "$1" == "push" ]; then
for f in $FILES; do for f in $FILES; do
# Remove the first character (.) from the string # Remove the first character (.) from the string
f=${f:1} f=${f#?}
# Resolved symbolic link # Resolved symbolic link
fr=$(readlink -f $f) fr=$(readlink -f "$f")
# The filepath starts with '/boot/', '/etc/', '/usr/share/' # The filepath starts with '/boot/', '/etc/', '/usr/share/'
if [ -n "$(echo $fr | sed -nr 's/^(\/(boot|etc|usr\/share)\/).*$/\1/p')" ]; then if [ -n "$(echo "$fr" | sed -nr 's/^(\/(boot|etc|usr\/share)\/).*$/\1/p')" ]; then
if [ "$1" == "pull" ]; then if [ "$1" = "pull" ]; then
sudo cp $fr $(pwd)/$fr sudo cp "$fr" "$(pwd)/$fr"
else elif [ "$1" = "push" ]; then
sudo cp $(pwd)/$fr $fr sudo cp "$(pwd)/$fr" "$fr"
fi fi
else else
if [ "$1" == "pull" ]; then if [ "$1" = "pull" ]; then
# cp /home/<user>/<file> /home/<user>/[<some dir>/]dotfiles/<file> # cp /home/<user>/<file> /home/<user>/[<some dir>/]dotfiles/<file>
cp $HOME$f $(pwd)/$f cp "$HOME$f" "$(pwd)/$f"
else elif [ "$1" = "push" ]; then
cp $(pwd)/$f $HOME$f cp "$(pwd)/$f" "$HOME$f"
fi fi
fi fi
done done
}
pull() {
pullpush "pull"
}
push() {
pullpush "push"
}
elif [ "$1" == "packages" ]; then packages() {
PACKAGE_LIST=$(comm -23 <(pacman -Qeq | sort) <(pacman -Qgq base base-devel | sort)) PACKAGE_LIST="$(pacman -Qqe | sort | grep -vx "$(pacman -Qqg base base-devel | sort)")"
if [ "$2" == "list" ] || [ "$2" == "" ]; then if [ "$1" = "list" ] || [ "$1" = "" ]; then
echo "$PACKAGE_LIST" echo "$PACKAGE_LIST"
elif [ "$2" == "store" ]; then elif [ "$1" = "store" ]; then
if [ ! -s $PACKAGE_FILE ]; then if [ ! -s $PACKAGE_FILE ]; then
touch "$PACKAGE_FILE" touch "$PACKAGE_FILE"
else else
truncate -s 0 "$PACKAGE_FILE" truncate -s 0 "$PACKAGE_FILE"
fi fi
echo "$PACKAGE_LIST" > "$PACKAGE_FILE" echo "$PACKAGE_LIST" > "$PACKAGE_FILE"
elif [ "$2" == "install" ]; then elif [ "$1" = "install" ]; then
# Install core packages # Install core packages
sudo pacman -S --needed $(comm -12 <(pacman -Slq | sort) <(sort $PACKAGE_FILE)) sudo pacman -S --needed --noconfirm "$(cat "$PACKAGE_FILE")"
# For AUR packages, run: <helper> -S - < packages # For AUR packages, run: <helper> -S - < packages
fi fi
else }
echo "./dotfiles.sh: '$1' is not a dotfiles command. \
See './dotfiles.sh help'."
if type "$1" 2> /dev/null | grep -q "function"; then
"$@"
else
help
fi fi
# @Todo: # @Todo:

Loading…
Cancel
Save