#!/bin/sh
# Copyright (c) 2016,2019,2021 Guillaume Outters
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# À inclure dans son .shrc
# Penser à ajouter aussi les amorces spécifiques (darcs.shrc, git.shrc, etc.), APRÈS l'inclusion de celui-ci.

#- memeprojet ------------------------------------------------------------------

# Détermine si nous sommes encore dans le même projet.
# À FAIRE: certains dossiers détectés comme projets (ex.: mon src) ont des sous-dossiers eux-mêmes projets (ex.: src/projets/alboretum). En ce cas il faudrait pouvoir marquer le projet comme "à vérifier à chaque sollicitation (qu'on n'est pas allé dans un sous-projet)", car le test de bascule se contente de vérifier qu'on est toujours dans une sous-arbo de la racine, donc ne verra pas le changement de projet.
memeprojet()
{
	[ -n "$PROJET" ] && case "$PWD/" in "$PROJET"/*) return 0 ;; esac
	
	local type
	
	PROJET="$PWD"
	while [ "$PROJET" != / ]
	do
		for testi in $DANSPROJET_TESTI
		do
			if $testi "$PROJET" ; then return 2 ; fi
		done
		PROJET="`dirname "$PROJET"`"
	done
	echo "# Veuillez vous placer dans un dossier projet." >&2
	PROJET=
	return 1
}

memeprojet_zero()
{
	DANSPROJET_TESTI=
}

memeprojet_zero

#- Commandes génériques --------------------------------------------------------
# Ces commandes sont juste des amorces qui doivent être remplacées par une implémentation spécifique via DANSPROJETS_TESTI.

# Pousse-projet, ou pousse-passé.
pp()
{
	memeprojet || { [ $? = 2 ] && pp "$@" ; return ; }
}

#- Calculs de dates ------------------------------------------------------------

# date: secondes (depuis l'Epoch) en date (+%… à ajouter en paramètre).
datesend()
{
	local secondes="$1" ; shift
	case `uname` in
		FreeBSD) date -r $secondes "$@" ;;
		*) date -d @$secondes "$@" ;;
	esac
}

# date: date (AAAAMMJJHHMMSS) en secondes (depuis l'Epoch)
datedens()
{
	case `uname` in
		FreeBSD) date -j -f %Y%m%d%H%M%S "$1" +%s ;;
		*) date -d "`echo "$1" | sed -E -e 's/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5:\6/'`" +%s ;;
	esac
}

# Initialise le décalage entre date hôte et date désirée.
pp_decalage()
{
	local ep_optionsDateRef ep_dateRef ep_date ep_expr_date ep_a_ce_moment ep_maintenant
	
	local dateur=date
	[ -z "$ep_dateRef" ] || dateur="datesend $ep_dateRef"
	ep_dateRef="`$dateur +%Y%m%d%H%M%S`"
	echo "Date de référence (dernière modif): $ep_dateRef"
	printf "À quelle date commençons-nous notre session ([[[aaaa]mm]jj]hhmm)? "
	read ep_date
	case "$ep_date" in
		*.??) ep_date="`echo "$ep_date" | tr -d .`" ; ep_expr_date="$ep_date\(\)" ;;
		*) ep_expr_date="$ep_date\(..\)" ;;
	esac
	ep_expr_date="`echo "$ep_expr_date" | tr '[0-9]' .`$"
	ep_date="`echo "$ep_dateRef" | sed -e "s/$ep_expr_date/$ep_date\1/"`"
	ep_a_ce_moment="`datedens "$ep_date"`"
	ep_maintenant="`date +%s`"
	ep_decalage="`expr $ep_maintenant - $ep_a_ce_moment`"
	pp_decalage="$ep_decalage"
}

pp_maintenant()
{
	expr "`date +%s`" - $pp_decalage
}
