Terrible.

#!/bin/sh

#
# Setup
#

# Pull out the variables we care about.
previous=$1
new=$2
branch=$3

# For debugging purposes, we write out a file to the temp location.
date > $TEMP/install-branch-local-config.txt
echo "previous $previous" >> $TEMP/install-branch-local-config.txt
echo "new $new" >> $TEMP/install-branch-local-config.txt
echo "branch $branch" >> $TEMP/install-branch-local-config.txt

#
# Branch Naming
#

# Figure out which branch we're going to be changing to. This is somewhat of an
# inelegant method that just cycles through the heads (which contains a hash)
# and figures out which branch head (based on the name). If there are multiple
# heads for that commit, it will pick a random one. Likewise, if a specific
# revision is checked out, this will assume "none".
new_branch_name="none"

for i in .git/refs/heads/*
do
    hash=$(cat $i)

    if [ "z$hash" = "z$new" ]
    then
        new_branch_name=$(basename "$i")
        echo "trying new $i" >> $TEMP/install-branch-local-config.txt
    fi
done

# Figure out which branch we were on
old_branch_name="none"

for i in .git/refs/heads/*
do
    hash=$(cat $i)

    if [ "z$hash" = "z$previous" ]
    then
        old_branch_name=$(basename "$i")
        echo "trying previous $i" >> $TEMP/install-branch-local-config.txt
    fi
done

#
# Perform the manipulation
#

# Figure out which files you want to swap around. We are using Local.config while
# storing the old versions in Local-branchName.config. As we switch branches, we
# move the files around.
#
# In addition, Local-*.config is added to .git/info/exclude as a local change.
web_folder="Website"
local_config="$web_folder/Local-$new_branch_name.config"
old_local_config="$web_folder/Local-$old_branch_name.config"

# Report the files we're dealing with.
echo >> $TEMP/install-branch-local-config.txt
echo "branch name $new_branch_name" >> $TEMP/install-branch-local-config.txt
echo "web folder $web_folder" >> $TEMP/install-branch-local-config.txt
echo "local config $local_config" >> $TEMP/install-branch-local-config.txt

# Save off the old local config. We make sure we have a valid branch
# then move Local.config into Local-branchName.config.
if [ "z$old_branch_name" != "znone" ]
then
    if [ -f $web_folder/Local.config ]
    then
        cp $web_folder/Local.config $old_local_config
    fi
fi

# See if we have a new local config based on the branch, then move
# it into the local config.
if [ -f $local_config ]
then
    echo "found a file $local_config" >> $TEMP/install-branch-local-config.txt
    cp $local_config $web_folder/Local.config
else
    echo "using standard config" >> $TEMP/install-branch-local-config.txt
    rm -f $web_folder/Local.config
fi

# Always touch Web.config to force IIS to restart
touch $web_folder/Web.config
2013-10-08