From 684292b90bdd829ead19047f51d28709113f5705 Mon Sep 17 00:00:00 2001
From: Case Duckworth
Date: Fri, 22 May 2020 17:12:15 -0500
Subject: Download non-text mimetypes

---
 bollux | 59 ++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 19 deletions(-)

diff --git a/bollux b/bollux
index 0234282..0361545 100755
--- a/bollux
+++ b/bollux
@@ -31,6 +31,10 @@ put() { printf '%s\n' "$*"; }
 # lower = more important
 log() { # log [LEVEL] [<] MESSAGE
 	case "$1" in
+	-)
+		lvl="-1"
+		shift
+		;;
 	[0-5])
 		lvl="$1"
 		shift
@@ -125,10 +129,8 @@ request() { # request [-s SERVER] URL
 # cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt
 handle() { # handle URL < RESPONSE
 	url="$(_address "$1")"
-	resp="$(cat)"
-	head="$(sed 1q <<<"$resp")"
-	body="$(sed 1d <<<"$resp")"
 
+	head="$(sed 1q)"
 	code="$(awk '{print $1}' <<<"$head")"
 	meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")"
 
@@ -148,7 +150,7 @@ handle() { # handle URL < RESPONSE
 		21) log 5 "- End of client certificate session" ;;
 		*) log 2 "- Unknown response code: '$code'." ;;
 		esac
-		display <<<"$body"
+		display "$meta"
 		;;
 	3*) # REDIRECT
 		log 3 "Redirecting"
@@ -157,7 +159,7 @@ handle() { # handle URL < RESPONSE
 		31) log 5 "- Permanent" ;;
 		*) log 2 "- Unknown response code: '$code'." ;;
 		esac
-		((RDRS+=1))
+		((RDRS += 1))
 		((RDRS > MAXR)) && die "$code" "Too many redirects!"
 		bollux "$meta"
 		;;
@@ -202,20 +204,39 @@ handle() { # handle URL < RESPONSE
 }
 
 # display the page
-display() { # display <<< STRING
-	# normalize line endings to "\n"
-	awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}'
-	# TODO: use less with linking and stuff
-	# less -R -p'^=>' +g
-	# lesskey:
-	# l /=>\n # highlight links
-	# o pipe \n open_url # open the link on the top line
-	# u shell select_url % # shows a selection prompt for all urls (on screen? file?)
-	# Q exit 1  # for one of these, show a selection prompt for urls
-	# q exit 0  # for the other, just quit
-	###
-	# also look into the prompt, the filename, and input preprocessor
-	# ($LESSOPEN, $LESSCLOSE)
+display() { # display MIMETYPE < DOCUMENT
+	mimetype="$1"
+	case "$mimetype" in
+	text/*)
+		# normalize line endings to "\n"
+		awk 'BEGIN{RS=""}{gsub("\r\n?","\n");print}'
+		# TODO: use less with linking and stuff
+		# less -R -p'^=>' +g
+		# lesskey:
+		# l /=>\n # highlight links
+		# o pipe \n open_url # open the link on the top line
+		# u shell select_url % # shows a selection prompt for all urls (on screen? file?)
+		# Q exit 1  # for one of these, show a selection prompt for urls
+		# q exit 0  # for the other, just quit
+		###
+		# also look into the prompt, the filename, and input preprocessor
+		# ($LESSOPEN, $LESSCLOSE)
+		;;
+	*)
+		tn="$(mktemp)"
+		dd status=progress >"$tn"
+		fn="$DLDR/${url##*/}"
+		if [[ -f "$fn" ]]; then
+			log - "Saved '$tn'."
+		else
+			if mv "$tn" "$fn"; then
+				log - "Saved '$fn'."
+			else
+				log 0 "Error saving '$fn'."
+			fi
+		fi
+		;;
+	esac
 }
 
 ### main entry point ###
-- 
cgit 1.4.1-21-gabe81