#!/bin/sh                                       
#  This file is part of fidoip. It is free software and it is covered
#   by the GNU general public license. See the file LICENSE for details. */
#  Shows last day of previous month for Unix and Linux. 

unix_cal() {
# return number days in previous month for unix on which cal exist and date use diffrerent options
#For SH/ASH/BASH
y=$(date '+%Y') # Get current year
m=$(date '+%m') # Get current month
m=$(expr $m - 1) # Decrement month
# If month zero, decrement year - equal in bash [[ ${m} == 0 ]] && ((expr $y - 1)) 
if [ "$m" -eq 0 ]; then
    ((expr $y - 1)) 
fi
# If month zero, reset to 12 - equal in bash [[ ${m} == 0 ]] && m=12    
if [ "$m" -eq 0 ]; then
    m=12
fi

# Run cal, and print the last field of the last line with fields.
cal ${m} ${y} | awk 'NF{A=$NF}END{print A}'  
}

linux_cal() {
# return number days in previous month on system on which cal may not exist
date -d "$(date +%Y-%m-01) -1 day" +%d
}

OSTYPE=`uname -o 2>/dev/null || uname -p` 

if [ "$OSTYPE" = Linux ]  ||  [ "$OSTYPE" = GNU/Linux ] || [ "$OSTYPE" = Cygwin ] || [ "$OSTYPE" = Msys ] || [ "$OSTYPE" = MinGW ]
 then
linux_cal
else
unix_cal
fi






