From: Simon Ruderich Date: Sun, 15 Feb 2009 16:23:01 +0000 (+0100) Subject: Add link.sh to create symbolic links to conf files. X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=c53733ec69115579bfd9a3bf1233cfac59a36468;p=config%2Fdotfiles.git Add link.sh to create symbolic links to conf files. It's used by Makefiles in subdirectories (like mail, shell, etc.) to create symbolic links in ~/ to their configuration files so all configuration data can be kept in one place. --- diff --git a/link.sh b/link.sh new file mode 100755 index 0000000..6a92038 --- /dev/null +++ b/link.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# Creates a symbolic link for file $1 in dirname of $2 with name of basenmae +# $2. +# +# `./link.sh example ~/.examplerc` creates a symbolic link to example +# (wherever it is located) in ~/ named .examplerc. + + +# Get all necessary paths. +pwd=`pwd` +base=`dirname "$2"` +source=`echo -n "$pwd/$1" | sed "s|$base/||"` +target=`basename "$2"` + +# Go to the directory where the link is going to be created. +cd "$base" + +# Abort if the target file exists and is no symbolic link. Prevents +# overwriting real files. +if [ -e "$target" -a ! -h "$target" ]; then + echo "target '$target' exists already and is no symbolic link!" >&2 + exit 1 +fi + +# Make sure the source exists. +if [ ! -e "$source" ]; then + echo "source '$source' doesn't exist!" >&2 + exit 1 +fi + +# Create the new symbolic link; remove the old one if necessary. +rm -f "$target" +ln -s "$source" "$target"