forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stow-to-chezmoi.sh
executable file
·57 lines (45 loc) · 1.26 KB
/
stow-to-chezmoi.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
#!/usr/bin/env bash
set -e
BASEDIR="${1:-$HOME}"
STOWDIR="${2:-dotfiles}"
BASEDIR="$(unset CDPATH; cd "$BASEDIR" >/dev/null 2>&1; pwd)"
# if we have greadlink, use that
READLINK="$(which greadlink 2>/dev/null || which readlink)"
removelink() {
[ -L "$1" ] && (
LINK_DEST="$($READLINK -f "$1")"
rm "$1"
echo -ne "$LINK_DEST ==> $1\t"
if cp -R "$LINK_DEST" "$1"; then
echo "Done"
else
echo "FAILED"
return 1
fi
)
}
work_file="$(mktemp)"
act_file="$(mktemp)"
trap "rm -f $work_file $act_file" EXIT
find "$BASEDIR" -not -path "$BASEDIR/$STOWDIR*" -type l > "$work_file" || echo "Find skipped some files"
cat "$work_file" | while read -r f; do
target="$($READLINK -f "$f" || echo '')"
if [[ "$target" == "$BASEDIR/$STOWDIR/"* ]]; then
echo "Add $f"
echo "$f" >> "$act_file"
fi
done
read -p "Migrate the above to chezmoi? y/N" migrate
case $migrate in
[Yy]*) echo "Migrating..."
;;
*) exit 1
esac
mkdir -p $BASEDIR/.local/share
cat "$act_file" | while read -r f; do
if removelink "$f" ; then
chezmoi --source "$BASEDIR/.local/share/chezmoi" --destination "$BASEDIR" add "$f"
else
echo "Unable to move: $f"
fi
done