From c4c00a8701b9bf884e05f6e168b42758b652b5a6 Mon Sep 17 00:00:00 2001
From: Jacob Babor <jacob@babor.tech>
Date: Thu, 15 Jun 2023 15:31:12 -0500
Subject: [PATCH] Add autocategorize service

---
 base/.config/systemd/user/autocategorize      | 40 +++++++++++++++++++
 .../systemd/user/autocategorize.service       |  9 +++++
 .../.config/systemd/user/autocategorize.timer | 11 +++++
 3 files changed, 60 insertions(+)
 create mode 100755 base/.config/systemd/user/autocategorize
 create mode 100644 base/.config/systemd/user/autocategorize.service
 create mode 100644 base/.config/systemd/user/autocategorize.timer

diff --git a/base/.config/systemd/user/autocategorize b/base/.config/systemd/user/autocategorize
new file mode 100755
index 00000000..35c16f10
--- /dev/null
+++ b/base/.config/systemd/user/autocategorize
@@ -0,0 +1,40 @@
+#! /bin/sh
+
+set -e
+
+# Pull in user-dirs.dirs if it exists
+userdirs="${XDG_CONFIG_DIR:-$HOME/.config}"/user-dirs.dirs
+[ -f "$userdirs" ] && . "$userdirs"
+# Obtain download directory from there, otherwise use default
+downloaddir="${XDG_DOWNLOAD_DIR:-$HOME/Downloads}"
+documentsdir="${XDG_DOCUMENTS_DIR:-$HOME/Documents}"
+picturesdir="${XDG_PICTURES_DIR:-$HOME/Pictures}"
+
+# Sort out downloads
+[ -e "$downloaddir" ] && {
+	# Sort out epubs
+	if [ -d ~/Books ]; then
+		echo "Sorting out books..."
+		find "$downloaddir" \
+			-type f \
+			-iname "*.epub" -or \
+			-iname "*.mobi" \
+			-print \
+			-exec mv {} ~/Books \;
+	fi
+	# Remove anything that's wicked old
+	echo "Removing ancient downloads..."
+	find "$downloaddir" \
+		-mtime +180 \
+		-print -delete
+	# Remove anything kinda old but really big
+	echo "Removing large downloads..."
+	find "$downloaddir" \
+		-mtime +30 -size 10M \
+		-print -delete
+	# Remove zips, but at a lower tolerance
+	echo "Removing old zips..."
+	find "$downloaddir" \
+		-mtime +30 -type f -iname "*.zip" \
+		-print -delete
+}
diff --git a/base/.config/systemd/user/autocategorize.service b/base/.config/systemd/user/autocategorize.service
new file mode 100644
index 00000000..599b05ad
--- /dev/null
+++ b/base/.config/systemd/user/autocategorize.service
@@ -0,0 +1,9 @@
+# vim:ft=systemd:
+[Unit]
+Description=Autocategorize service
+
+[Service]
+ExecStart=%h/.config/systemd/user/autocategorize
+
+[Install]
+WantedBy=default.target
diff --git a/base/.config/systemd/user/autocategorize.timer b/base/.config/systemd/user/autocategorize.timer
new file mode 100644
index 00000000..8cf7c305
--- /dev/null
+++ b/base/.config/systemd/user/autocategorize.timer
@@ -0,0 +1,11 @@
+# vim:ft=systemd:
+[Unit]
+Description=Autocategorize timer
+
+[Timer]
+Persistent=true
+OnBootSec=15min
+OnCalendar=*-*-* 22:00:00
+
+[Install]
+WantedBy=default.target