-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.bash
99 lines (88 loc) · 2.97 KB
/
functions.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
98
99
machine_specific_dir="symlinked_files-machine_specific"
function clone_machine_specific_dotfiles() {
echo "Clone machine specific dotfile repo? Y/n"
read -r use_extra_dotfiles
if [ -z "$use_extra_dotfiles" ] || [ "$use_extra_dotfiles" = "y" ]; then
echo "Enter URL of machine specific dotfile repo:"
read -r extra_dotfiles_repo_url
git clone --shallow-submodules "$extra_dotfiles_repo_url" "$machine_specific_dir"
fi
}
function update_machine_specific_dotfiles() {
git -C "$machine_specific_dir" pull --recurse-submodules
}
function generate_untracked_gitconfig() {
extra_file="$machine_specific_dir/gitconfig_extra"
untracked_file="$HOME/.gitconfig_untracked"
if [ -f "$extra_file" ]; then
echo "Not generating untracked gitconfig as $extra_file has been cloned."
elif [ ! -f "$untracked_file" ]; then
echo "Enter full name for Git global config:"
read -r git_user_name
echo "Enter email address for Git global config:"
read -r git_user_email
echo -e -n \
"[user]\n\
\tname = $git_user_name\n\
\temail = $git_user_email\n\
#\tsigningkey = \n\
#[commit]\n\
#\tgpgSign = true\n" \
>> "$untracked_file"
else
echo -e "$untracked_file already exists:\n\
Ensure at least full name and email are set."
fi
}
function create_project_symlink() {
install_link=$HOME/.dotfiles
echo -e "\nCreating symlink for $PWD directory"
if [ -L "$install_link" ]; then
# Delete any existing symlink to avoid the risk of following it when
# updating to the current location.
rm "$install_link"
fi
ln -si "$PWD"/ "$install_link"
}
function manage_dotfile_symlinks() {
# Build list of files managed by this dotfiles repo
dir_list=(symlinked_files*/)
file_list=()
for dir in "${dir_list[@]}"; do
file_list+=("$dir"*)
done
echo "${#file_list[@]} files to symlink found"
# Create backup directory for any existing versions of dotfiles
backup_dir=$HOME/dotfiles_backup-$(date +%s)
backup_dir_created=false
backup_count=0
echo "Creating backup directory $backup_dir"
if mkdir "$backup_dir"; then
backup_dir_created=true
fi
# Install the managed files
for file in "${file_list[@]}"; do
# Check for any exitisting versions of these files
filename=.$(basename "$file")
echo -e "\nChecking for existing version of $filename"
if find -f "$HOME"/"$filename" > /dev/null 2>&1; then
echo " Found existing vesion... moving to $backup_dir"
mv "$HOME"/"$filename" "$backup_dir"
backup_count=$((backup_count+1))
else
echo " No existing version found"
fi
# Create symlink to the managed version
echo "Creating symlink for $file"
ln -s "$PWD"/"$file" "$HOME"/"$filename"
done
# Remove backup directory if there was nothing to backup
if [ $backup_count -eq 0 ] && [ $backup_dir_created == true ]; then
echo -e "\nRemoving unused backup directory $backup_dir"
rm -r "$backup_dir"
fi
}
function manage_submodules() {
git submodule init
git submodule update --depth 1
}