#!/bin/sh

usage="Usage : $0 -q `date +'%j'` [-y `date +'%Y'`]"

# gestion de la ligne de commande
if test "$#" -eq 2
then
	if test "$1" != "-q"
	then
		echo $usage
		exit 1
	fi
elif test "$#" -eq 4
then
	if test "$1" != "-q"
	then
		echo $usage
		exit 1
	fi
	if test "$3" != "-y"
	then
		echo $usage
		exit 1
	fi
else
	echo $usage
	exit 1
fi

qte=$2

if test "$4" != ""
then
	annee_act=$4
	val_y=`expr $annee_act + 1 2>> /dev/null`
	if test "$?" != "0"
	then
		echo $usage
		exit 1
	fi
fi

# verification des nombres
val_q=`expr $qte + 1 2>> /dev/null`
if test "$?" != "0"
then
	echo $usage
	exit 1
fi

# depassements de valeurs
if test "$qte" -gt 366
then
	echo $usage
	exit 1
fi

if test "$qte" -lt 1
then
	echo $usage
	exit 1
fi

# comparaison avec le quantieme actuel
if test "$4" = ""
then
	qte_act=`date +'%j'`
	annee_act=`date +'%Y'`

	if test "$qte_act" -gt "$qte"
	then
		continue
	elif test "$qte_act" -lt "$qte"
	then
		# la date recherchee est dans l'annee passee
		annee_act=`expr $annee_act - 1`
	else
		# la date recherchee est celle d'aujourd'hui
		echo `date +'%Y-%m-%d'`
		exit 0
	fi
fi


bis=`expr $annee_act % 4`
if test "$bis" -gt 0
then
	fin_janvier=31
	fin_fevrier=`expr 31 + 28`
	fin_mars=`expr 31 + 28 + 31`
	fin_avril=`expr 31 + 28 + 31 + 30`
	fin_mai=`expr 31 + 28 + 31 + 30 + 31`
	fin_juin=`expr 31 + 28 + 31 + 30 + 31 + 30`
	fin_juillet=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31`
	fin_aout=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31`
	fin_septembre=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30`
	fin_octobre=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31`
	fin_novembre=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30`
	#fin_decembre=`expr 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31`
else
	fin_janvier=31
	fin_fevrier=`expr 31 + 29`
	fin_mars=`expr 31 + 29 + 31`
	fin_avril=`expr 31 + 29 + 31 + 30`
	fin_mai=`expr 31 + 29 + 31 + 30 + 31`
	fin_juin=`expr 31 + 29 + 31 + 30 + 31 + 30`
	fin_juillet=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31`
	fin_aout=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31`
	fin_septembre=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30`
	fin_octobre=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31`
	fin_novembre=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30`
	#fin_decembre=`expr 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31`
fi

# annee non bissextile
if test "$qte" -eq 366 && test "$bis" -gt 0
then
	exit 1
fi

# detection de l'annee bissextile
if test "$qte" -le "$fin_janvier"
then
	mois="01"
	jour=`expr $qte + 0`
elif test "$qte" -le "$fin_fevrier"
then
	mois="02"
	jour=`expr $qte - 31`
elif test "$qte" -le "$fin_mars"
then
	mois="03"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28`"
	else
		jour="`expr $qte - 31 - 29`"
	fi
elif test "$qte" -le "$fin_avril"
then
	mois="04"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31`"
	else
		jour="`expr $qte - 31 - 29 - 31`"
	fi
elif test "$qte" -le "$fin_mai"
then
	mois="05"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30`"
	fi
elif test "$qte" -le "$fin_juin"
then
	mois="06"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31`"
	fi
elif test "$qte" -le "$fin_juillet"
then
	mois="07"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30`"
	fi
elif test "$qte" -le "$fin_aout"
then
	mois="08"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30 - 31`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30 - 31`"
	fi
elif test "$qte" -le "$fin_septembre"
then
	mois="09"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31`"
	fi
elif test "$qte" -le "$fin_octobre"
then
	mois="10"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31 - 30`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31 - 30`"
	fi
elif test "$qte" -le "$fin_novembre"
then
	mois="11"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31`"
	fi
else
	mois="12"
	if test "$bis" -gt 0
	then
		jour="`expr $qte - 31 - 28 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 - 30`"
	else
		jour="`expr $qte - 31 - 29 - 31 - 30 - 31 - 30 - 31 - 31 - 30 - 31 - 30`"
	fi
fi

# affichage de la date trouvee
if test "$jour" -lt 10
then
	echo "${annee_act}-${mois}-0${jour}"
else
	echo "${annee_act}-${mois}-${jour}"
fi