#!/bin/sh

BASE="/mnt/sdb/www/mocus/code"
AUTHOR_NAME="robot"
AUTHOR_EMAIL="robot@andromeda"

QUOTE=$(shuf -n 1 "$BASE/git-utils/quotes")

for dir in "$BASE"/*; do
    [ -d "$dir" ] || continue

    cd "$dir" || continue

    git rev-parse --is-inside-work-tree >/dev/null 2>&1 || continue

    if [ -n "$(git status --porcelain)" ]; then
        if [ -f ".build.status" ]; then
            MSG="$(basename "$dir"): $(cat .build.status)"
        else
            MSG="$QUOTE"
        fi
        git add -A >/dev/null 2>&1
        GIT_AUTHOR_NAME="$AUTHOR_NAME" \
        GIT_AUTHOR_EMAIL="$AUTHOR_EMAIL" \
        GIT_COMMITTER_NAME="$AUTHOR_NAME" \
        GIT_COMMITTER_EMAIL="$AUTHOR_EMAIL" \
        git commit -m "$MSG" >/dev/null 2>&1
    fi
done
