-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-bash
97 lines (86 loc) · 2.65 KB
/
setup-bash
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
95
96
97
#!/bin/bash
set -e
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DOT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DOT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
# Figure out how to link files in this environment
USE_SYMLINKS=1
test_link="test_symlink"
test_target="."
if [[ $OSTYPE == "msys" || $OSTYPE == "cygwin" ]]; then
echo "Testing symlink creation"
export MSYS="$MSYS winsymlinks:nativestrict"
export CYGWIN="$CYGWIN winsymlinks:nativestrict"
set +e
ln -s $test_target $test_link > /dev/null 2>&1
ln_result=$?
set -e
if [ $ln_result -eq 0 ]; then
# Cleanup
rm "$test_link"
echo "Current user can create symbolic links."
USE_SYMLINKS=0
else
echo "Current Windows user is unable to create symbolic links."
fi
else
# Assume symlinks are available.
USE_SYMLINKS=0
fi
UNDO_SCRIPT="$DOT_DIR/.local/undo-$(date +"%Y-%m-%d_%H-%M-%S")"
TEST_ONLY=1
create_link() {
local target=$(realpath $1)
local desired_link=$2
if [ $USE_SYMLINKS -eq 0 ]; then
do="ln -T -f -s \"$target\" \"$desired_link\""
undo="unlink \"$desired_link\""
else
if [ -d "$target" ]; then
do="(if [ -d \"$desired_link\" ]; then rm \"$desired_link\"; fi) && cmd //c mklink //J \"$(cygpath -w $desired_link)\" \"$(cygpath -w $target)\""
undo="rm \"$desired_link\""
else
do="(if [ -f \"$desired_link\" ]; then rm \"$desired_link\"; fi) && cmd //c mklink //H \"$(cygpath -w $desired_link)\" \"$(cygpath -w $target)\""
undo="rm \"$desired_link\""
fi
fi
echo $do
if [ $TEST_ONLY -ne 0 ]; then
(eval $do) && (echo $undo >> $UNDO_SCRIPT)
fi
}
provide_from() {
local link=$1
local target=$2
mkdir -p "$(dirname $link)"
create_link $target $link
}
provide() {
local link=$1
local link_base=$(basename $link)
local target_base=${link_base#.}
provide_from $link "$DOT_DIR/$target_base"
}
while getopts "t" opt; do
case $opt in
t ) TEST_ONLY=0 ;;
esac
done
if [ $TEST_ONLY -ne 0 ]; then
mkdir -p $DOT_DIR/.local
touch $UNDO_SCRIPT
echo "#!/bin/bash" >> $UNDO_SCRIPT
fi
provide ~/.bashrc
provide ~/.bash_profile
provide ~/.gitconfig
provide ~/.inputrc
provide ~/.nvm
provide ~/.config.omp.json
provide ~/.emacs.el
echo "Linking winhome in WSL2."
~/.dotfiles/bin/wsl/link-winhome