From 5c049f8bd147c232e3b7985bb8bc92503dab537f Mon Sep 17 00:00:00 2001
From: Timmy Keller <tjk@tjkeller.xyz>
Date: Mon, 2 Sep 2024 20:41:30 -0500
Subject: mimewiz script

---
 misc/mime    | 85 ------------------------------------------------------------
 misc/mimewiz | 19 ++++++++++++++
 2 files changed, 19 insertions(+), 85 deletions(-)
 delete mode 100755 misc/mime
 create mode 100755 misc/mimewiz

(limited to 'misc')

diff --git a/misc/mime b/misc/mime
deleted file mode 100755
index 7c6a649..0000000
--- a/misc/mime
+++ /dev/null
@@ -1,85 +0,0 @@
-#!/bin/sh
-# Media type csv caching
-cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mimewiz"
-mtypesrc="https://www.iana.org/assignments/media-types"
-mtypes="`grep -v '#'`" << TYPES
-#application
-audio
-font
-#example
-image
-#message
-#model
-#multipart
-text
-video
-TYPES
-nmtypes="`cat`" << TYPES
-pdf,
-spreadsheet ms-excel,
-wordprocessing ms-word,
-presentation ms-powerpoint,
-drawing,
-zip gzip,
-TYPES
-
-dlcsv() { curl -sS -C - -o "$2" "$1" || exit; }
-
-# Download all media type csv's if they're not avaliable
-mkdir -p "$cachedir"
-for mtype in $mtypes; do
-	mtypefile="$cachedir/$mtype.csv"
-	[ -e "$mtypefile" ] || dlcsv "$mtypesrc/$mtype.csv" "$mtypefile"
-done
-
-interactiveopt() {
-	# Usage: interactiveopt "options" "prompt" "maxopts (if unset than equal to inf)" "col"
-	# Purpose: give the user a numbered list of options. User can specify multiple choices. User can specify either the number or the name of the option.
-	# Output: saved in variable "opt"
-	unset opt # This won't be set if using recursion, do it in a while loop instead
-	while [ -z $opt ] 2>/dev/null; do
-		# Column option
-		[ -n "$4" ] && [ "$4" = "col" ] \
-			&& echo "$1" | nl -s': ' | column \
-			|| echo "$1" | nl -s': '
-		# Parse user input
-		read -p "$2" choices \
-			&& choices="$(echo $choices | cut -d' ' -f1-$3)"
-		for choice in $choices; do
-			case "$choice" in
-				[0-9]*)	opt="$(echo "$1" | head -"$(echo $choice | tr -cd '[:digit:]')" | tail -1) $opt"	;;
-				*) 	opt="$(echo "$1" | grep "^$choice$" 2>/dev/null) $opt"					;;
-			esac || ( echo "Invalid option \"$choice\"" && undet opt && break ) # Why wont this unset opt or break???????
-		done
-	done
-}
-
-
-# Choose mimetype:
-mtypes="$mtypes\n$(echo "$nmtypes" | cut -d',' -f1)"
-interactiveopt "$mtypes" "Choose mimetype: " 1
-mtype="$(echo "$opt" | cut -d' ' -f1)"
-echo "$mtype"
-echo
-
-# Choose subtype
-mtypefile="$cachedir/$mtype.csv"
-[ -e "$mtypefile" ] && interactiveopt "all\n$(cut -d',' -f1 "$mtypefile" | tail -n +2)" "Choose $mtype subtype: " "" col
-mfiletypes=" $opt "
-echo
-
-# Choose program
-interactiveopt "$(ls /usr/share/applications | sed 's/\.desktop//')" "Choose program to open with: " "" col
-program="$opt"
-echo "$program"
-echo
-
-#echo "$mfiletypes" | grep ' all ' && mfiletypes="$(cut -d',' -f1 "$cachedir/$mtype.csv" | tail -n +2)"
-
-# Xdg-mime commands
-#cut -d',' -f2 "$cachedir/image.csv"
-
-for mfiletype in $(cut -d',' -f1 "$cachedir/$mtype.csv" | tail -n +2); do
-	echo "Changing default program for $mfiletype to $program"
-	xdg-mime default "$program.desktop" $mfiletype
-done
diff --git a/misc/mimewiz b/misc/mimewiz
new file mode 100755
index 0000000..a275bcc
--- /dev/null
+++ b/misc/mimewiz
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+config="${XDG_CONFIG_HOME:-~/.config}/mimewiz"
+#mkdir -p "$config"
+
+install_default_mimetypes() {
+	for app in "$config"/*; do
+		desktop="$(basename "$app").desktop"
+		types="$(tr '\n' ' ' < "$app")"
+		echo "Setting $desktop as default application for types listed in $app"
+		xdg-mime default $desktop $types
+	done
+}
+
+case $1 in
+	-i) install_default_mimetypes; break ;;
+esac
-- 
cgit v1.2.3