badgy/badgy

129 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
CURLIE="curlie"
clockin()
{
${CURLIE} POST ${BADGEURL}/clockin key=${KEY} 2> /dev/null
}
clockout()
{
${CURLIE} POST ${BADGEURL}/clockout key=${KEY} 2> /dev/null
}
validate()
{
${CURLIE} POST ${BADGEURL}/validate key=${KEY} dateof=$(date +'%Y-%m-%d') 2> /dev/null
}
devalidate()
{
${CURLIE} POST ${BADGEURL}/devalidate key=${KEY} dateof=$(date +'%Y-%m-%d') 2> /dev/null
}
s2h()
{
local sc=0
local sign=""
if [ ${1} -lt 0 ]; then
sign="-"
sc=$(echo ${1} | cut -c2-)
else
sc=${1}
fi
local h=$(( sc / 3600))
local m=$(( (sc % 3600) / 60 ))
local s=$(( sc % 60))
printf "%s%dh%02dm" "${sign}" "${h}" "${m}"
}
getNewBalance()
{
local workDay=25200
local blh=$(echo ${1} | cut -d ':' -f 1 | cut -c2-)
local blm=$(echo ${1} | cut -d ':' -f 2)
local bls=$((blh*3600 + blm*60))
local nbls=0
if [ "${bls}" -le "${workDay}" ]; then
nbls=$((${2}-(workDay - bls)))
else
nbls=$((bls+${2}))
fi
echo $nbls
}
getCapitalTime()
{
data=$(${CURLIE} POST ${BADGEURL}/showtimers key=${KEY} dateof=$(date +'%Y-%m-%d') 2> /dev/null)
clocks=$(echo ${data} | jq -rc ".timers[]")
sl=$(echo ${data} | jq -rc ".capitaltime")
dr=0
echo "Timers :"
for cl in ${clocks}
do
starts=$(echo ${cl} | jq -rc ".start")
st=$(date --date="${starts}" +'%s')
ends=$(echo ${cl} | jq -rc ".end")
echo
echo -n " $(date --date=${starts} +"%A %d %B %Y") "
echo -n "$(date --date=${starts} +'%H:%M') to "
if [ "${ends}" != "null" ];then
ed=$(date --date="${ends}" +'%s')
echo -n "$(date --date=${ends} +'%H:%M')"
else
ed=$(date +'%s')
echo -n "....."
fi
timerCount="$((ed - st))"
echo -n " ($(s2h ${timerCount}))"
dr=$(( dr + timerCount))
done
nbl=$(getNewBalance "${sl}" "${dr}")
echo
echo
echo "Your time balance is: ${sl}"
echo "Today you worked: $(s2h ${dr})"
echo
echo "Balance if you validate now: $(s2h ${nbl})"
}
show()
{
${CURLIE} POST ${BADGEURL}/showtimers key=${KEY} dateof=$(date +'%Y-%m-%d') 2> /dev/null
}
if [ -e "${HOME}/.badgyrc" ]; then
. ${HOME}/.badgyrc
else
echo "${HOME}/.badgyrc file is missing, I need it"
echo "Create it with this command:"
echo " echo 'BADGEURL="https://myninebage" KEY="XXXX-XX-XXX-XXX"' > ${HOME}/.badyrc"
exit 2
fi
case ${1} in
in|clockin|c)
clockin | jq
;;
out|clockout|o)
clockout | jq
;;
val|validate|v)
validate | jq
;;
dev|devalidate|d)
devalidate | jq
;;
solde|sld|captial|s)
getCapitalTime
;;
*)
echo `${1}` | jq
;;
esac