#!/bin/sh

GL_PACKAGE_CONF=/tmp/share/gitolite/conf
# must be the same as the value for the same variable in
# $GL_PACKAGE_CONF/example.gitolite.rc.  Sorry about the catch-22 :)

# TODO need to fix for portability to ksh and so on
# TODO need to get the version in there somehow

# This program is meant to be completely non-interactive, suitable for running
# server-side from a "post RPM/DEB install" script, or manually by users.
# Please see the doc/0-user-setup.mkd for details.

# usage:
#   $0 [foo.pub]

# The pubkey filename must end with ".pub" and is mandatory when you first run
# this command.  Otherwise it is optional, and can be used to override a
# pubkey file if you happen to have lost all gitolite-access to the repos (but
# do have shell access via some other means)

die() { echo "$@"; exit 1; }

TEMPDIR=$(mktemp -d)
export TEMPDIR
trap "/bin/rm -rf $TEMPDIR" 0

if [ -n "$GITOLITE_HTTP_HOME" ]
then
    HOME=$GITOLITE_HTTP_HOME
    admin_name=$1
else
    pubkey_file=$1
    admin_name=
    if [ -n "$pubkey_file" ]
    then
        echo $pubkey_file | grep '.pub$' >/dev/null || die "$pubkey_file must end in .pub"
        [ -f $pubkey_file ] || die "cant find $pubkey_file"
        admin_name=` basename $pubkey_file .pub`
        echo $admin_name | grep '@' >/dev/null && die "please don't use '@' in the initial admin name"
    fi
fi

if [ -f $HOME/.gitolite.rc ]
then
    perl -ne 's/^\s+//; s/[\s=].*//; print if /^\$/;' < $GL_PACKAGE_CONF/example.gitolite.rc | sort > $TEMPDIR/.newvars
    perl -ne 's/^\s+//; s/[\s=].*//; print if /^\$/;' < $HOME/.gitolite.rc                   | sort > $TEMPDIR/.oldvars
    comm -23 $TEMPDIR/.newvars $TEMPDIR/.oldvars > $TEMPDIR/.diffvars
    if [ -s $TEMPDIR/.diffvars ]
    then
        cp $GL_PACKAGE_CONF/example.gitolite.rc $HOME/.gitolite.rc.new
        echo new version of the rc file saved in $HOME/.gitolite.rc.new
        echo
        echo please update $HOME/.gitolite.rc manually if you need features
        echo controlled by any of the following variables:
        echo ----
        sed -e 's/^/    /' < $TEMPDIR/.diffvars
        echo ----
    fi
else
    [ -n "$GITOLITE_HTTP_HOME" ] || [ -n "$pubkey_file" ] || die "looks like first run -- I need a pubkey file"
    [ -z "$GITOLITE_HTTP_HOME" ] || [ -n "$admin_name"  ] || die "looks like first run -- I need an admin name"

    cp $GL_PACKAGE_CONF/example.gitolite.rc $HOME/.gitolite.rc
    printf "The default settings in the "rc" file ($HOME/.gitolite.rc) are fine for most\n"
    printf "people but if you wish to make any changes, you can do so now.\n\nhit enter..."
    read i
    ${EDITOR:-vi} $HOME/.gitolite.rc
fi

# setup ssh stuff.  We break our normal rule that we will not fiddle with
# authkeys etc., because in this case it seems appropriate
(
    cd $HOME
    mkdir -p .ssh
    chmod go-rwx .ssh
    touch .ssh/authorized_keys
    chmod go-w . .ssh .ssh/authorized_keys
)

# now we get to gitolite itself

gl-install -q

GL_ADMINDIR=` cd $HOME;perl -e 'do ".gitolite.rc"; print $GL_ADMINDIR'`
REPO_BASE=`   cd $HOME;perl -e 'do ".gitolite.rc"; print $REPO_BASE'  `

[ -f $GL_ADMINDIR/conf/gitolite.conf ] || {
    cat <<EOF > $GL_ADMINDIR/conf/gitolite.conf
        repo    gitolite-admin
                RW+     =   $admin_name

        repo    testing
                RW+     =   @all
EOF
}
[ -n "$pubkey_file" ] && cp $pubkey_file $GL_ADMINDIR/keydir

touch $HOME/.ssh/authorized_keys
gl-compile-conf -q

# setup push-to-admin
od=$PWD
cd; cd $REPO_BASE/gitolite-admin.git
GIT_WORK_TREE=$GL_ADMINDIR git add conf/gitolite.conf keydir
GIT_WORK_TREE=$GL_ADMINDIR git diff --cached --quiet 2>/dev/null || GIT_WORK_TREE=$GL_ADMINDIR git commit -am start
cd $od

# now that the admin repo is created, you have to set the hooks properly; best
# do it by running install again
gl-install -q
