-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_home.sh
More file actions
executable file
·94 lines (88 loc) · 3.7 KB
/
update_home.sh
File metadata and controls
executable file
·94 lines (88 loc) · 3.7 KB
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
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
## Update root dir from all the dot files in this directory. Back up existing
## versions of replaced files by tacking on a .update.
##
## TODO Ensure that this is run from the correct directory
## TODO Move If symlink exist in $HOME and it should be in $XDG_CONFIG_HOME move
## it to $XDG_CONFIG_HOME
##
## 2016-01-15 omit .svn .sync .git .DS_Store
## 2021-01-25 Copy .profile_`hostname` or .profile_$USER files
## 2025-02-02 If XDG_CONFIG_HOME is set, put select files in tha config directory
type _include.sh &>/dev/null && . _include.sh
ext='.update' ## Name used for suffixing backed up names.
#shopt -s dotglob
## Iterate over all "dot" files and directores
for i in .*; do
case "$i" in
## Skip some dot-files
. | .. | .svn | .git | .sync | .DS_Store | .profile.* | .*.bak | .*_history) continue ;;
## Copy to XDG_CONFIG_HOME (or HOME) destination
.profile_$(uname -s) | .profile_${USER:-$(id -un)} | .profile_${HOSTNAME%.local} |
.alias | .bash_completion.d )
[ -n "$XDG_CONFIG_HOME" -a ! -d "$XDG_CONFIG_HOME" ] && mkdir -p "$XDG_CONFIG_HOME"
[ -n "$XDG_CONFIG_HOME" ] && target="$XDG_CONFIG_HOME" || target="$HOME"
;;
## Skip other profiles that do not apply to this environment
.profile_*) continue ;;
## Default: .profile, .bashrc, .bash_profile, .inputrc, .vimrc, .vim, .emacs, .emacs.d, .ssh, .gnupg, .gitconfig, .gitignore, .gitattributes, .config, .local, .cache, .state, etc.
*)
target="$HOME"
;;
esac
## Check that it's a file and is up to date
if [ -s "$i" ]; then
if [ -f ${target}/"$i" ]; then
## If home version is not a hardlink to this one, back it up
if [ ! "$i" -ef ${target}/"$i" ]; then
## Even if files are not the same, only backup if their contents
## are different.
if diff -q "$i" ${target}/"$i" 2>/dev/null; then
rm ${target}/"$i"
elif [ "$(uname -s)" = Darwin ] && which osx_trash >/dev/null; then
osx_trash ${target}/"$i"
elif [ -e ${target}/"$i$ext" ]; then
echo " \"${t_red}${target}/$i$ext${t_reset}\" already exists... cannot back up."
else
echo " Backing up \"${target}/$i\" to \"${target}/$i${ext}\"" && mv ${target}/"$i" ${target}/"$i${ext}"
fi
echo " ${t_green}Linking \"$i\"${t_red}"
ln -s "$(pwd)/$i" ~
echo -n "$t_reset"
fi
# elif [ -L ${target}/"$i" ]; then
# ## It's a link, so probably nothing to update
# if [ ! "$i" -ef ${target}/"$i" ]; then
# if [ "$`uname -s`" = Darwin ] && which osx_trash >/dev/null; then
# osx_trash ${target}/"$i"
# elif [ -e ${target}/"$i$ext" ]; then
# echo " \"$t_red${target}/$i$ext$t_reset\" already exists... cannot back up."
# else
# echo " Backing up \"${target}/$i\" to \"${target}/$i$ext\"" && mv ${target}/"$i" ${target}/"$i$ext"
# fi
# echo " Linking \"$i\""
# ln -s "`pwd`/$i" ~
# fi
elif [ -e ${target}/"$i" ]; then
echo " ${t_red}${target}/$i exists, but it not a file (it should be a file)${t_reset}".
else ## Create link
echo " ${t_green}Linking \"${target}/$i\"${t_red}"
# ln "`pwd`/$i" ~ || ln -s "`pwd`/$i" ~
ln -s "$(pwd)/$i" ~
echo -n "$t_reset"
fi
else ## Assume source is directory; check that it is up to date
if [ -e ${target}/"$i" -o -L ${target}/"$i" -a ! "$(readlink ${target}/"$i")" -ef "$i" ]; then
if [ ! "$i" -ef ${target}/"$i" ]; then
[ ! -e ${target}/"$i${ext}" ] && echo " Backing up directory \"${target}/$i\" to \"${target}/$i${ext}\"" && mv ${target}/"$i" ${target}/"$i${ext}"
echo " ${t_green}Linking \"${target}/$i\" directory${t_red}"
ln -s "$(pwd)/$i" ~
echo -n "$t_reset"
fi
else
echo " ${t_green}Linking \"${target}/$i\" directory${t_red}"
ln -s "$(pwd)/$i" ~
echo -n "$t_reset"
fi
fi
done