-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·115 lines (97 loc) · 3.48 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·115 lines (97 loc) · 3.48 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# TODO: Make folder-links and NOT link to each file
INSTALL="n"
DRY_RUN="n"
show_help() {
echo "Create soft-links from this to corresponding XDG directories. Optionally, install all the necessary programs for coding on a new Linux setup."
echo
echo "usage: $0 [option]"
echo "options:"
echo " -h, --help show this help message"
echo " -d, --dry-run Simulate dotfile linking without doing anything"
echo " -i, --install Install programs listed on package-list-os & package-list-brew files"
}
place_dotfile_at_target_location() {
source_file_location="$1"
file_target_location="$2"
TS="$3"
# To avoid over writing an existing dot-file, we rename them
# Appending the timestamp to file name
if [ -f "$file_target_location" ] || [ -L "$file_target_location" ]; then
[ "$DRY_RUN" = "y" ] && echo "mv ${file_target_location} ${file_target_location}_${TS}" && echo "Existing dotfile renamed to ${file_target_location}_${TS}"
[ "$DRY_RUN" = "n" ] && mv "$file_target_location" "${file_target_location}_${TS}" && echo "Existing setting renamed to ${file_target_location}_${TS}"
fi
target_directory=$(dirname "$file_target_location")
if [ ! -d "$target_directory" ]; then
mkdir -p "$target_directory" && echo "Directory ${target_directory} created"
fi
[ "$DRY_RUN" = "y" ] && echo "ln -sf ${source_file_location} ${target_directory}"
[ "$DRY_RUN" = "n" ] && ln -sf "$source_file_location" "$target_directory" && echo "Linked ${file_target_location}"
}
parse_input() {
while [ "${#}" -gt 0 ]; do
case $1 in
-i | --install)
INSTALL="y"
shift
;;
-d | --dry-run)
DRY_RUN="y"
shift
;;
-h | --help)
show_help
exit 0
;;
*)
show_help
exit 1
;;
esac
done
}
setup_symlinks() {
case $(uname -a) in
Linux*)
OS="linux"
;;
Darwin*)
OS="macos"
;;
*)
OS="UNSUPPORTED"
;;
esac
TS=$(date '+%d_%m_%Y-%H_%M_%S')
# Switch inside dotfile repository directory
cd -P "$(dirname "$0")" || exit
# Copy all files in "Common" dotfiles to $HOME directory ("~")
find "./common" -type f -not -path '*.DS_Store' -not -path '*.directory' | while read -r file; do
file_target_location="${HOME}${file#./common}"
source_file_location="${PWD}${file#.}"
place_dotfile_at_target_location "$source_file_location" "$file_target_location" "$TS"
done
# Copy platform-specific files to $HOME directory ("~")
find "./${OS}" -type f -not -path '*.DS_Store' -not -path '*.directory' | while read -r file; do
file_target_location="${HOME}${file#./"${OS}"}"
source_file_location="${PWD}${file#.}"
place_dotfile_at_target_location "$source_file_location" "$file_target_location" "$TS"
done
}
main() {
# Refuse to run as root (symlinks and brew/flatpak/node installs don't need it)
if [ "$(id -u)" -eq 0 ]; then
echo "This script should not be run as root. Run it as your normal user."
exit 1
fi
parse_input "$@"
install_file_location="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit
pwd -P
)/scripts"
setup_symlinks
if [ "$INSTALL" = "y" ]; then
cd "$install_file_location" || exit
[ "$DRY_RUN" = "n" ] && "$install_file_location"/install.sh
fi
}
main "$@"