#!/bin/bash

#######################################
## notification center for instantOS ##
#######################################

# show notification history in rofi
# enter to open a submenu for reading and deleting
# esc to exit

if ! [ -e /tmp/notifications/notif.txt ]; then
    echo "creating notification dir"
    mkdir /tmp/notifications
    cd /tmp/notifications
    echo "press enter to open submenu" >notif.txt
fi

cd /tmp/notifications

NREAD=$(tac notif.txt | rofi -dmenu -i -p notifications -markup-rows)

while [ -n "$NREAD" ]; do
    CHOICE=$(echo "r: read
x: delete" | instantmenu -x 500 -y 300 -n -w 100 -l 2 -bw 4 -h 50)
    case "$CHOICE" in
    r*)
        echo "$NREAD" |
            sed 's/^(\([0-9]*:[0-9]*\)) \[\(.*\)\] <b>\(.*\)<\/b> | <i>\(.*\)<\/i>/time: \1\nApp:  \2\n      \3\n\n      \4/g ' |
            sed 's/&amp;/&/g' >/tmp/instantmessage
        urxvt -e sh -c "less /tmp/instantmessage"
        ;;
    x*)
        grep -vF "$NREAD" notif.txt >tmp.notif && mv tmp.notif notif.txt
        ;;
    esac
    NREAD=$(tac notif.txt | rofi -dmenu -i -p notifications -markup-rows)
done
