Add .fluxbox/keys.
[dotfiles-framework.git] / bin / link.sh
1 #!/bin/bash
2 #
3 # Link each FILE in patched-src to ~/FILE
4 #
5 # By default, link.sh only replaces missing files and simlinks.  You
6 # can optionally overwrite any local files by passing the --force
7 # option.
8
9 if [ -z "${DOTFILES_DIR}" ]; then
10     echo 'DOTFILES_DIR is not set.  Bailing out.'
11     exit 1
12 fi
13
14 DOTFILES_SRC="${DOTFILES_DIR}/patched-src"
15 FORCE='no'   # If 'file', overwrite existing files.
16              # If 'yes', overwrite existing files and dirs.
17 DRY_RUN='no' # If 'yes', disable any actions that change the filesystem
18
19 # parse options
20 while [ -n "${1}" ]; do
21     case "${1}" in
22         '--force')
23         FORCE='yes'
24         ;;
25         '--force-file')
26         FORCE='file'
27         ;;
28         '--dry-run')
29         DRY_RUN='yes'
30         ;;
31     esac
32     shift
33 done
34
35 # Create the symbolic link, overriding the target if it exists.
36 #
37 # link_file( $file )
38 #
39 # Parameters:
40 # file - The file we're processing '.foo'
41 function link_file()
42 {
43     FILE="${1}"
44     if [ -e ~/"${FILE}" ] || [ -h ~/"${FILE}" ]; then
45         if [ "${DRY_RUN}" = 'yes' ]; then
46             echo "move ~/${FILE} to ~/${FILE}.bak"
47         else
48             echo -n 'move '
49             mv -v ~/"${FILE}" ~/"${FILE}.bak" || exit 1
50         fi
51     fi
52     if [ "${DRY_RUN}" = 'yes' ]; then
53         echo "link ~/${FILE} to ${DOTFILES_DIR}/${FILE}"
54     else
55         echo -n 'link '
56         ln -sv "${DOTFILES_DIR}/patched-src/${FILE}" ~/"${FILE}" || exit 1
57     fi
58 }
59
60 cd "${DOTFILES_DIR}/patched-src" || exit 1
61
62 while read FILE; do
63     if [ "${FILE}" = '.' ]; then
64         continue
65     fi
66     FILE="${FILE:2}"  # strip the leading './'
67     if [ "${DOTFILES_SRC}/${FILE}" -ef ~/"${FILE}" ]; then
68         continue  # already simlinked
69     fi
70     if [ -d "${DOTFILES_SRC}/${FILE}" ] && [ -d ~/"${FILE}" ] && \
71         [ "${FORCE}" != 'yes' ]; then
72         echo "use --force to override the existing directory: ~/${FILE}"
73         continue  # allow unlinked directories
74     fi
75     if [ -e ~/"${FILE}" ] && [ "${FORCE}" = 'no' ]; then
76         echo "use --force to override the existing target: ~/${FILE}"
77         continue  # target already exists
78     fi
79     link_file "${FILE}"
80 done < <(find .)