#! /bin/sh

########################################################################
#                 copyright 2001, Amoco Production Company             #
#                             All Rights Reserved                      #
#                     an affiliate of BP America Inc.                  #
########################################################################

#
umask 2

if ( test $# -le 1 || test "${FreeUSP}" = "" ) then
	echo " "
	echo "mancopy:  copy usp manual pages from specified section_# "
	echo "          to remote systems using shadowcopy facility"
	echo "          & to ${FreeUSP}/dist/man/usp/man/[man#,cat#] "
	echo "          The new date is also stamped onto the man page"
	echo "          If a pattern file exists for XIKP, that is also"
	echo "          copied to the ${FreeUSP}/dist/etc/usp/xui_pattern"
        echo "          directory."
	echo " "
	echo "Usage:"
	echo "          mancopy section_# [program names]"
	echo " "
	echo " Please set the env var FreeUSP to the proper location"
	exit 1
fi
#set pgm = $argv[1]
suffix=$1
section=`echo $suffix | sed s/[a-z].*//`
shift
cdir=`pwd`

for args
	do
#
# if there's a pattern file, copy it out to the dist structure
# (modified 2/67/02 to copy all pattern files  - j.m.wade )
#
	for pattern_file in *.pattern
	do
	  if test -f ${pattern_file}
	  then
	    cp ${pattern_file} ${FreeUSP}/dist/etc/usp/xui_pattern/
	  fi
	done
#
# commented out June 07, 2001 - man pages now reside with source
#
# 	cd $FreeUSP/dist/man/usp/man/man${section}
	if (test ! -f $args.${suffix}) then
	  exit 0
	fi
	date="`date '+\"%a %h %d %Y\"' `"
	line=".TH $1 ${suffix}  $date \"From FreeUSP\""
	sed "1,$ s/\.TH.*/$line/" $args.${suffix} > tmp.$$
	"mv" tmp.$$ $args.${suffix}
	cp ${args}.${suffix} ${FreeUSP}/dist/man/usp/man/man${section}

#	if test ! -f ${DEPTROOT}/usp/man/cat1/$args.${suffix}
#	 then
#		catman -t -M ${DEPTROOT}/usp/man
#	fi
#
# Run  a man command native to our architecture to generate the cat1 file.
#
	MANPAGER=cat
	export MANPAGER

	Vendor=`usp-osarch -v`

	if test "${Vendor}" = "Sun"
	then
	  Version=`usp-osarch -R`
	  if test ${Version} -eq 4
	  then
 	    man - -M ${FreeUSP}/dist/man/usp/man $suffix $args
	  else
	    man -F -M ${FreeUSP}/dist/man/usp/man -s $suffix - $args
	  fi
	elif test "${Vendor}" = "Linux"
	then
	  if test -f /etc/redhat-release
	  then
	    man --preformat -M ${FreeUSP}/dist/man/usp/man $suffix $args
	  elif test -f /etc/SuSE-release
	  then
	   echo No point in making cat1 files
	    #man --pager cat -M ${FreeUSP}/dist/man/usp/man $suffix $args
	  else
	    man --preformat -M ${FreeUSP}/dist/man/usp/man $suffix $args
	  fi
	elif test "${Vendor}" = "SGI"
	then
 	  man -M ${FreeUSP}/dist/man/usp/man $suffix $args
	elif test "${Vendor}" = "Darwin"
	then
 	  man -P /bin/cat -M ${FreeUSP}/dist/man/usp/man $suffix $args
	else
 	  man - -M ${FreeUSP}/dist/man/usp/man $suffix $args
	fi

done 
