#! /bin/zsh -fue

# invoke repo sync, and then restore the checked-out branches.
# It's assumed they know how to rebase. ( git-rebase-* cmds)

# files created:
#  horizont.pre  horizont.pre.DATE ...w/ stash sha1-es
#  horizont.DATE after.  no stashes !
#  tmp: (full_horizont)

# so what's the version then?

STEP_COLOR=green
VERBOSE=no

save_horizont_and_heads()
{
    start_git_heads=$1
    horizont_file=$2
    # we reused previously created git-heads.txt file.
    # cecho $STEP_COLOR "saving sha1-s in git-heads.txt ... in background"
    # don't fail if the file is missing:
    test -f git-heads.txt && mv -f git-heads.txt $start_git_heads

    cecho $STEP_COLOR "saving sym refs in $horizont_file"
    # bug!
    # tee $horizont_file
    repo_save_horizont |sort > full_horizont
    repo_extract_symbolic.awk < full_horizont > $horizont_file || rm -fv $horizont_file
}

reuse_pre_horizont=n
while getopts :s OPT; do
    case $OPT in
	s|+s)
	    reuse_pre_horizont=y
	    ;;
	*)
	    print "usage: ${0##*/} [+-s} [--] ARGS..."
	    exit 2
    esac
done
shift OPTIND-1
OPTIND=1


day_suffix=$(date +%m-%d)

start_git_heads=git-heads.txt-$day_suffix
if [ $reuse_pre_horizont = "n" ]; then
    save_horizont_and_heads $start_git_heads horizont.pre.$day_suffix
else
    test -r horizont.pre.$day_suffix
fi

cecho blue "Here the sym-ref horizont:"
cat horizont.pre.$day_suffix
# mkdir -p horizonts
#    horizonts/horizont.pre.$day_suffix.pre
ln --force --symbolic horizont.pre.$day_suffix horizont.pre

# the version is "day_suffix"

#cecho red "press enter ==>"
# read a
# This can fail

sync_status=0
ARGS=()
if [ $VERBOSE = yes ]; then
    ARGS+=(--trace)
fi
if time repo $ARGS sync; then
    cecho green -e "\noooooooooooooooooooo ** repo PASSED **\n"
else
    sync_status=1
    cecho red -e   "\n#################### ** repo sync FAILED **\n"
fi

#cecho $STEP_COLOR "saving sym refs in horizont.post"
#repo-repair -t horizont.post horizont.pre

## todo: review & adapt:
if repo-restore.awk < horizont.pre;
then
    cecho green -e "\noooooooooooooooooooo ** rebase PASSED **\n"
else
    {cecho red -e   "\n#################### ** rebase FAILED **\n"; exit 1}
fi



# fixme: no point in stashing again!
cecho cyan "Check any difference"
# todo --dont-stash
repo_save_horizont -s |sort > horizont.$day_suffix
repo_extract_symbolic.awk horizont.$day_suffix > horizont.restored


# can this fail?
if false;
then
    repo_restore_horizont -d horizont.pre horizont.restored && \
    cecho green -e "\noooooooooooooooooooo ** rebase PASSED **\n"  || \
    cecho red -e   "\n#################### ** rebase FAILED **\n"
fi

#todo: this is useless if the `rebase' failed:
# show changelog
cecho $STEP_COLOR "saving sha1-s in git-heads.txt."
# repo forall -c 'echo $REPO_PATH `git log --format="format:%H" --max-count=1`' > git-heads.txt
repo forall -c 'echo $REPO_PATH `git rev-parse HEAD`' > git-heads.txt
if test -e $start_git_heads;
    then
    # --no-pager
    repo-diff $start_git_heads git-heads.txt
fi

exit $sync_status

