#!/bin/sh SEARCH_DIR="../blog/" MAX_RESULTS=50 QUERY="$QUERY_STRING" if [ -z "$QUERY" ]; then printf "!Search Sava.Rocks\r\n\r\n" printf "You can search this gopher hole. \r\n" printf "\r\n" printf "7Search the blog\t/search/\r\n" printf "\r\n" exit 0 fi RESULTS=$(find "$SEARCH_DIR" -mindepth 2 -type f -name "gophermap" -print0 \ | xargs -0 grep -liF "$QUERY" 2>/dev/null \ | head -n "$MAX_RESULTS") printf "!Search Sava.Rocks\r\n" printf "\r\n" printf "Search results for '%s'\r\n" "$QUERY" printf "\r\n" [ -z "$RESULTS" ] && { printf "No results found.\r\n" printf "\r\n" printf "7Search again\t/search/\r\n" printf "1Back to my blog\t/blog/\r\n" printf "1Back to my homepage\t/\r\n" exit 0 } get_title() { grep -m 1 '^!' "$1" | sed 's/^! *//' } printf '%s\n' "$RESULTS" | while IFS= read -r FILE || [ -n "$FILE" ]; do REL=${FILE#"$SEARCH_DIR"} # removes "../blog/" SLUG=$(echo "$REL" | sed -E 's#([^/]+)/gophermap#\1#') LINK="/blog/$SLUG/" TITLE=$(get_title "$FILE") [ -z "$TITLE" ] && TITLE="$SLUG" SNIPPET=$(grep -i -m 1 "$QUERY" "$FILE" | cut -c1-70) printf "1$TITLE\t$LINK\r\n" printf "%s\r\n\r\n" "$SNIPPET" done printf "7Search again\t/search/\r\n" printf "1Back to my blog\t/blog/\r\n" printf "1Back to my homepage\t/\r\n"