summaryrefslogtreecommitdiffstats
path: root/bashrc/main.sh
blob: 01ca30943e911ba06ab917e26c62fa2acb547e1b (plain) (blame)
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
# this file is sourced by all bash shells on startup

# test for interactive shell
[[ $- != *i* ]] && return

# return if this script is executed twice
[[ -n $(declare -p _DOTFILES_DIR 2>/dev/null) ]] && return

declare -r _DOTFILES_DIR="${HOME}/.dotfiles"
declare -r _BASHRC_DIR="${_DOTFILES_DIR}/bashrc"

# start in home dir
if [[ -n "${DIR}" ]]; then
	cd "${DIR}"
else
	cd "${HOME}"
fi

# self update magic
_self_update() {
	pushd ${_DOTFILES_DIR} &>/dev/null

	# save the origin, the current master is based of
    if ! git tag | grep old-origin -q; then
	    if git branch | grep old-origin -q; then
		    git branch -D old-origin >/dev/null 2>&1
	    fi

        git tag old-origin origin/master >/dev/null 2>&1
    fi

	if hash timeout >/dev/null 2>&1; then
		timeout 3s git fetch origin 2>/dev/null
		if [[ "$?" == "124" ]]; then
			echo -e "\033[31m*\033[0m timeout: git fetch origin"
	        popd &>/dev/null
			return
		fi
	else
		git fetch origin 2>/dev/null
	fi

	if [[ -n "$(git whatchanged HEAD..origin/master)" ]]; then
		echo -ne "\033[31m*\033[0m dotfile updates found, merge now? (Y/n) "
		read _merge;
		if [[ $_merge != 'n' ]]; then
			_local_commits="$(git rev-list old-origin..HEAD	 | xargs echo)"
			_local_changes="$(git status | grep '^# Change')"

			if [[ -n "$_local_commits" ]]; then
				echo -e "\033[31m*\033[0m You have some local commits. I will now try to use the new origin/master"
				echo "  and apply the extra commits afterwards."
			fi

			if [[ -n "$_local_changes" ]]; then
				echo -e "\033[31m*\033[0m You have some uncommited local changes."
				echo "  I will try to preserve it using the git stash."
				git stash >/dev/null || return
			fi

			git reset --hard origin/master >/dev/null

			# update old-origin
			git tag -d old-origin >/dev/null 2>&1
			git tag old-origin origin-master >/dev/null 2>&1

			if [[ -n "$_local_commits" ]]; then
                echo -e "\033[33m*\033[0m cherry-pick: $_local_commits"
				git cherry-pick $_local_commits  >/dev/null || return
			fi

			if [[ -n "$_local_changes" ]]; then
				git stash pop >/dev/null || return
			fi

			if [[ -e management/symlinks.md5 ]]; then
				if hash md5sum 2> /dev/null ; then
					md5sum -c management/symlinks.md5 --status 2>/dev/null
				else
					if hash md5 2> /dev/null ; then
						md5 -c management/symlinks.md5 >/dev/null
					fi
				fi

				if [[ $? -ne 0 ]]; then
					echo -e "\033[33m*\033[0m symlink mapping changed!"
					echo -e "\033[33m*\033[0m Maybe you should execute godot.sh again..."
				fi
			else
				echo -e "\033[33m*\033[0m symlink mapping checksum not found"
				echo -e "\033[33m*\033[0m Maybe you should execute godot.sh again."
			fi

			popd &>/dev/null
			exec $SHELL
		fi
	fi

	popd &>/dev/null
}

# module load magic
_load() {
	local base path
	local pedantic=0

	[[ -z $1 || -z $2 ]] && return

	base=${_BASHRC_DIR}/${1}

	if [[ $1 == "common" || $2 == "init" ]]; then
		path=${2}.sh
		pedantic=1
	else
		case $1 in
			(dist)  path=${_DISTNAME}/${2}.sh ;;
			(node)  path=${_NODENAME}/${2}.sh ;;
			(net)	path=${_NETNAME}/${2}.sh ;;
		esac
	fi

	if [[ -r ${base}/${path} ]]; then
	    source ${base}/${path} 2>&1 > /dev/null
	elif [[ $2 == "*" ]]; then
	    for file in ${base}/${path}; do
		if [[ -r $file ]]; then
		    source $file 2>&1 > /dev/null
		fi
	    done
	elif [[ ${pedantic} -eq 1 ]]; then
	    echo "error: cannot find necessary startup file: ${base}/${path}"
	fi
}

# update first
_self_update

_load common '*'

# load internals
_load dist  init
_load node  init
_load net   init

# load common distribution settings
_load dist common

# load distribution specific node settings
_load node ${_DISTNAME}

# initialize preexec hack last
init_preexec_hack