aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/usr/bin/leap-year
blob: 7fe3cb68f87de0d9b8150606029a97110b682701 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
###################################################################################################
# leap-year
###################################################################################################
# Core algorithm credits to Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# Tweakings & additions made by Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv2 License or above; you may not use this file except in compliance with 
# the License. You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/old-licenses/gpl-2.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if 
#               changes were made. You may do so in any reasonable manner, but not in any way that 
#               suggests the licensor endorses you or your use.
###################################################################################################

###################################################################################################
#
# Checking whether the parameter is a leap year or not
#
###################################################################################################
#
# Parameters:
# ----------
#
# - <year> contains the year to check
#
# - -<anything> will display this help message
#
###################################################################################################
#
# Returned values:
# ---------------
#
# - true: the <year> is a leap year
# - false the <year> is not a leap year
#
###################################################################################################
#
# Usage example:
# -------------
#
# leap-year 2017
#
###################################################################################################
if [[ $LEAP_YEAR == ON ]]; then
        # Getting the last existing file descriptor of ~/.drive-google/logs/drive-google.log
        fd=$(ls -al /proc/$$/fd | grep ~/.drive-google/logs/drive-google.log | sed -E 's|^.* ([0-9]+) -> .*$|\1|g' | sort -V | tail -n 1)

        # Case where fd is unset, i.e the file ~/.drive-google/logs/drive-google.log has not yet been opened
        if [[ -z $fd ]]; then
                lowest_unused_fd () 
                {
                        local fd=0
                        while [ -e /proc/$$/fd/${fd} ]; do
                                fd=$((fd+1))
                        done
                        echo $fd
                }
                fd=$(lowest_unused_fd)

                # Opening ~/.drive-google/logs/drive-google.log as file descriptor $fd for appending
                eval "exec $fd>> ~/.drive-google/logs/drive-google.log"
        fi

        # Writing the trace output generated when set -x is enabled to file descriptor $fd 
        eval "BASH_XTRACEFD=$fd"
        # Logging line numbers - We could also use ${0} for ${BASH_SOURCE}
        BASH_SOURCE_BASENAME=$(basename ${BASH_SOURCE})
        export PS4='${BASH_SOURCE_BASENAME}.${LINENO}+ '
        # Expanding all variables and prints the full commands before output of the command
        set -x
fi


year=$1

# Checking for no parameter or parameter beginning with '-'
if [[ ($# -eq 0) || ($year =~ ^-) ]]; then
        end_of_help_message_line_number=$(awk '/^if \[\[ \$LOG_/{ print NR; exit }' /usr/bin/leap-year)
        ((end_of_help_message_line_number--))
        awk -v var=$end_of_help_message_line_number 'NR >= 20 && NR <= var' /usr/bin/leap-year | zenity --text-info --title "leap-year help" --width 1150 --height 850
        exit 1
else
        if [[ $(($year % 4)) -ne 0 ]] ; then
                # not a leap year
                echo "false"
        elif [[ $(($year % 400)) -eq 0 ]] ; then
                # yes, it's a leap year
                echo "true"
        elif [[ $(($year % 100)) -eq 0 ]] ; then
                # not a leap year
                echo "false"
        else
                # yes, it's a leap year
                echo "true"
        fi
fi