#!/usr/bin/env bash

set -x
set -e

#
# script to tag notmuch emails based on my preference
#
# inspired by https://forsooth.org/posts/email-in-spacemacs/index.html
#
#
# tags: important solaris oci osd
#
#

notmuch tag +sent -unread 'folder:/[Ss]ent.*/'

notmuch tag +me to:petr.nyc@oracle.com

# idea for logic
# sort by projects, don't unset new/read tag
# at the very bottom, unset the new/read tag based on if I'm interested in the latest
# status for the project / logs for the project and so on - good for stepping for someone

# General sorting rules not specific to projects
################################################################################
notmuch tag +important -- \
        tag:new AND "
        (
        subject:/[Cc]omplete.*[Tt]raining/ OR
        subject:/.*Reminder:?/ OR
        from:elanor.cz OR
        from:payroll_cz OR
        from:instructor.cz OR
        subject:Expense Report Approval OR
        subject:/[Aa]ction.[Nn]eeded/ OR
        from:permissions.oci.oraclecloud.com
)"
notmuch tag +general \
        tag:new AND "(
        subject:'Oracle Open Roles' OR
        from:tania.c.cortez OR
        from:info@eduagroup.cz OR
        subject:'FYI: Expense Reimbursement'
)"

notmuch tag +calendar \
        tag:new AND mimetype:text/calendar


# Solaris
################################################################################

# general solaris
notmuch tag +solaris +logs +mrspatmore -- \
        tag:new AND "(
        subject:/.*BugDB.*release.deployment/ OR
        subject:import/sig_ci/mercurial/mrspatmore OR
        (from:vcap_admin_ww_grp AND subject:/WARNING:|INFO:/)
        )"

notmuch tag +solaris +logs \
        tag:new AND 'subject:Next IDR ID is'
notmuch tag +solaris \
        tag:new AND "(
        from:deirdre.williams@oracle.com AND subject:'Sustaining Meeting'
        )"

# phabricator
notmuch tag +phabricator +logs -me \
        tag:new AND \
        from:noreply@scapen-phabricator.us.oracle.com

notmuch tag +s11u3-sru +on \
        tag:new AND "(
        tag:phabricator AND
        subject:/rONCLOSED|rONMAN|rONSRC/ and subject:/11.3/
        )"

notmuch tag +s11u3-sru +userland \
        tag:new AND "(
        tag:phabricator AND
        subject:/\[Diffusion\].*rUSERLAND.*11\.3/
)"

notmuch tag +userland -unread -inbox \
	tag:new AND "(
		from:mailman-owner@userland.us.oracle.com
	)"

notmuch tag +s11u4-sru +userland \
        tag:new AND "(
        tag:phabricator AND
        subject:/\[Diffusion\].*rUSERLAND.*11\.4/
)"

notmuch tag +mrspatmore \
        tag:new AND "(
        tag:phabricator AND \
        subject:/\[Diffusion\].*rMRSPATMORE/
)"

notmuch tag +mrspatmore \
        tag:new AND "(
        tag:phabricator AND \
        subject:/\[Diffusion\].*rREJENKINSLIB/
)"

notmuch tag +IPS \
        tag:new AND "(
        tag:phabricator AND \
        ( ( subject:/\[Differential\]/ and body:REPOSITORY NEAR/1 rPKG ) OR
          ( subject:/\[Diffusion\]/ and body:COMMITS NEAR rPKG )
        )
)"

# akidr
notmuch tag +akidr +logs -unread \
        tag:new AND "(
        from:vcap_admin_ww_grp AND
        subject:'Docker with ARU uploadcli has been automatically frozen'
        )"

notmuch tag +akidr \
        tag:new AND "(
        to:'bot@artas' OR
        from:'bot@artas' OR
        to:'fishworks-gk_ww_grp@oracle.com' OR
        subject:'PASS s11_4aksruidr' OR
        subject:'Build started for AK IDR' OR
        subject:'New AK IDR request for'
)"

notmuch tag +akidr +logs -unread \
        tag:new AND "(
        from:'vcap_admin_ww_grp@oracle.com' AND \
        subject:'ARU Uploads has been frozen for' AND \
        subject:'this is a reminder please review'
)"

