#
# 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}"
for REPO in *; do
if [ "${REPO}" = '*' ]; then
break # no known repositories
+ elif [ -f "${REPO}" ]; then
+ continue # repositories are directories
fi
"${COMMAND}" "${@}" "${REPO}" || return 1
done
# usage: link_file REPO FILE
#
# Create the symbolic link to the version of FILE in the REPO
-# repository, overriding the target if it exists.
+# repository, overriding the target if it exists. If you want to
+# override the options passed to ${LN}, set LINK_OPTS.
function link_file()
{
REPO=$(nonempty_option 'link_file' 'REPO' "${1}") || return 1
FILE=$(nonempty_option 'link_file' 'FILE' "${2}") || return 1
+ LINK_OPTS="${LINK_OPTS:--sv}" # default to `-sv`
if [ "${BACKUP}" = 'yes' ]; then
if [ -e "${TARGET}/${FILE}" ] || [ -h "${TARGET}/${FILE}" ]; then
if [ "${DRY_RUN}" = 'yes' ]; then
if [ "${DRY_RUN}" = 'yes' ]; then
echo "link ${TARGET}/${FILE} to ${DOTFILES_DIR}/${REPO}/patched-src/${FILE}"
else
+ SOURCE="${DOTFILES_DIR}/${REPO}/patched-src/${FILE}"
echo -n 'link '
- "${LN}" -sv "${DOTFILES_DIR}/${REPO}/patched-src/${FILE}" "${TARGET}/${FILE}" || return 1
+ "${LN}" ${LINK_OPTS} "${SOURCE}" "${TARGET}/${FILE}" || return 1
fi
}
cat <<-EOF
- usage: $0 ${COMMAND} [--force|--force-file] [--dry-run] [--no-backup] [REPO]
+ usage: $0 ${COMMAND} [--force] [--force-dir] [--force-file] [--force-link]
+ [--dry-run] [--no-backup] [--relative] [REPO]
Where 'REPO' is the name the dotfiles repository to link. If it
is not given, all repositories will be linked.
- By default, link.sh only replaces missing files and simlinks. You
- can optionally overwrite any local files by passing the --force
- option.
+ By default, ${COMMAND} only replaces missing directories, files,
+ simlinks. You can optionally overwrite any local stuff by passing
+ the --force option. If you only want to overwrite a particular
+ type, use the more granular --force-dir, etc.
+
+ If you have coreutils 8.16 (2012-03-26) or greater, you can set
+ the --relative option to create symlinks that use relative paths.
EOF
}
function link()
{
- FORCE='no' # If 'file', overwrite existing files.
- # If 'yes', overwrite existing files and dirs.
+ FORCE_DIR='no' # If 'yes', overwrite existing directories.
+ FORCE_FILE='no' # If 'yes', overwrite existing files.
+ FORCE_LINK='no' # If 'yes', overwrite existing symlinks.
DRY_RUN='no' # If 'yes', disable any actions that change the filesystem
- BACKUP='yes'
+ BACKUP_OPT='yes'
+ LINK_OPTS='-sv'
while [ "${1::2}" = '--' ]; do
case "${1}" in
'--force')
- FORCE='yes'
+ FORCE_DIR='yes'
+ FORCE_FILE='yes'
+ FORCE_LINK='yes'
+ ;;
+ '--force-dir')
+ FORCE_DIR='yes'
;;
'--force-file')
- FORCE='file'
+ FORCE_FILE='yes'
+ ;;
+ '--force-link')
+ FORCE_LINK='yes'
;;
'--dry-run')
DRY_RUN='yes'
;;
'--no-backup')
- BACKUP='no'
+ BACKUP_OPT='no'
+ ;;
+ '--relative')
+ LINK_OPTS="${LINK_OPTS} --relative"
;;
*)
echo "ERROR: invalid option to link (${1})" >&2
DOTFILES_SRC="${DOTFILES_DIR}/${REPO}/patched-src"
while read FILE; do
+ BACKUP="${BACKUP_OPT}"
if [ "${DOTFILES_SRC}/${FILE}" -ef "${TARGET}/${FILE}" ]; then
- continue # already simlinked
- fi
- if [ -d "${DOTFILES_SRC}/${FILE}" ] && [ -d "${TARGET}/${FILE}" ] && \
- [ "${FORCE}" != 'yes' ]; then
- echo "use --force to override the existing directory: ${TARGET}/${FILE}"
- continue # allow unlinked directories
- fi
- if [ -e "$TARGET/${FILE}" ] && [ "${FORCE}" = 'no' ]; then
- echo "use --force to override the existing target: ${TARGET}/${FILE}"
- continue # target already exists
+ if [ "${FORCE_LINK}" = 'no' ]; then
+ # don't prompt about --force-link, because this will happen a lot
+ continue # already simlinked
+ else
+ # don't backup links that already point to the right place
+ BACKUP='no'
+ fi
+ else
+ if [ -d "${DOTFILES_SRC}/${FILE}" ] && [ -d "${TARGET}/${FILE}" ] && \
+ [ "${FORCE_DIR}" = 'no' ]; then
+ echo "use --force-dir to override the existing directory: ${TARGET}/${FILE}"
+ continue # allow unlinked directories
+ elif [ -f "${TARGET}/${FILE}" ] && [ "${FORCE_FILE}" = 'no' ]; then
+ echo "use --force-file to override the existing target: ${TARGET}/${FILE}"
+ continue # target already exists
+ fi
fi
link_file "${REPO}" "${FILE}" || return 1
done <<-EOF
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