-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdots
executable file
·68 lines (56 loc) · 2.31 KB
/
dots
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
#!/usr/bin/env bash
shopt -s nullglob
declare -a ignored=("." ".." "README.md" ".git" "dots")
declare -al commands=("sync" "install" "link" "backup" "clean")
declare DOTS_DIR
if [ -z "$DOTS_DIR" ]; then
if [ -e "$(pwd)/dots" ]; then
DOTS_DIR="$(pwd)"
else
echo "Install 'dots' using $(dirname "$0")/install script"
exit
fi
fi
declare -a files=($DOTS_DIR/.*)
function __copy_dotfile_to_home { "/usr/bin/rsync" "-a" "$DOTS_DIR/$(basename "$1")" "$HOME"; }
function __symlink_dotfile_to_home { "/bin/ln" "-s" "$DOTS_DIR/$(basename "$1")" "$HOME/$(basename "$1")"; }
function __create_backup_dotfile { mv "$HOME/$(basename "$1")" "$HOME/$(basename "$1").bak"; }
function __remove_backup_dotfile { "/bin/rm" "-r" "$HOME/$(basename "$1").bak"; }
function __sync_existing_dotfile {
if [ -L "$HOME/$(basename "$1")" ]; then
echo "$1 is the symlink… skipping"
else
"/usr/bin/rsync" "-a" "$HOME/$(basename "$1")" "$DOTS_DIR/";
fi
}
function __loop {
for file in "${files[@]}"; do
if ! [[ ${ignored[*]} =~ $(basename "$file") ]]; then
$1 "$file"
fi
done
}
function __exec_command { echo "$2"; __loop "$1"; echo "$3"; }
function __install { __exec_command __copy_dotfile_to_home "copying dotfiles…" "dotfiles copied!"; }
function __sync { __exec_command __sync_existing_dotfile "syncing existing commands…" "dotfiles synced!"; }
function __link { __exec_command __symlink_dotfile_to_home "linking dotfiles…" "dotfiles linked!"; }
function __backup { __exec_command __create_backup_dotfile "creating backups…" "dotfiles backed up as .bak files."; }
function __clean { __exec_command __remove_backup_dotfile "cleaning up backups…" "backups cleaned up!"; }
if [ "$1" == "install" ]; then __install; fi
if [ "$1" == "sync" ]; then __sync ; fi
if [ "$1" == "link" ]; then __link; fi
if [ "$1" == "backup" ]; then __backup; fi
if [ "$1" == "clean" ]; then __clean; fi
if ! [[ ${commands[*]} =~ $1 ]]; then
exec echo "
usage: dots [option]
available options:
sync copy dotfiles from $HOME into this repository
install copy dotfiles from this repository into $HOME
link symlink dotfiles from this repository into $HOME
backup create backup files for all dotfiles in $HOME
clean remove backup files in $HOME
dots are located in $DOTS_DIR
"
fi
unset ignored files commands