#!/bin/bash

### Copyright notice at the bottom of this file ###

# $Id: getman,v 1.7 2012/01/08 19:15:37 adamm Exp adamm $
#
# $Source: /home/adamm/src/getman/RCS/getman,v $


###
### Extract the text of a man page from a program; mainly
### used for C programs, but could be used for any file
###


export PROG=$(basename $0)
export STR1="===== MAN PAGE STARTS HERE ====="
export STR2="===== MAN PAGE STOPS HERE ====="


main() {
    # GNU-style help
    if [ "x$1" == "x--help" ] || [ "x$1" == "x--man" ] ; then
	usage
	exit 0
    fi

    if [ $# -ne 1 ] ; then
	usage 1>&2
	exit 1
    fi

    case "x$1" in
    x-s)
	printf "%s\n%s\n" "$STR1" "$STR2"
	exit 0
	;;

    x-*)
	printf "%s: unknown option: '%s'\n\n" $PROG "$1" 1>&2
	usage 1>&2
	exit 1
	;;

    *)
	awk '
	BEGIN {
	    copy = 0
	}
	/'"$STR1"'/ {
	    copy = 1
	    next
	}
	/'"$STR2"'/ {
	    copy = 0
	    next
	}
	copy == 1 {
	    print $0
	}' $1
    	;;
    esac
} # main


usage() {
    printf "Usage:\n"
    printf "    getman file\n"
    printf "        extract man page from file\n\n"
    printf "    getman -s\n"
    printf "        show separators\n"
} # usage


main "$@"


###
### Copyright, 2009, Adam Moskowitz. All rights reserved.
###
### Any redistribution of this software must retain the above copyright
### notice, this list of conditions, and the following disclaimer.
###
### Without specific prior written permission from the copyright holder,
### you may not charge a fee for the redistribution of this software.
###
### All other redistribution and use is hereby permitted.
###
### This software is provided "as is" and without any express or implied
### warranties, including, without limitation, the implied warranties of
### merchantability and fitness for a particular purpose.
###
