-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslog
executable file
·176 lines (152 loc) · 4.71 KB
/
sslog
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
clear
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
source ${SCRIPT_DIR}/env_sslog
if ! test -f "$SCRIPT_DIR/settings"; then
printf "${RED}The settings have not been configured. Executing ./init_sslog.\n${DEF}"
$SCRIPT_DIR/init_sslog
fi
source $SCRIPT_DIR/settings
source $SCRIPT_DIR/functions_sslog
# -------------------------------------------------------- Retrieve data ------------------------------------------------------- #
SRC_FOLDER="$SCRIPT_DIR/src"
printf "
$PURPLE
█▀ █▀▀ █ █ █▀█ █ ▄▀█ █▀█ █▀ █ █ █ █▀█ █ █▀█ █▀▀ ▀█▀ █ █▀▄▀█ █▀▀
▄█ █▄▄ █▀█ █▄█ █▄▄ █▀█ █▀▄ ▄█ █▀█ █ █▀▀ █▄▄ █▄█ █▄█ █ █ █ ▀ █ ██▄
____________________________________________________________________
$DEF
"
if ! command -v jq &>/dev/null; then
echo "$JQ_NOT_INSTALLED" 1>&2
exit 1
fi
login=""
month=""
option_s=""
# Handles options and parameters
while [[ $# -gt 0 ]]; do
case "$1" in
-l|--login)
if [[ -z "$2" ]]; then # Checks if the following argument exists or not
printf "$MISSING_LOGIN" 1>&2
exit 1
fi
if [[ -z "$login" ]]; then
login="$2"
else
printf "$ALREADY_SET_LOGIN" 1>&2
exit 1
fi
shift 2 # Shifts the command line arguments to the left by 2 positions
;;
-m|--month)
if [[ -z "$2" ]]; then
printf "$MISSING_MONTH" 1>&2
exit 1
fi
if [[ -z "$month" ]]; then
month="$2"
else
printf "$ALREADY_SET_MONTH" 1>&2
exit 1
fi
shift 2
;;
-s|--show)
option_s="-s"
shift
;;
-h|--help)
show_help
exit 0
;;
*)
printf "$UNRECOGNIZED_ARG" 1>&2
show_help
exit 1
;;
esac
done
if [ -z "$login" ]; then # Check if a login is in argument
if [ -z "$LOGIN" ]; then # if not, check if a login is in .env_sslog
printf "$SET_LOGIN" 1>&2
exit 1
else
LOGIN=$LOGIN
fi
else
LOGIN=$login
fi
# --------------------------------------------------- Calculate current date --------------------------------------------------- #
year=$(date +%Y)
last_year=$(date +%Y)
current_day=$(date +%d)
if [[ -z "$month" ]]; then
month=$(date +%m)
printf "$CURRENT_MONTH$(write_month $month)\n"
if [ "$current_day" -gt 26 ]; then
month=$(expr $month + 1)
if [ "$month" -gt 12 ]; then
month=1
fi
fi
else
month=$(format_date $month)
if [ "$month" -lt 1 ] || [ "$month" -gt 12 ]; then
printf "$NO_EXIST_MONTH" 1>&2
exit 1
else
printf "$CHOSEN_MONTH$(write_month $month)\n"
if [ "$month" -gt "$(date +%m)" ]; then
year=$(expr $year - 1)
last_year=$(expr $last_year - 1)
fi
fi
fi
last_month=$(expr $month - 2)
if [ "$last_month" -le 0 ]; then
last_month=$(expr 12 + $last_month)
fi
if [ "$last_month" -gt "$month" ] && [ "$last_year" -eq "$(date +%Y)" ]; then
last_year=$(expr $last_year - 1)
fi
last_month=$(format_date $last_month)
# ---------------------------------------------------------- API CALL ---------------------------------------------------------- #
curl -s -k "https://calendrier.api.gouv.fr/jours-feries/metropole.json" | jq -r 'to_entries[] | "\(.key) : \(.value)"' | \
sort -r | grep -E "(^$last_year-)|(^$year-)" > $SCRIPT_DIR/holidays.txt
RESPONSE=$(curl -s --request POST \
--url "https://api.intra.42.fr/oauth/token" \
--header "content-type: application/json" \
--data "{
\"grant_type\": \"client_credentials\",
\"client_id\": \"$UID_42\",
\"client_secret\": \"$SECRET_42\"
}")
if [[ "$RESPONSE" == *"error"* ]]; then
printf "${BOLD}${RED}Error: Failed to obtain access token (check UID and SECRET / execute ./init_sslog)\n${DEF}"
exit 1
fi
TOKEN_42=$(echo $RESPONSE | jq -r '.access_token')
if [ -z "$TOKEN_42" ]; then
printf "${BOLD}${RED}Error: Failed to obtain access token (check UID and SECRET / execute ./init_sslog)\n${DEF}"
exit 1
fi
DATES=$(curl -s -H "Authorization: Bearer $TOKEN_42" "https://api.intra.42.fr/v2/users/$LOGIN/locations_stats?begin_at=$last_year-$last_month-27&end_at=$year-$month-26" | jq -r 'to_entries[] | "\(.key) : \(.value)"' | cut -c 1-21)
if [[ -z "$DATES" ]]; then
printf "$EXIST_LOGIN" 1>&2
exit 1
else
echo "$DATES" > $SCRIPT_DIR/dates.txt
printf "$LOGIN_IS$LOGIN\n"
fi
# ------------------------------------------------------- PROGRAM LAUNCH ------------------------------------------------------- #
make -C $SCRIPT_DIR > /dev/null 2>&1
mv $SCRIPT_DIR/calculator $SRC_FOLDER/calculator > /dev/null 2>&1
if [ $? != 0 ]; then
printf "$ERR_COMPILE" 1>&2
exit 1
fi
$SRC_FOLDER/calculator $SCRIPT_DIR $month $year $option_s
make -C $SCRIPT_DIR fclean > /dev/null 2>&1
rm $SRC_FOLDER/calculator > /dev/null 2>&1