#!/bin/sh
logcount=$(wc -l "/var/gopher/listing.gophermap" | awk '{print $1}')

if [ -z "$1" ] || [ "$1" -eq "$1" ] 2>/dev/null
then
  awk -v tot="${logcount}" '{gsub("^0",tot-NR+1" >> ",$0);print $0}' "/var/gopher/listing.gophermap" | sed "s|\\t.*||" | head -n "${1:-5}"
else
  awk -v tot="${logcount}" '{gsub("^0",tot-NR+1" >> ",$0);print $0}' "/var/gopher/listing.gophermap" | grep -i "$*" | sed "s|\\t.*||" | head -n 5
fi
