-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_install.sh
executable file
·138 lines (115 loc) · 3.27 KB
/
dot_install.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
set -e
script_dir="$(dirname "$(readlink -f "$0")")"
source "$script_dir/_shared_functions.sh"
dry_run='false'
user_files='false'
file_names=''
file_array=()
default_file_array=(
'ackrc'
'bash_functions'
'bashrc'
'ctags'
'ctags.d/scala.ctags'
'ctags.d/tsx.ctags'
'gitignore_global'
'tmux.conf'
'vimrc'
)
print_options () {
printf "Options:\n"
printf " -d Dry run\n"
printf " -f Space separated list of files to link\n"
printf " -h This help screen\n"
}
while getopts 'dhf:' opt
do
case "${opt}" in
d) dry_run='true' ;;
h) print_options ; exit 0 ;;
f) user_files='true' ; file_names+="${OPTARG}" ;;
*) print_options ; exit 1 ;;
esac
done
shift $(( OPTIND - 1 ));
additional_files=("$@")
file_array+=("$file_names")
file_array+=("${additional_files[@]}")
file_names+=" ${additional_files[*]}"
[ "$dry_run" = 'true' ] && printf "\n\n\n!!!DRY RUN!!!\n!!!DRY RUN!!!\n!!!DRY RUN!!!\n\n\n"
printf "\nscript directory: '%s'\n" "$script_dir"
targ_dir=$HOME
printf "\ntarget directory: '%s'\n" "$targ_dir"
link_file_with_backup () {
filename=$1
path_filename="$targ_dir/.$filename"
backup="$targ_dir/.$filename.BAK"
prn_note "Processing '$filename'... "
cp_cmd=""
if [ -h "$path_filename" ]
then
cp_cmd="cp $(readlink "$path_filename") $backup"
elif [ -f "$path_filename" ]
then
cp_cmd="cp $path_filename $backup"
fi
if [[ "$path_filename" == *\/* ]]
then
mkdir -p ${path_filename%/*}
fi
printf "Running '%s'\n" "$cp_cmd"
[ "$dry_run" = 'false' ] && eval "$cp_cmd"
printf "Done.\n"
ln_cmd="ln -sf $script_dir/$filename $path_filename"
printf "Running '%s'\n" "$ln_cmd"
[ "$dry_run" = 'false' ] && eval "$ln_cmd"
printf "Done.\n"
echo
}
check_files_exist () {
for file in "${file_array[@]}"
do
file_with_path="$script_dir/${file#.}"
printf "\nChecking '%s'\n" "$file_with_path"
if [ -f "$file_with_path" ]; then
printf "File exists.\n"
else
missing_file_message="ERROR: File '$file_with_path' does not exist. Exiting."
prn_error "$missing_file_message"
exit 1
fi
done
printf "Done.\n\n"
}
link_dot_files () {
printf "\nLinking dot files by 'ln -sf %s/filename %s/.filename'\n" "$script_dir" "$targ_dir"
printf 'Backups will be made as follows: %s/.filename -> %s/filename.BAK\n\n' "$targ_dir" "$targ_dir"
if [ "$user_files" = 'false' ]
then
printf "No files specified with '-f SPACE SEPARATED LIST OF FILENAMES'.\n"
printf "Using default list.\n\n"
file_array=("${default_file_array[@]}")
file_names="${file_array[*]}"
fi
printf "List of %d files to process: %s\n" "${#file_array[@]}" "$file_names"
resp=$(get_confirmation "Is this the correct list of files?")
if [ -n "$resp" ]
then
check_files_exist
else
exit 1
fi
for file in "${file_array[@]}"
do
clean_name="${file#.}"
link_file_with_backup "$clean_name"
done
}
resp=$(get_confirmation "Do you wish to link your dot files to your home directory?")
if [ -n "$resp" ]
then
link_dot_files
prn_success "SUCCESS: Ran 'dot_install.sh' script without errors."
fi
exit 0