3 # Link each _FILE in the current directory to ~/.FILE
5 # Originally by Steve Kemp (http://www.steve.org.uk/)
7 # By default, fixup only replaces missing files and simlinks. You can
8 # optionally overwrite any local files and directories by passing the
11 FORCE="no" # If "yes", overwrite existing .files
12 DRY_RUN="no" # If "yes", disable any actions that change the filesystem
27 # Create the symbolic link.
29 # linkFiles( $file, $dotfile )
32 # file - The file we're processing '_foo'
33 # dotfile - The file it should be linked to in ~/, e.g. '.foo'
34 function linkFiles ( )
38 ln -s `pwd`/$file ~/$dotfile
41 # Check if a file is patch controlled
43 # isPatchFile( $file, $patchfiles)
46 # file - The file we're processing '_foo'
47 # patchfiles - A string list of patchfiles
48 function isPatchFile( )
54 for patchfile in $patchfiles; do
55 if [ $file == $patchfile ]; then
62 # Check if a file is controlled by the dotfiles framework
64 # isControlledFile( $file, $patchfiles )
67 # file - The file we're processing '_foo'
68 # patchfiles - A string list of patchfiles
69 function isControlledFile( )
76 if [ ! -e ~/$dotfile ]; then
77 #echo "~/$dotfile is controlled (does not exist)"
79 elif [ -h ~/$dotfile ]; then
80 #echo "~/$dotfile is controlled (a symlink)"
82 elif isPatchFile $file $patchfiles; then
83 #echo "~/$dotfile is controlled (a patchfile)"
86 #echo "~/$dotfile is not controlled"
90 # Check if the installed file matches the dotfiles version
92 # fileChanged( $file, $dotfile )
95 # file - The file we're processing '_foo'
96 # dotfile - The file it should be linked to in ~/, e.g. '.foo'
97 function fileChanged()
101 DIFF=`diff -r ~/$dotfile $file`
106 # Prettyprint a list of files
108 # listFiles( $title, $list )
111 # title - The name of the list
112 # list - The files in the list
118 if [ $# -gt 0 ]; then
120 for file in $files; do
127 # See if we can find any _files.
130 if [ -e $file ]; then
131 found=`expr $found + 1`
135 # If we found none then exit
136 if [ "$found" -lt 1 ]; then
137 echo "WARNING: No files matching _* were found"
141 # If a local.patch file exists, apply it's changes to our dotfiles
142 # files. We catch the output of this to get a list of the files under
143 # local.patch control
144 if [ -f "local.patch" ]; then
146 if [ $DRY_RUN == "yes" ]; then
147 patchoption="--dry-run"
149 echo "\$ patch $patchoption -i local.patch"
150 patchout=`patch $patchoption -i local.patch || exit 1`
154 # patching file _emacs
155 # patching file _gnuplot
156 PATCHFILES=`echo "$patchout" | sed -n 's/patching file //p'`
157 #listFiles "Patched files" $PATCHFILES
165 # For each dotfile in this directory.
167 # Create .dotfile version.
170 # Decide what to do with files we don't normally control
172 if ! isControlledFile $file $PATCHFILES; then
173 if [ $FORCE == "yes" ]; then
175 UPDATED="$UPDATED ~/$dotfile"
176 if [ $DRY_RUN == "no" ]; then
177 # Back up the ~/$dotfile
178 mv ~/$dotfile ~/$dotfile.bak
181 IGNORED="$IGNORED ~/$dotfile"
186 # Targets getting to this point should be controlled
187 if [ -e ~/$dotfile ]; then
188 # The target exists, see if it has changed
189 if fileChanged $file $dotfile; then
190 NOT_CHANGED="$NOT_CHANGED ~/$dotfile"
193 if [ $OVERRIDDEN == "no" ]; then
194 UPDATED="$UPDATED ~/$dotfile"
196 if [ $DRY_RUN == "no" ]; then
197 # Back up the ~/$dotfile
198 mv ~/$dotfile ~/$dotfile.bak
202 if [ $OVERRIDDEN == "no" ]; then
203 ADDED="$ADDED ~/$dotfile"
206 if isPatchFile $file $PATCHFILES; then
207 if [ $DRY_RUN == "no" ]; then
208 # Install the patched ~/$dotfile
212 if [ $DRY_RUN == "no" ]; then
213 # Install a symlink ~/$dotfile
214 linkFiles $file $dotfile
219 listFiles "Added" $ADDED
220 listFiles "Updated" $UPDATED
221 listFiles "NotChanged" $NOT_CHANGED
222 listFiles "Ignored" $IGNORED
224 # Revert the action of the patch on our dotfiles files now that we've
225 # installed the patched versions.
226 if [ -f "local.patch" ] && [ $DRY_RUN == "no" ]; then
228 echo '$ patch -i local.patch -R'
229 patch -i local.patch -R