#!/bin/bash

# ------------------------------------------------------------
# RPC Project :
# RPC_nday_of_month : return number of day in a month
# syntax : RPC_nday_of_month year month
#      ex: RPC_nday_of_month 1997 11
# P. Robert, LPP, 2011-03-24
# ------------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=2

if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : return number of day in a month"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   year month"
   echo "    ex: $appli 1997 11"
   echo "    ex: (1971 < year < 2059)"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi


yyc=$1
mmc=$2

# check input date validity
# -------------------------

if ((test $yyc -lt 1972) || (test $yyc -gt 2058))
   then 
        echo "year must be >= 1972 and =< 2058"
        echo "$appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

if ((test $mmc -lt 1) || (test $mmc -gt 12))
   then 
       echo "month must be >= 1 and =< 12"
       echo ""
       echo "$appli36 : *** ERROR ! Command aborted." >&2
       exit 3
fi

# compute nb day in the given month
# ---------------------------------

nfeb=28
if test $yyc == 1972 ; then nfeb=29 ; fi
if test $yyc == 1976 ; then nfeb=29 ; fi
if test $yyc == 1980 ; then nfeb=29 ; fi
if test $yyc == 1984 ; then nfeb=29 ; fi
if test $yyc == 1988 ; then nfeb=29 ; fi
if test $yyc == 1992 ; then nfeb=29 ; fi
if test $yyc == 1996 ; then nfeb=29 ; fi
if test $yyc == 2000 ; then nfeb=29 ; fi
if test $yyc == 2004 ; then nfeb=29 ; fi
if test $yyc == 2008 ; then nfeb=29 ; fi
if test $yyc == 2012 ; then nfeb=29 ; fi
if test $yyc == 2016 ; then nfeb=29 ; fi
if test $yyc == 2020 ; then nfeb=29 ; fi
if test $yyc == 2024 ; then nfeb=29 ; fi
if test $yyc == 2028 ; then nfeb=29 ; fi
if test $yyc == 2032 ; then nfeb=29 ; fi
if test $yyc == 2038 ; then nfeb=29 ; fi
if test $yyc == 2044 ; then nfeb=29 ; fi
if test $yyc == 2048 ; then nfeb=29 ; fi
if test $yyc == 2054 ; then nfeb=29 ; fi
if test $yyc == 2058 ; then nfeb=29 ; fi


if test $mmc == 01 ; then njm=31    ; fi
if test $mmc == 02 ; then njm=$nfeb ; fi
if test $mmc == 03 ; then njm=31    ; fi
if test $mmc == 04 ; then njm=30    ; fi
if test $mmc == 05 ; then njm=31    ; fi
if test $mmc == 06 ; then njm=30    ; fi
if test $mmc == 07 ; then njm=31    ; fi
if test $mmc == 08 ; then njm=31    ; fi
if test $mmc == 09 ; then njm=30    ; fi
if test $mmc == 10 ; then njm=31    ; fi
if test $mmc == 11 ; then njm=30    ; fi
if test $mmc == 12 ; then njm=31    ; fi

echo $njm
# ------------------------------------------------------------
