#!/bin/sh
# $Id: git-mclone,v 1.11 2019/08/22 02:35:38 friedman Exp $

case $1 in
    --bare        ) mode=$1 ; shift  ;;
    --mirror      ) mode=$1 ; shift  ;;
    --shared      ) mode=$1 ; shift  ;;
    --no-checkout ) mode=$1 ; shift  ;;
    *             ) mode=--recursive ;;
esac

emptydir=${XDG_RUNTIME_DIR-${TMPDIR-/tmp}}/empty.$$
trap 'rm -rf "$emptydir"' 0 1 2 3 15
(umask 077; mkdir -p "$emptydir")

for url in "$@"; do
    localdir=${url##*/}
    localdir=${localdir%.git}.git  # make sure it ends in .git

    echo "** $url"
    ${GITPROG-git} clone $mode --template "$emptydir" -- "$url" "$localdir" || continue

    if [ -e "$localdir/.git" ]; then
        ${GITPROG-git} -C "$localdir" restore-commit-mtime
    else # bare repo
        if type git-lfs > /dev/null 2>&1; then
            ${GITPROG-git} -C "$localdir" lfs fetch --all
        fi

        tm=`${GITPROG-git} -C "$localdir" log -n1 --format=tformat:%ci`
        find -- "$localdir" -exec touch -d "$tm" '{}' +
    fi
    echo
done

# eof
