The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/bin/sh

sdsetup=0
replica=$(git config --get sd.local-replica)
if [ -z "$replica" ]; then
    gitdir=$(git rev-parse --git-dir 2>/dev/null)
    if [ -z "$gitdir" ]; then
        echo "Can't find a .git directory anywhere in your current directory"
        echo "or any higher directories."
        exit 1;
    fi
    # Special case: when in the toplevel directory, rev-parse --git-dir only
    # returns '.git' instead of the full path.
    if [ "$gitdir" = ".git" ]; then
        gitdir="$(pwd)/.git"
    fi
    replica="$gitdir/sd"
    echo "You don't appear to have an sd.local-replica defined in your .git/config"
    echo "Setting it to '$replica':"
    echo ""
    echo "  git config --add sd.local-replica $replica"
    echo ""
    git config --add sd.local-replica "$replica"
    sdsetup=1
fi

if [ \( "$1" = "init" \) -o \( "$1" = "clone" \) ]; then
    gitemail=$(git config --get user.email)
    if [ ! -z $gitemail ]; then
        echo "Setting your SD email address to your git email:"
        echo ""
        echo "  git config --get user.email"
        if [ ! -d $replica ]; then
            mkdir $replica
        fi
        echo "  sd config user.email-address $gitemail"
        echo ""
        SD_REPO="$replica" sd config user.email-address $gitemail
    fi
fi

SD_REPO="$replica" exec sd "$@"