#!/bin/sh
#
# Copyright (c) 2005 Daichi GOTO <daichi@ongs.co.jp>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
# THE POSSIBILITY OF SUCH DAMAGE.

# author: Daichi GOTO (daichi@ongs.co.jp)
# first edition: Tue Mar 22 20:35:43 2005
# last modified: $Date: 2005/04/21 14:26:13 $
# version: $Revision: 1.8 $

# shsubrmerge
#    shsubrmerge command_file
#
# usage
#    shsubrmerge command_file


# error
#    print error message
# usage
#    error_msg='error message'
#    error
error()
{
    echo "$error_msg" 1>&2
}

# usage
#    print usage
# usage
#    usage_msg='usage message'
#    usage
usage()
{
    echo "$usage_msg" 1>&2
}

# version
#    print version
# usage
#    version '$Revision: 1.19 $'
version()
{
    ver=$1
    ver=${ver#* }
    echo ${ver% $}
}

usage_msg='usage:
    shsubrmerge command_file
     -h             print help message
     -v             print version'

# check the option arguments
#
while getopts hv option
do
    case "$option" in
    h|\?)
        usage
        exit 0
        ;;
    v)
        version '$Revision: 1.8 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))

[ 1 != $# ] && { usage; exit 1; }
[ ! -f "$1" ] && { error_msg="${0##*/}: no such file"; error; exit 1; }

# temporary directory
#
tempdir=$(mktemp -d ~/."${0##*/}".XXXXXX)
trap "rm -rf \"$tempdir\"; exit 0" EXIT HUP INT QUIT ALRM TERM

# checkout function of .shsubr
#
funcmode=false
cat "${0%/*}"/.shsubr |
while IFS= read line
do
    [ false = $funcmode ] && {
        echo "$line" | LANG=C grep '^# ' > /dev/null && {
            [ -z "$comment" ] &&
                comment=$(echo "$line") ||
                comment=$(echo "$comment"; echo "$line")
            continue
        }

        echo "$line" | LANG=C grep '^$' > /dev/null && {
            comment=
            continue
        }

        echo "$line" | LANG=C grep -E '^[0-9A-Za-z_-]*\(\)$' > /dev/null && {
            func=
            funcmode=true
            funcname=${line%()*}
        }
    }

    func=$(echo "$func"; echo "$line")

    [ } = "$line" ] && {
        echo -n "$comment" > "$tempdir"/"$funcname"
        echo "$func" >> "$tempdir"/"$funcname"
        funcmode=false
        comment=
    }
done

# check out functions used in command
#
functions=
commandlist=$(ls "$tempdir")
for command in $commandlist
do
    LANG=C grep -- "$command" "$1" > /dev/null && {
        [ -z "$functions" ] &&
            functions="$(cat "$tempdir/$command")" ||
            functions="$(echo "$functions"; echo; cat "$tempdir/$command")"
    }
done

# merger shsubr
#
includeline=$(cat -n "$1" | grep '/*}"/.shsubr' | head -1 | awk '{print $1}')

head -$(($includeline - 1)) "$1"
echo "$functions"
tail -$(( $(wc -l "$1" | awk '{print $1}') - $includeline)) "$1"
