-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·183 lines (145 loc) · 5.26 KB
/
setup.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env bash
# This script will setup the dotfiles and executables
# contained in this repository. It will symlink all
# dotfiles and all files inside the bin-directory
# to the users ~ -dir.
# It will overwrite any existing file, but attempt to make
# a backup of the original file if they exist. Symlinks
# are not backed up.
# It keeps 2 iterations of backups, but NOTE that it is
# a simple script with very little logic in it. This means
# that if the script is run twice, with no change to the
# original files, then both backups will be equal.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# NOTE1: This script only deals with the actual dotfiles
# and executables. Directories, such as etc (vim) needs to
# be handled otherwise, manually for example!
#
# NOTE2: On most linux system symlink permissions
# don't matter. The actual permissions will be
# the ones that the target file has. So make sure
# the dotfiles in THIS directory has appropriate
# permissions and flags set.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# USAGE:
# Simply run
# ./setup.sh
# from inside the cloned repository.
#
# Copyright (C) 2020,2021 Daniel Korpela.
# Permission to copy and modify is hereby granted,
# free of charge and without warranty of any kind,
# to any person obtaining a copy of this script.
BACKUPDIR="${HOME}/.dotfiles_backup"
function check_create_dir {
# Creates the directory passed as the first argument if it's
# not already present in the file system. Exits with error code 1
# if the directory could not be created.
if [ ! -d "$1" ]; then
mkdir $1
if [ "$?" -eq 0 ]; then
echo "Created \`$1\` $2"
echo ""
fi
fi
if [ ! -d "$1" ]; then
echo "Could not create \`$1 $2\`"
echo "Exiting with no changes!"
exit 1
fi
}
function backup_file {
# Backup the file passed as argument if it is a file
# and not a symlink. Backup is defined as moving the
# file to the backup-directory (deleting the orig).
if [ "$#" -ne 2 ]; then
echo "function backup_file called with illegal num of parameters"
exit 5
fi
local f=$1
local backupdir=$2
if [ -f "${HOME}/${f}" ] && [ ! -h "${HOME}/${f}" ]; then
echo "Backing up ${HOME}/${f}..."
# Backup the backup..
[ -f "${backupdir}/${f}" ] && mv -v "${backupdir}/${f}" "${backupdir}/${f}.old"
# Backup the original file
mv -iv "${HOME}/${f}" "${backupdir}/${f}"
if [ "$?" -eq 0 ] && [ -f "${backupdir}/${f}" ]; then
echo "Backup created as (unless you got a prompt and selected n): \`${backupdir}/${f}\`"
else
echo ""
echo "!! Something went wrong when trying to backup: \`${HOME}/${f}\` !!"
echo "!! Exiting. You should investigate this issue! !!"
exit 2
fi
fi
}
function symlink_file {
# Creates a symlink from the file passed as argument to the
# home directory. Overwrites the existing file.
if [ "$#" -ne 1 ]; then
echo "function symlink_file called with illegal num of parameters"
exit 5
fi
local f=$1
ln -sfnv "${PWD}/${f}" "${HOME}/${f}"
if [ "$?" -eq 0 ] && [ ! -f "${HOME}/${f}" ]; then
echo ""
echo "!!! Something went wrong when trying !!!"
echo "!!! to create symlink: !!!"
echo "!!! ${PWD}/${f} -> ${HOME}/${f} !!!"
echo "!!! Exiting. You should investigate this issue! !!!"
exit 3
fi
}
function setup_dotfiles {
# First attempts to backup all the dotfiles in the users home directory
# with the same name as the dotfiles available in the current directory
# this script is run from. After that attempts to symlink all the
# dotfiles from the current dir to the users pwd.
check_create_dir $BACKUPDIR "for backups!"
echo "Backing up and symlinking dotfiles.."
echo "------------------------------------"
echo ""
local f
for f in .*; do
if [ -f "$f" ]; then
backup_file $f $BACKUPDIR
# At this point the file does not exist in ~. => create symlink
symlink_file $f
echo ""
fi
done
echo "Done!"
echo "-----"
echo ""
}
function setup_binfiles {
# First attempts to backup all the executables in the users ~/bin directory
# with the same name as the executables available in the $PWD/bin directory
# this script is run from. After that attempts to symlink all the
# dotfiles from the current executables dir to the users ~/bin -directory.
# This function assumes that all files living inside the bin-directory
# are executables, it is not tested for!
check_create_dir "${HOME}/bin" "for executables!"
check_create_dir "${BACKUPDIR}/bin" "for executables backups!"
echo "Backing up and symlinking executables.."
echo "---------------------------------------"
echo ""
local f
for f in bin/*; do
if [ -f "$f" ]; then
backup_file $f $BACKUPDIR
# At this point the file does not exist in ~/bin. => create symlink
symlink_file $f
echo ""
fi
done
echo "Done!"
echo "-----"
echo ""
}
setup_dotfiles
setup_binfiles
echo "All done! Exiting!"