Erinnermich/add_date.sh

81 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
### This shell script is used to create new events in caldav on the owncloud server
### Copied from https://gist.github.com/remyd1/a353b07219884c5878cc27d04aab9d96 and modified to fit my needs.
# Usage output string. $0 is filename.
usage="$0 categories date description [dtstart] [dtend]
where categories is a comma separated list that MUST start with FRIST, WF or TERMIN
and the second item in this list denotes the required alarm set."
# print usage string if improper options, or if requested.
if [ "$#" -lt 3 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "$usage"
exit 1
else
# read login credentials and config
BASEPATH=$(dirname $(realpath $0))
. $BASEPATH/.env
# generate random UID starting here
random1=`</dev/urandom tr -dc '1-9a-z' | head -c8; echo ""`
random2=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random3=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random4=`</dev/urandom tr -dc '1-9a-z' | head -c4; echo ""`
random5=`</dev/urandom tr -dc '1-9a-z' | head -c12; echo ""`
# UID=`</dev/urandom tr -dc '1-9a-z' | head -c10; echo ""`
# ics example 8cbf7d9e-6g68-43b9-zb3c-073a8e6b8f46.ics
UUID=$random1-$random2-$random3-$random4-$random5
ics=$UUID.ics
# done here generating an UID, and defining a filename using that UID
NOW=`date +%Y%m%dT%H%M%S`
# find main category and decide for a template
main_cat=$(echo $1 | cut -d, -f1 | tr 'A-Z' 'a-z')
template=$BASEPATH/templates/template_$main_cat.ics
# initiate iCal file with template
cp $template $ics
# and change some stuff with cli options.
# what we need to change:
# dtstamp, lastmodified, created:
sed -i "s|20160224T172807|$NOW|g" $ics
# date (this script will only add single day events. Use other calDAV front-ends for more sophisticated usage)
sed -i "s|20160225|$2|g" $ics
# categories
sed -i "s|replacemecategory|$1|g" $ics
# replacemealarm[et]
# We have to do quite a lot of shitty magic here to get the right representations for the different kinds of newlines.
alarmt=$($BASEPATH/add_alarm.sh t $1 $2)
alarmt=${alarmt//$'\n'/\\$'\n'}
alarme=$($BASEPATH/add_alarm.sh e $1 $2)
alarme=${alarme//$'\n'/\\$'\n'}
sed -i "s|replacemealarmt|${alarmt//\\n/\\\\n}|g" $ics
sed -i "s|replacemealarmt|${alarme//\\n/\\\\n}|g" $ics
# AktenzeichenXYZ
# human-readable date
sed -i "s|replacemedate|$(date -d $2 +%d.%m.%Y), 2300|g" $ics
# description:
sed -i "s|replacemedescription|${3//$'\n'/\\\\n}|g" $ics
# what we might need to replace, but not even that often:
# dtstart, time component optional. We only work with minutes here:
[ -n $4 ] && sed -i "s|2300|$4|g" $ics
# dtend, same as dtstart:
[ -n $5 ] && sed -i "s|2359|$5|g" $ics
# UUID:
sed -i "s|99g999gggg|$UUID|g" $ics
user=$username:$secret
url=$domain/$username/$calendar
# curl -k --user $user -X PUT -H "Content-Type: text/calendar; charset=utf-8" --data-binary @./$ics --url $url/$ics
cat $ics
fi