notmuch tag +akidr +logs +important \
        tag:new AND "(
        (from:sig-devops_ww_grp OR from:adam.paul) AND \
        subject:/8\.\8.*idr.*DC.*has.failed/
)"


# WebRTI
notmuch tag +webrti \
        tag:new AND "(
        body:Consolidation: AND
        body:RTI: AND
        subject:WebRTI:
        )"
notmuch tag +important \
        tag:new AND "(
        tag:webrti AND
        to:petr.nyc and
        subject:/NEED.APPROVAL.*WebRTI/
        )"


# BugDB
notmuch tag +bugdb \
        tag:new AND "(
        from:ociapp-notifications and subject:/ER|Bug/
        )"

# ON
notmuch tag +on tag:new AND "(tag:webrti AND body:Consolidation: NEAR/1 on"

notmuch tag +s11u3-sru tag:new AND "(tag:webrti AND tag:on AND body:Gates: S11.SRU.Sustaining)"

notmuch tag +s11u3-sru tag:new AND "(subject:/Solaris.*11.3.*[Rr]eleased/)"
notmuch tag +s11u4-sru tag:new AND "(subject:/Solaris.*11.4.*[Rr]eleased/)"

notmuch tag +s11u4-sru +logs +on \
        tag:new and 'from:/root@sig-on-sru11-3-[xs]01/'

notmuch tag +s11u4-sru tag:new AND "(
        tag:webrti AND tag:on AND body:Gates: 11.4-SRU
        )"

notmuch tag +on +pushlog \
        tag:new AND \
        subject:'/export.on-gate.*push/'

notmuch tag +on +trunk \
        tag:new AND "(
        to:gk@hubbins OR to:gatekeeper@hubbins OR to:hg@hubbins OR
        from:gatekeeper@hubbins
        )"

notmuch tag +important \
        tag:new AND "(
        tag:on and subject:error and (from:gatekeeper@hubbins or to:gatekeeper@hubbins)
)"

notmuch tag +important +s11u4-sru +on \
        tag:new AND "(
        from:sig-devops_ww_grp  AND
        subject:/INPUT.NEEDED.*11.4|s11u4_sust.*Ready.for/
)"

notmuch tag +trunk \
        tag:new AND "(
        tag:on AND tag:pushlog body:Branch: default
)"

notmuch tag +s11u3-sru tag:new AND "(
        tag:on AND tag:pushlog body:Branch:  s11u3-sru
)"

notmuch tag +on +trunk +logs \
        tag:new AND \
        '(to:gk-builds@hubbins.us.oracle.com OR to:gk-splice-builds@hubbins.us.oracle.com) AND body:"Build summary"'

notmuch tag +on +trunk +logs -unread \
        tag:new AND "(
        from:noaccess AND
        subject:/boston|eleven|hopper|on-gate-test|severin|smalls|stumpy|zero/
        )"

notmuch tag +on +trunk +jenkins tag:new AND to:gk-jenkins@hubbins.us.oracle.com
notmuch tag +on +trunk +logs \
        tag:new AND "(
        (to:gk@hubbins.us.oracle.com and subject:'zpool report for') OR
        (to:on-parfait@hubbins and subject:'Parfait Differences')
)"

notmuch tag +on +s11u4-sru +logs +important \
        tag:new AND "(
        from:gk@hubbins.us.oracle.com AND
        subject:'New SRU branch from trunk'
)"

notmuch tag +on +trunk +s11u4-sru +s11u3-sru +logs +important \
        tag:new AND subject:on-repo-hooks

notmuch tag +on +s11u4-sru +important +logs \
        tag:new AND 'subject:/URGENT:.production.build-11.4.*/'

notmuch tag +on +pushlog \
        tag:new AND to:hubbins AND 'subject:/.export\/on-gate.*push/'

notmuch tag +on +s11u4-sru +important +logs \
        tag:new AND subject:"fail" AND subject:"Solaris_11/11.4/ON" AND subject:"11.4"

notmuch tag +on +s11u4-sru +important +logs \
        tag:new AND subject:"fail" AND 'subject:/on-11u4-bld/'

notmuch tag +on +gkap +logs +s11u4-sru \
        tag:new AND "(
        from:s11autoport_grp@oracle.com AND
        (subject:'periodic GKAP push analysis') or subject:'GK Backport testing has completed'
)"

