#!/bin/bash # Create whatis file for bash scripts DIR="$HOME/bin" LOCALDIR="/usr/local/bin" MANDIR="$HOME/share/man" notexistapp() { echo "$(basename $0): '$1' does not exist" } notexistwhatis() { echo "$(basename $0): whatis for '$1' does not exist" } while getopts ":lL:dwx:Xu:amfe:r:inv?" OPTION do case $OPTION in l) LIST=true;; # list script(s) L) grepfile="$OPTARG"; LIST=true;; # list script d) NODESC=true;LIST=true;; # list scripts with empty descriptions w) NOWHAT=true;LIST=true;; # list scripts with empty whatis x) EXCLUDE=true # exclude script app=$OPTARG [ -f "$DIR/$app" ] && touch "$MANDIR/man1/$app.1" || notexistapp $app;; X) LISTEXCLUDE=true # list excluded scripts for i in $MANDIR/man1/*; do [ ! -s $i ] && echo $(basename $i); done;; u) UPDATE=true # update script app=$OPTARG;; a) for i in $(mkhelp -wn); do # update all scripts mkhelp -u $i; done;; m) MISSING=true;; # list missing scripts f) FORCE=true;; # force (update/remove) e) EDITWHATIS=true # edit whatis file app=$OPTARG whatisfile="$MANDIR/man1/$app.1" if [ -f "$whatisfile" ]; then $EDITOR "$whatisfile" makewhatis $MANDIR 2>/dev/null else notexistwhatis $app; fi;; r) REMOVE=true # remove whatis file app=$OPTARG;; i) echo "Updating whatis index." # update whatis index makewhatis $MANDIR 2>/dev/null;; n) NAME=true;; # print script name only v) VERBOSE=true;; # verbose mode *) [ $(which getusage) ] && getusage $0 exit;; esac done [ $# == 0 ] && getusage $0 && exit if [ $LIST ]; then for i in $DIR/*; do if [ -f "$i" ] && [ -x "$i" ]; then # && [ "$(sed -n '1p' $i | grep "bash")" ] app=$(basename $i) appdesc=$(sed -n '2p' $i | grep '^# ' | sed 's/# //') whtdesc=$(whatis $app | grep "^$app(" | sed "s/$app(.) *- //") [ $NODESC ] && [ "$appdesc" != "" ] && unset app [ $NODESC ] && [ "$whtdesc" != "" ] && unset app [ $NOWHAT ] && [ "$whtdesc" != "" ] && unset app # grep specific file [ "$grepfile" != "" ] && [ "$app" != "$grepfile" ] && unset app # exclude tagged files [ -e "$MANDIR/man1/$app.1" ] && [ ! -s "$MANDIR/man1/$app.1" ] && unset app # exclude compiled scripts [ "$(file $i | grep 'shell script')" == "" ] && unset app if [ $app ]; then printf $app [ -e "$MANDIR/man1/$app.1" ] && # check for non-mkhelp manfiles [ $(wc -l $MANDIR/man1/$app.1 | cut -d" " -f1) -gt 2 ] && printf " [!]" (( cols=$(tput cols)-${#app} )) [ $NAME ] && echo || echo "-> $appdesc// $whtdesc" | cut -c 1-$cols fi fi done fi if [ $UPDATE ]; then whatisfile="$MANDIR/man1/$app.1" if [ -f "$whatisfile" ] && [ ! $FORCE ]; then echo "$(basename $0): whatis for '$app' already exists" exit fi if [ -f "$whatisfile" ]; then if [ $(wc -l $MANDIR/man1/$app.1 | cut -d" " -f1) -gt 2 ]; then echo "$(basename $0): whatis for '$app' was not created by $(basename $0)" exit fi fi if [ -f "$DIR/$app" -o -f "$LOCALDIR/$app" ] && [ -x "$DIR/$app" -o -x "$LOCALDIR/$app" ]; then if [ -f "$DIR/$app" ]; then if [ "$(sed -n '1p' $DIR/$app | grep "bash")" ]; then echo "Updating whatis for '$app' from description." appdesc=$(sed -n '2p' $DIR/$app | grep '^# ' | sed 's/# //' | sed 's/^[A-Z]/\L&/') else TEMPLATE=true fi else TEMPLATE=true fi [ $TEMPLATE ] && echo "Creating whatis template for '$app'." && appdesc="" echo ".SH NAME" > $whatisfile echo "$app \- $appdesc" >> $whatisfile makewhatis $MANDIR 2>/dev/null echo ": $(whatis $app | grep "^$app(" | sed "s/$app(.) *- //")" else notexistapp $app fi fi # cleanup whatis files (remove deliberately and missing) removewhatis() { local $INTERACTIVE INTERACTIVE="-i" [ $FORCE ] && unset INTERACTIVE && echo "Removing whatis for '$1'." if [ $( cat "$MANDIR/man1/$1.1" | wc -l) -gt 2 ]; then echo "$(basename $0): WARNING: whatis for '$1' contains more than shname" INTERACTIVE="-i" fi [ -f "$MANDIR/man1/$1.1" ] && rm $INTERACTIVE $MANDIR/man1/$1.1 || notexistwhatis $1 } if [ $MISSING ]; then for i in $MANDIR/man1/*; do app=$(basename -s.1 $i) # exclude ronn [ "$app" == "ronn" ] && unset app [ "$(echo $app | grep ".1.ronn")" ] && unset app [ ! -f "$DIR/$app" ] && [ ! -f "$LOCALDIR/$app" ] && [ "$app" ] && removewhatis $app done makewhatis $MANDIR 2>/dev/null fi if [ $REMOVE ]; then removewhatis $app makewhatis $MANDIR 2>/dev/null fi