#
# Dotfiles management script. For details, run
# $ dotfiles.sh --help
+#
+# Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
VERSION='0.2'
DOTFILES_DIR="${PWD}"
TOUCH=$(which touch)
WGET=$(which wget)
+#####
+# Compatibility checks
+
+BASH="${BASH_VERSION%.*}"
+BASH_MAJOR="${BASH%.*}"
+BASH_MINOR="${BASH#*.}"
+
+if [ "${BASH_MAJOR}" -eq 3 ] && [ "${BASH_MINOR}" -eq 0 ]; then
+ echo "ERROR: ${0} requires Bash version >= 3.1" >&2
+ echo "you're running ${BASH}, which doesn't support += array assignment" >&2
+ exit 1
+fi
+
#####
# Utility functions
for REPO in *; do
if [ "${REPO}" = '*' ]; then
break # no known repositories
+ elif [ -f "${REPO}" ]; then
+ continue # repositories are directories
fi
- "${COMMAND}" "${REPO}" "${@}" || return 1
+ "${COMMAND}" "${@}" "${REPO}" || return 1
done
return
fi
}
# Global variable to allow passing associative arrats between functions
-declare -A REPO_SOURCE_DATA
+
+if [ "${BASH_MAJOR}" -ge 4 ]; then
+ declare -A REPO_SOURCE_DATA
+fi
function set_repo_source()
{
+ if [ "${BASH_MAJOR}" -lt 4 ]; then
+ echo "ERROR: ${0}'s set_repo_source requires Bash version >= 4.0" >&2
+ echo "you're running ${BASH}, which doesn't support associative arrays" >&2
+ return 1
+ fi
REPO=$(nonempty_option 'set_repo_source' 'REPO' "${1}") || return 1
> "${REPO}/source_cache" || return 1
for KEY in "${!REPO_SOURCE_DATA[@]}"; do
# usage: get_repo_source REPO
function get_repo_source()
{
+ if [ "${BASH_MAJOR}" -lt 4 ]; then
+ echo "ERROR: ${0}'s get_repo_source() requires Bash version >= 4.0" >&2
+ echo "you're running ${BASH}, which doesn't support associative arrays" >&2
+ return 1
+ fi
REPO=$(nonempty_option 'get_repo_source' 'REPO' "${1}") || return 1
REPO_SOURCE_DATA=()
if [ -f "${REPO}/source_cache" ]; then
fi
}
+function git_fetch()
+{
+ REPO=$(nonempty_option 'git_fetch' 'REPO' "${1}") || return 1
+ REMOTES=$(cd "${REPO}" && "${GIT}" remote) || return 1
+ if [ -n "${REMOTES}" ]; then
+ (cd "${REPO}" && "${GIT}" pull) || return 1
+ else
+ echo "no remote repositories found for ${REPO}"
+ fi
+}
+
function wget_fetch()
{
REPO=$(nonempty_option 'wget_fetch' 'REPO' "${1}") || return 1
if [ -n "${SERVER_ETAG}" ]; then # store new ETag
REPO_SOURCE_DATA['etag']="${SERVER_ETAG}"
set_repo_source "${REPO}" || return 1
- else
- if [ -n "${ETAG}" ]; then # clear old ETag
- unset "${REPO_SOURCE_DATA['etag']}"
- set_repo_source "${REPO}" || return 1
- fi
+ elif [ -n "${ETAG}" ]; then # clear old ETag
+ unset "${REPO_SOURCE_DATA['etag']}"
+ set_repo_source "${REPO}" || return 1
fi
echo "extracting ${BUNDLE} to ${REPO}"
"${TAR}" -xf "${BUNDLE}" -C "${REPO}" --strip-components 1 --overwrite || return 1
fi
}
+
# usage: link_file REPO FILE
#
# Create the symbolic link to the version of FILE in the REPO
echo "link ${TARGET}/${FILE} to ${DOTFILES_DIR}/${REPO}/patched-src/${FILE}"
else
echo -n 'link '
- "${LN}" -sv "${DOTFILES_DIR}/${REPO}/patched-src/${FILE}" "${TARGET}/${FILE}" || return 1
+ "${LN}" -rsv "${DOTFILES_DIR}/${REPO}/patched-src/${FILE}" "${TARGET}/${FILE}" || return 1
fi
}
# multi-repo case handled in main() by run_on_all_repos()
REPO=$(nonempty_option 'fetch' 'REPO' "${1}") || return 1
maxargs 'fetch' 1 "${@}" || return 1
- get_repo_source "${REPO}" || return 1
- TRANSFER=$(nonempty_option 'fetch' 'TRANSFER' "${REPO_SOURCE_DATA['transfer']}") || return 1
+ if [ "${BASH_MAJOR}" -ge 4 ]; then
+ get_repo_source "${REPO}" || return 1
+ TRANSFER=$(nonempty_option 'fetch' 'TRANSFER' "${REPO_SOURCE_DATA['transfer']}") || return 1
+ else
+ echo "WARNING: Bash version < 4.0, assuming all repos use git transfer" >&2
+ TRANSFER='git'
+ fi
if [ "${TRANSFER}" = 'git' ]; then
- "${GIT}" --git-dir "${REPO}/.git" pull || return 1
+ git_fetch "${REPO}" || return 1
elif [ "${TRANSFER}" = 'wget' ]; then
wget_fetch "${REPO}" || return 1
else
if [ ! -e "${TARGET}/${FILE}" ]; then
echo "${FILE}"
fi
- else
- if [ -f "${TARGET}/${FILE}" ]; then
- (cd "${REPO}/src" && "${DIFF}" -u "${FILE}" "${TARGET}/${FILE}")
- fi
+ elif [ -f "${TARGET}/${FILE}" ]; then
+ (cd "${REPO}/src" && "${DIFF}" -u "${FILE}" "${TARGET}/${FILE}")
fi
done <<-EOF
$(list_files "${REPO}/src")
if [ -f "${FILE}" ]; then
echo "apply ${FILE}"
pushd "${REPO}/patched-src/" > /dev/null || return 1
- "${PATCH}" -p0 < "../../${FILE}" || return 1
+ "${PATCH}" -p1 < "../../${FILE}" || return 1
popd > /dev/null || return 1
fi
done
# remove any files marked for removal in local-patch
for REMOVE in "${REPO}/local-patch"/*.remove; do
if [ -f "${REMOVE}" ]; then
+ echo "apply ${FILE}"
while read LINE; do
if [ -z "${LINE}" ] || [ "${LINE:0:1}" = '#' ]; then
continue # ignore blank lines and comments
BASHRC='no'
while read FILE; do
- if [ "${FILE}" = '.bashrc' ] && [ "$TARGET" -ef "${HOME}" ]; then
+ if [ "${FILE}" = '.bashrc' ] && [ "${TARGET}" -ef "${HOME}" ]; then
BASHRC='yes'
fi
if [ "${DOTFILES_SRC}/${FILE}" -ef "${TARGET}/${FILE}" ] && [ -h "${TARGET}/${FILE}" ]; then