-
Notifications
You must be signed in to change notification settings - Fork 0
/
homefiles_setup
executable file
·104 lines (87 loc) · 2.43 KB
/
homefiles_setup
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
#!/bin/bash
set -e
REPO_URL="$1"
if [ -z "${REPO_URL}" ]; then
echo 'Usage:'
echo ' homefiles_setup REPO_URL'
echo
echo 'Example:'
echo ' homefiles_setup https://github.com/jcouball/homefiles.git'
echo
exit 1
fi
if [ -d "${HOME}/.homefiles" ]; then
echo ".homefiles directory already exists"
exit 1
fi
function init_script {
cat <<'SCRIPT'
# HOMEFILES START
function homefiles {
git --git-dir="${HOME}/.homefiles/" --work-tree="${HOME}" "$@"
}
function homefiles_init {
git init --bare "${HOME}/.homefiles"
}
function homefiles_add_files {
for file in "$@"; do
HOME_FILE="${HOME}/${file}"
[ -f "${HOME_FILE}" ] && homefiles add "${HOME_FILE}"
done
echo "Done adding"
}
# HOMEFILES END
SCRIPT
}
function update_init_files {
local START="# HOMEFILES START"
local END="# HOMEFILES END"
for file in "$@"; do
HOME_FILE="${HOME}/${file}"
if [ -f "${HOME_FILE}" ]; then
if ! grep -q "^$START" "${HOME_FILE}"; then
init_script >> "${HOME_FILE}"
fi
fi
done
}
function remove_init_script {
local START="# HOMEFILES START"
local END="# HOMEFILES END"
for file in "$@"; do
HOME_FILE="${HOME}/${file}"
if grep -q "^$END" "${HOME_FILE}"; then sed -i .bak "/^$START/,/^$END/d" "${HOME_FILE}"; fi
done
}
function source_init_script {
local TMPFILE=$(mktemp)
init_script > "${TMPFILE}"
source "${TMPFILE}"
rm "${TMPFILE}"
}
function create_gitignore {
local GIT_IGNORE_FILE="${HOME}/.gitignore"
if [ ! -f "${GIT_IGNORE_FILE}" ]; then echo ".homefiles" >> "${GIT_IGNORE_FILE}"; fi
if ! grep -q '^.homefiles$' "${GIT_IGNORE_FILE}"; then echo ".homefiles" >> "${GIT_IGNORE_FILE}"; fi
}
function create_readme {
local README_FILE="${HOME}/README.md"
if [ -f "${README_FILE}" ]; then return; fi
cat <<'README' > ${README_FILE}
# My Home Files
Use the `homefiles` command to manage files from this home directory in Git.
See [homefiles_setup](https://github.com/jcouball/homefiles_setup) for details.
README
}
pushd ~
update_init_files .bashrc .zshrc
source_init_script
homefiles_init
homefiles config --local status.showUntrackedFiles no
create_gitignore
create_readme
homefiles_add_files .gitignore README.md .bashrc .bash_profile .bash_login .profile .bash_logout .zshenv .zprofile .zshrc .zlogin .zlogout
homefiles commit -m 'Initial commit'
homefiles remote add origin "${REPO_URL}"
homefiles push --set-upstream origin `homefiles branch --no-color --show-current`
popd