-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·86 lines (72 loc) · 2.32 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
set -eu
set -o pipefail
if [ -f .env ] ; then
# ref: https://qiita.com/reflet/items/2caf9dbf0e3f775276ec
export $(cat .env | grep -v '^#' | xargs)
else
touch .env
fi
LAST_DEST_DIR="${LAST_DEST_DIR:-$HOME}"
DEST_DIR="${1:-${LAST_DEST_DIR}}"
mkdir -p "$DEST_DIR"
DEST_DIR=`readlink -f "$DEST_DIR"`
cd `dirname "$0"`
SRC_DIR="$(pwd)"
DOTFILES_STATUS="${DOTFILES_STATUS:-not_installed}"
[ "$DEST_DIR" != "$LAST_DEST_DIR" ] && export DOTFILES_STATUS="not_installed"
# this should be synced with `home-manager/${each-platform}/home.nix`
# for each line: $SRC_DIR/$line[0] -> $DEST_DIR/$line[1]
INSTALL_MAPPINGS="
zshrc .zshrc
wezterm.lua .wezterm.lua
tmux.conf .tmux.conf
config/starship.toml .config/starship.toml
config/bat/config.conf .config/bat/config
config/nvim/init.vim .config/nvim/init.vim
config/mise/config.toml .config/mise/config.toml
config/sheldon/plugins.toml .config/sheldon/plugins.toml
config/git/gitmessage.txt .config/git/gitmessage.txt
rye/config.toml .rye/config.toml
"
case "$DOTFILES_STATUS" in
"not_installed" )
echo "this dotfiles repository is not installed, install dotfiles from \`$SRC_DIR\` into \`$DEST_DIR\`"
;;
"installed" )
echo "this dotfiles repository is already installed, update symlinks from \`$SRC_DIR\` into \`$DEST_DIR\`"
;;
* )
echo "ERROR! unexpected environment variable DOTFILES_STATUS='$DOTFILES_STATUS', please check the content of .env"
exit 1
;;
esac
function check_installed() {
if [ -L "$1" ] ; then
echo "\`$1\` is a simbolic link, unlink this"
unlink "$1"
elif [ -f "$1" ] ; then
echo "\`$1\` is a file, move to \`$1.old\` (if \`$1.old\` exists, remove it)"
[ -e "$1.old" ] && rm "$1.old"
mv "$1" "$1.old"
fi
}
echo "$INSTALL_MAPPINGS" | while read MAP
do
[ ! -n "$MAP" ] && continue
ARGS=($MAP)
SRC="$SRC_DIR/${ARGS[0]}"
DEST="$DEST_DIR/${ARGS[1]}"
DEST_D=`dirname "$DEST"`
echo "mkdir -p '$DEST_D'"
mkdir -p "$DEST_D"
check_installed "$DEST"
echo "create a symbolic link to \`$SRC\` at \`$DEST\`"
ln -s "$SRC" "$DEST"
done
echo "update git commit template"
git config --global commit.template "$DEST_DIR/.config/git/gitmessage.txt"
echo "update $SRC_DIR/.env"
echo "DOTFILES_STATUS=installed" > .env
echo "LAST_DEST_DIR='$DEST_DIR'" >> .env
echo "done."