diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95132dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Ignore all emacs temp files +*~ \ No newline at end of file diff --git a/Changelog b/Changelog new file mode 100644 index 0000000..78b8886 --- /dev/null +++ b/Changelog @@ -0,0 +1 @@ +Adding Initial Change Log Entry diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh index f3939e7..410902d 100755 --- a/scripts/bootstrap.sh +++ b/scripts/bootstrap.sh @@ -9,107 +9,10 @@ # so as to update the system cleanly to new versions of the dotfiles repository. # -# Helper Functions - -# Make a backup of the existing file if it exists -# Then link to our supplied dotfile -link() { - # Only make a backup if an existing file is there and is not a link - if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then - cp "$1/$2" "$1/$2.old" - echo "Backed up original $2 to $1/$2.old" - fi - ln -sf "$3" "$1/$2" -} - -copy() { - # Only make a backup if an existing directory is there and is not a link - # Allow passing true as third param to do backups - backup=true - if [ ! -z "$4" ]; then - backup=$4 - fi - overwrite=false - if [ ! -z "$5" ]; then - overwrite=$5 - fi - if [ -f "$1/$2" ] || [ -L "$1/$2" ]; then - if $backup; then - mv "$1/$2" "$1/$2.old" - echo "Backed up original $2 to $1/$2.old" - else - if $overwrite; then - rm -r "$1/$2" - echo "removed original $2" - fi - fi - fi - # Should probably only do this is the source is newer - # Remove the old and copy the new - if $overwrite; then - cp -r "$3" "$1/$2" - fi -} - -link_dir() { - # Only make a backup if an existing directory is there and is not a link - if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then - mv "$1/$2" "$1/$2.old" - echo "Backed up original $2 to $1/$2.old" - fi +BASEDIR=$(dirname $0) - # Don't overwrite links - if [ ! -L "$1/$2" ]; then - echo "ln -sf $3 $1/$2" - ln -sf "$3" "$1/$2" - fi -} - -copy_dir() { - # Only make a backup if an existing directory is there and is not a link - if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then - # Allow passing true as third param to do backups - backup=true - if [ ! -z "$3" ]; then - backup=$4 - fi - if $backup; then - mv "$1/$2" "$1/$2.old" - echo "Backed up original $2 to $1/$2.old" - else - rm -r "$1/$2" - echo "removed original $2" - fi - fi - # Should probably only do this is the source is newer - # Remove the old and copy the new - cp -r "$3" "$1/$2" -} - -# Escape the path so we can use it in sed -escape_path() { - local safe="$(echo $1 | sed 's/\//\\\//g')" - echo $safe -} - -# Clone a git repository into a location -clone_git_repo() { - local target=$1 - local repo=$2 - if [ ! -d "$target" ]; then - mkdir -p "$target" - echo "Cloning [$repo] into [$target]" - git clone "$repo" "$target" - else - # Update the repo otherwise - echo "Updating [$repo]" - cur_dir=$PWD - cd "$target" - git checkout . - git pull - cd $cur_dir - fi -} +# Source the tools script +. ${BASEDIR}/tools.sh set -x set -e diff --git a/scripts/tools.sh b/scripts/tools.sh new file mode 100644 index 0000000..f50eee0 --- /dev/null +++ b/scripts/tools.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash + +# Tools for the scripts + +# Make a backup of the existing file if it exists +# Then link to our supplied dotfile +link() { + # Only make a backup if an existing file is there and is not a link + if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then + cp "$1/$2" "$1/$2.old" + echo "Backed up original $2 to $1/$2.old" + fi + ln -sf "$3" "$1/$2" +} + +copy() { + # Only make a backup if an existing directory is there and is not a link + # Allow passing true as third param to do backups + backup=true + if [ ! -z "$4" ]; then + backup=$4 + fi + overwrite=false + if [ ! -z "$5" ]; then + overwrite=$5 + fi + if [ -f "$1/$2" ] || [ -L "$1/$2" ]; then + if $backup; then + mv "$1/$2" "$1/$2.old" + echo "Backed up original $2 to $1/$2.old" + else + if $overwrite; then + rm -r "$1/$2" + echo "removed original $2" + fi + fi + fi + # Should probably only do this is the source is newer + # Remove the old and copy the new + if $overwrite; then + cp -r "$3" "$1/$2" + fi +} + +link_dir() { + # Only make a backup if an existing directory is there and is not a link + if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then + mv "$1/$2" "$1/$2.old" + echo "Backed up original $2 to $1/$2.old" + fi + + # Don't overwrite links + if [ ! -L "$1/$2" ]; then + echo "ln -sf $3 $1/$2" + ln -sf "$3" "$1/$2" + fi +} + +copy_dir() { + # Only make a backup if an existing directory is there and is not a link + if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then + # Allow passing true as third param to do backups + backup=true + if [ ! -z "$3" ]; then + backup=$4 + fi + if $backup; then + mv "$1/$2" "$1/$2.old" + echo "Backed up original $2 to $1/$2.old" + else + rm -r "$1/$2" + echo "removed original $2" + fi + fi + # Should probably only do this is the source is newer + # Remove the old and copy the new + cp -r "$3" "$1/$2" +} + +# Escape the path so we can use it in sed +escape_path() { + local safe="$(echo $1 | sed 's/\//\\\//g')" + echo $safe +} + +# Clone a git repository into a location +clone_git_repo() { + local target=$1 + local repo=$2 + if [ ! -d "$target" ]; then + mkdir -p "$target" + echo "Cloning [$repo] into [$target]" + git clone "$repo" "$target" + else + # Update the repo otherwise + echo "Updating [$repo]" + cur_dir=$PWD + cd "$target" + git checkout . + git pull + cd $cur_dir + fi +}