#!/bin/sh

# this is literally the simplest gitweb update possible.  You are free to add
# whatever you want and contribute it back, as long as it is upward
# compatible.

# ----------------------------------------------------------------------
# delete the 'description' file that 'git init' created if this is run from
# the post-create trigger.  However, note that POST_CREATE is also called from
# perms (since POST_CREATE doubles as eqvt of POST_COMPILE to propagate ad hoc
# permissions changes for wild repos) and then you should not delete it.
[ "$1" = "POST_CREATE" ] && [ "$4" != "perms" ] && rm -f $GL_REPO_BASE/$2.git/description 2>/dev/null

plf=`gitolite query-rc GITWEB_PROJECTS_LIST`
[ -z "$plf" ] && plf=$HOME/projects.list
# since mktemp does not honor umask, we just use it to generate a temp
# filename (note: 'mktemp -u' on some systems, this gets close enough)
tmpfile=`mktemp $plf.tmp_XXXXXXXX`
rm -f $tmpfile;

if [ "$1" = "POST_CREATE" ] && [ -n "$2" ]
then
    # just one to be done
    repo="$2"
    grep -v "^$repo.git$" $plf > $tmpfile
    if gitolite access -q $repo gitweb R any || gitolite git-config -q -r $repo gitweb\\.
    then
        echo "$repo.git" >> $tmpfile
    fi
else
    # all of them
    (
        gitolite list-phy-repos | gitolite access % gitweb R any | grep -v DENIED
        gitolite list-phy-repos | gitolite git-config -r % gitweb\\.
    ) |
        cut -f1 | sort -u | sed -e 's/$/.git/' > $tmpfile
fi

[ -f $plf ] && perl -e "chmod ( ( (stat('$plf'))[2] & 07777 ), '$tmpfile')"
mv $tmpfile $plf