notmuch tag +on +gkap +logs +s11u4-sru  \
        "tag:new AND tag:inbox AND (subject:GKAP or subject:GKAB)"

notmuch tag +on +gkap +logs +s11u4-sru \
        "tag:new AND to:rpe_gk_backport_ww_grp and subject:/pass.*on11u4.*scapen/"

notmuch tag +on +gkap +logs +s11u4-sru \
        "tag:new AND from:blduser@bld.us.oracle.com AND to:rpe_gk_backport_ww_grp"

notmuch tag +on +gkap +logs +s11u4-sru \
        "tag:new AND from:rpe_sme_ww_grp AND NOT
                             (subject:/INTERIM.GK.Backport.*/ OR subject:/New.GK.backport.testing.has.started/ OR
                              subject:/^.*GK.Backport.testing.has.completed/ OR
                              subject:/INTERIM.GK.Backport.report.for/)"




notmuch tag +on +gkap +logs +s11u4-sru \
        tag:new AND from:rpe_sme_ww_grp@oracle.com AND subject:"/INTERIM.GK.Backport.report.for.GKAP_11.4.*/"

# notmuch tag +on +gkap +logs +s11u4-sru \
#         "tag:new AND subject:/.*Build.*GKAB.*Completed/"

# notmuch tag +on +gkap +logs +s11u4-sru \
#         "tag:new AND from:rpe_gk_backport_ww_grp AND (subject:pass or subject:successfull or subject:successful)"


notmuch tag +on +logs +s11u4-sru \
        tag:new AND "(
        subject:/on-gate.s11u4..*.packages.ready/ OR
        subject:/pass:.*Solaris_11/11.4/ON/production.build-11.4.*/ OR
        subject:/^pass.*11.4..*.11.4.*..on-11u4-bld-[xs]./
)"

notmuch tag +solaris +logs -unread \
        tag:new AND 'subject:/\[\/var\/tmp\/repo_cache\/PKG\]/'

notmuch tag +on +important \
        tag:new AND \
        to:solaris_sru_gk_ww_grp AND \
        subject:FAILED

# userland
notmuch tag +userland  tag:new AND "subject:GATE OPEN" AND "from:solaris_sru_gk_ww_grp" AND body:userland
notmuch tag +userland  tag:new AND tag:webrti AND "body:/Consolidation.*userland/"
notmuch tag +s11u4-sru tag:new AND tag:userland AND tag:webrti AND body:"/Gates:.*S11.4-SRU/"
notmuch tag +trunk     tag:new AND tag:userland AND tag:webrti AND body:"/Gates:.*solaris-trunk/"
notmuch tag +userland  tag:new AND to:userland-s11-sru-notify
notmuch tag +s11u3-sru \
        tag:new AND \
        tag:userland AND \
        subject:'push to s11u3-sru'
notmuch tag +s11u4-sru \
        tag:new AND \
        tag:userland AND \
        subject:'/.*push.to.s11u4_sust.*/'


# cluster
notmuch tag +cluster \
        tag:new AND "(
        tag:webrti AND (body:Consolidation:  sc-geo or body:Consolidation:  suncluster) OR
        subject:'SC4.4 SRU Nightly' OR from:'SC Build Engineering'
)"
notmuch tag +important \
        tag:new AND \
        tag:cluster and 'subject:/[Ff]ail/'

# IPS
notmuch tag +IPS \
        tag:new AND \
        subject:'[Differential]' AND body:'rPKG Solaris Image Packaging System'

# RE
notmuch tag +RE \
        tag:new AND "(
        to:solaris-re_us_grp OR
        to:ips_tracker_support_ww_grp OR
        from:ips_tracker_support_ww_grp
        )"

notmuch tag +s11u3-sru \
        tag:new AND \
        tag:re AND \
        'subject:/Oracle.Solaris.11\.3/'

notmuch tag +RE +s11u4-sru \
        tag:new AND \
        tag:RE AND \
        'subject:/.*IDRs.*s11_4sru.*/'

notmuch tag +s11u4-sru \
        tag:new AND \
        tag:RE AND \
        'subject:/RTI.*withdrawal.notification/' AND \
        body:'11.4'

notmuch tag +s11u4-sru \
        tag:new AND \
        tag:RE AND \
        'subject:/S11.4.*repo.ready.for.testing/'


