scripts/shell/strip_ics_alarms.sh
2024-03-20 11:28:46 -05:00

70 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Given an ICS input file, write a new ICS output file with all the
# alarms stripped out (BEGIN:VALARM -> END:VALARM)
#
# SPDX-License-Identifier: MIT
if [ ! -r "$1" ]; then
echo "Cannot read input file $1, exiting."
exit 1
fi
INFILE=$1
OUTFILE="${INFILE}.stripped"
# create/truncate it so we can use >> later
: > "${OUTFILE}"
VERBOSE=1
READLINES=0
WROTELINES=0
EVENTCOUNT=0
ALARMCOUNT=0
GOTALARM=0
if [ ${VERBOSE} ]; then
echo -n "Working"
fi
# preserve whitespace, don't interpret escapes
# shellcheck disable=SC2004
while IFS=$'\n' read -r vline; do
READLINES=$(($READLINES+1))
if [ ${VERBOSE} ]; then
if [ $(($READLINES%100)) -eq 0 ]; then
echo -n "."
fi
fi
# use substring matching to avoid possible '\r' and '^M' on lines
if [ "${vline:0:12}" == "BEGIN:VEVENT" ]; then
EVENTCOUNT=$(($EVENTCOUNT+1))
fi
if [ ${GOTALARM} -ne 1 ]; then
if [ "${vline:0:12}" != "BEGIN:VALARM" ]; then
# don't interpret escapes while writing
echo -E "${vline}" >> "${OUTFILE}"
WROTELINES=$(($WROTELINES+1))
continue
else
GOTALARM=1
continue
fi
else
if [ "${vline:0:10}" == "END:VALARM" ]; then
GOTALARM=0
ALARMCOUNT=$(($ALARMCOUNT+1))
fi
continue
fi
done < "${INFILE}"
if [ ${VERBOSE} ]; then
echo "done."
echo "Read ${READLINES} lines"
echo "Wrote ${WROTELINES} lines"
echo "Found ${EVENTCOUNT} events"
echo "Stripped ${ALARMCOUNT} alarms"
fi
exit 0