notmuch tag +s11u3-sru \
        tag:new AND \
        tag:RE AND \
        'subject:/.*IDRs.*s11_3sru.*/'

notmuch tag +cluster \
        tag:new AND \
        tag:re AND \
        '(subject:/.*IDRs.*cluster.*/ OR subject:/SRU.Build.Package.Profile.successfully.created.for.solariscluster.*/)'

notmuch tag +akidr \
        tag:new AND \
        tag:RE AND \
        'subject:/.*Tracker.FYI.*IDR.*state.change/'

notmuch tag +RE s11u4-sru \
        tag:new AND \
        subject:'[Differential]' AND body:'Repository rRETRUNK Release Engineering Trunk Gate'

notmuch tag +RE +logs -unread \
        tag:new AND "(
        from:noaccess AND
        subject:/ipkg[1-9]/
        )"

notmuch tag +solaris \
        tag:new AND "(
        tag:akidr       OR
        tag:bugdb       OR
        tag:cluster     OR
        tag:IPS         OR
        tag:on          OR
        tag:phabricator OR
        tag:RE          OR
        tag:trunk       OR
        tag:userland    OR
        tag:webrti
)"

# end of Solaris
################################################################################

# OCI / OSD
################################################################################


# seatch term to include in each search
# commented out for debugging
# ST='tag:new'
ST="date:1/1/2026.."

notmuch tag +osd \
        "$ST" AND "(
        to:osd-devops_ww_grp@oracle.com OR \
        subject:[gbu confluence]
)"

notmuch tag +osd +brownbag \
        "$ST" AND 'subject:/[Bb]rown.[Bb]ag/'
notmuch tag +deployment-ticket "$ST" AND tag:osd AND subject:"New OSD deployment ticket:"
notmuch tag +ocean-incident "$ST" AND tag:osd AND subject:"[Ocean Incident]"
notmuch tag +alarm "$ST" AND tag:osd AND subject:'Alarm: '
notmuch tag +osd "$ST" AND subject:'InfraCloud JIRA'
notmuch tag +osd +sgd \
        "$ST" AND subject:'SGD'
notmuch tag +oci +announcement \
        "$ST" AND subject:'A PSA'
# PRs for approval
notmuch tag +osd +PRs \
        "$ST" AND subject:'ACTION REQUIRED - Peer approval needed for Change Request' AND \
        body:'/Service Owner:.*Desktop as a Service/'
# PRs from other projects
notmuch tag +oci +CCAT-PRs \
        "$ST" AND \
        subject:'ACTION REQUIRED - Peer approval needed for Change Request' AND \
        body:'/Service Owner:Compute Cloud at Customer/'

# OCI production regions
notmuch tag +oci +osd +important +region-ga \
        "$ST" AND \
        "from:/OCI-REGION-BUILD.*/"

# Change Freeze
notmuch tag +oci +important +changefreeze \
	"$ST" AND \
	"subject:/Oracle.Change.Management.Alert/"


# ODAAS Deployments Calendar
notmuch tag +osd +calendar \
        "$ST" AND \
        "subject:/\[confluence\].*created.a.new|updated.a.*event.in.the.*OSD.RE.Deployments.*calendar/"

# ODAAS PRs
notmuch tag +osd +PRs\
        "$ST" AND \
        "subject:/ODAAS\/.*Pull.request.*:/"





notmuch tag +oci tag:new AND tag:osd

################################################################################

# I don't want these in my focused inbox
################################################################################

# passed logs for on trunk
notmuch tag -inbox -unread tag:on AND tag:trunk AND tag:logs AND subject:"pass"

# trunk RTIs,pushlog,jenkins logs
notmuch tag -inbox -unread tag:trunk and tag:webrti
notmuch tag -inbox -unread tag:on AND tag:trunk AND tag:pushlog
notmuch tag -inbox -unread tag:on AND tag:trunk AND tag:jenkins

# ON GKAP notifications
notmuch tag -inbox -unread tag:new AND tag:gkap AND tag:logs and not subject:failed

# osd ocean incidents, alarms
notmuch tag -inbox -unread tag:osd AND '(tag:ocean-incident or tag:alarm)'


# emails with subject:"REGION BUILD ALERT]" are important to watch



# ================================================================================
# the very last part of this - remove the new tag
notmuch tag -new tag:new
