#!/bin/sh

trap "rm -f /tmp/$$ /tmp/$$a" 0 1 2 3 15
entab </dev/null >/dev/null
if [ "$?" -ne 0 ]
then	echo "Go to the src/tools/entab directory and do a 'make' and 'make install'." >&2
	echo "This will put the 'entab' command in your path." >&2
	echo "Then run $0 again."
	exit 1
fi
indent -st -npro </dev/null >/dev/null 2>&1
if [ "$?" -ne 0 ]
then	echo "You do not appear to have 'indent' installed on your system." >&2
	exit 1
fi
indent -version -npro </dev/null >/dev/null 2>&1
if [ "$?" -eq 0 ]
then	echo "You appear to have GNU indent rather than BSD indent." >&2
	echo "See the pgindent/README file for a description of its problems." >&2
	EXTRA_OPTS="-ncdb -bli0 -npcs -cli4"
else	echo "Hope you installed /src/tools/pgindent/indent.bsd.patch." >&2
	EXTRA_OPTS="-bbb -cli1"
fi

for FILE
do
	cat $FILE |
	sed 's;/\*  *---;/*---X_X;g' |
	sed 's;\([} 	]\)else[ 	]*\(/\*.*\)$;\1else\
\2;g' | # workaround for indent bug
	detab -t4 -qc |
	sed 's;^DATA(.*$;/*&*/;' >/tmp/$$a    # protect backslashes in DATA() 

# We get the list of typedef's from /src/tools/find_typedef
	indent -bad -bap -bc -bl -d0 -cdb -nce -nfc1 -di12 -i4 -l75 \
		-lp -nip -npro $EXTRA_OPTS \
-TACL \
-TA_Const \
-TA_Expr \
-TA_Indices \
-TAbsoluteTime \
-TAcl \
-TAclId \
-TAclIdType \
-TAclItem \
-TAclMode \
-TAddAttrStmt \
-TAgg \
-TAggFuncInfo \
-TAggInfo \
-TAggState \
-TAggreg \
-TAllocElem \
-TAllocElemData \
-TAllocMode \
-TAllocPointer \
-TAllocSet \
-TAllocSetData \
-TAlterUserStmt \
-TAnyInvalidation \
-TAppend \
-TAppendState \
-TArray \
-TArrayRef \
-TArrayType \
-TAttr \
-TAttrDefault \
-TAttrNumber \
-TAttributeSize \
-TAttributeTupleForm \
-TAuthRequest \
-TAuthRequestPacket \
-TBOX \
-TBTItem \
-TBTItemData \
-TBTMetaPageData \
-TBTPageOpaque \
-TBTPageOpaqueData \
-TBTPageState \
-TBTPriQueue \
-TBTPriQueueElem \
-TBTScanList \
-TBTScanListData \
-TBTScanOpaque \
-TBTScanOpaqueData \
-TBTSortKey \
-TBTSpool \
-TBTStack \
-TBTStackData \
-TBTTapeBlock \
-TBUCKET_INDEX \
-TBackend \
-TBackendId \
-TBackendTag \
-TBindingEnt \
-TBitArray \
-TBitIndex \
-TBlock \
-TBlockId \
-TBlockIdData \
-TBlockNumber \
-TBoolPtr \
-TBucket \
-TBufFlags \
-TBuffer \
-TBufferBlock \
-TBufferDesc \
-TBufferHdr \
-TBufferLock \
-TBufferTag \
-TCHUNK_INFO \
-TCIRCLE \
-TCInfo \
-TCPFunction \
-TCPPFunction \
-TCandidateList \
-TCash \
-TCatCTup \
-TCatCache \
-TCatalogInvalidationData \
-TChangeACLStmt \
-TChar16 \
-TChoose \
-TChromosome \
-TCity \
-TClosePortalStmt \
-TClusterStmt \
-TCmdType \
-TColumnDef \
-TCommandDest \
-TCommandId \
-TCommonScanState \
-TCommonState \
-TConnStatusType \
-TConst \
-TConstrCheck \
-TConstrType \
-TConstraint \
-TCopyStmt \
-TCost \
-TCreatePLangStmt \
-TCreateSeqStmt \
-TCreateStmt \
-TCreateTrigStmt \
-TCreateUserStmt \
-TCreatedbStmt \
-TDateADT \
-TDateTime \
-TDatum \
-TDatumPtr \
-TDefElem \
-TDefineStmt \
-TDeleteStmt \
-TDestroyStmt \
-TDestroydbStmt \
-TDlelem \
-TDllist \
-TDropPLangStmt \
-TDropTrigStmt \
-TDropUserStmt \
-TDynamicFileList \
-TELEMENT \
-TEState \
-TEdge \
-TErrorMessagePacket \
-TExcContext \
-TExcData \
-TExcDetail \
-TExcFrame \
-TExcId \
-TExcMessage \
-TExcProc \
-TException \
-TExecStatus \
-TExecStatusType \
-TExitStatus \
-TExplainState \
-TExplainStmt \
-TExpr \
-TExprContext \
-TExtendStmt \
-TFILE \
-TFUNMAP \
-TFetchStmt \
-TFile \
-TFileName \
-TFixedItem \
-TFixedItemData \
-TFixedStack \
-TFixedStackData \
-TFjoin \
-TFmgrCall \
-TFmgrInfo \
-TFmgrValues \
-TFormData_pg_aggregate \
-TFormData_pg_am \
-TFormData_pg_amop \
-TFormData_pg_amproc \
-TFormData_pg_attrdef \
-TFormData_pg_attribute \
-TFormData_pg_class \
-TFormData_pg_database \
-TFormData_pg_group \
-TFormData_pg_index \
-TFormData_pg_inherits \
-TFormData_pg_ipl \
-TFormData_pg_language \
-TFormData_pg_listener \
-TFormData_pg_log \
-TFormData_pg_opclass \
-TFormData_pg_operator \
-TFormData_pg_proc \
-TFormData_pg_relcheck \
-TFormData_pg_rewrite \
-TFormData_pg_sequence \
-TFormData_pg_statistic \
-TFormData_pg_trigger \
-TFormData_pg_user \
-TFormData_pg_variable \
-TFormData_pg_version \
-TForm_pg_aggregate \
-TForm_pg_am \
-TForm_pg_amop \
-TForm_pg_amproc \
-TForm_pg_attrdef \
-TForm_pg_class \
-TForm_pg_database \
-TForm_pg_group \
-TForm_pg_ipl \
-TForm_pg_language \
-TForm_pg_log \
-TForm_pg_opclass \
-TForm_pg_proc \
-TForm_pg_relcheck \
-TForm_pg_rewrite \
-TForm_pg_statistic \
-TForm_pg_trigger \
-TForm_pg_user \
-TForm_pg_variable \
-TFunc \
-TFuncCall \
-TFuncIndexInfo \
-TFuncIndexInfoPtr \
-TFuncInfo \
-TFunction \
-TFunctionCache \
-TFunctionCachePtr \
-TGISTENTRY \
-TGISTPageOpaque \
-TGISTPageOpaqueData \
-TGISTSTACK \
-TGISTSTATE \
-TGISTScanList \
-TGISTScanListData \
-TGISTScanOpaque \
-TGISTScanOpaqueData \
-TGIST_SPLITVEC \
-TGene \
-TGlobalMemory \
-TGroup \
-TGroupBuffer \
-TGroupClause \
-TGroupState \
-THASHACTION \
-THASHCTL \
-THHDR \
-THISTORY_STATE \
-THIST_ENTRY \
-THInfo \
-THTAB \
-THash \
-THashBucket \
-THashBucketData \
-THashItem \
-THashItemData \
-THashJoin \
-THashJoinState \
-THashJoinTable \
-THashMetaPage \
-THashMetaPageData \
-THashPageOpaque \
-THashPageOpaqueData \
-THashPath \
-THashScanList \
-THashScanListData \
-THashScanOpaque \
-THashScanOpaqueData \
-THashState \
-THashTableData \
-THashtFunc \
-THeapAccessStatistics \
-THeapAccessStatisticsData \
-THeapMemoryBlock \
-THeapMemoryBlockData \
-THeapScanDesc \
-THeapScanDescData \
-THeapTuple \
-THeapTupleData \
-TINTRANGE \
-TIPCKey \
-TIdList \
-TIdent \
-TIdxInfoRetval \
-TIndDesc \
-TIndInfo \
-TIndex \
-TIndexAttributeBitMap \
-TIndexAttributeBitMapData \
-TIndexElem \
-TIndexInfo \
-TIndexList \
-TIndexPath \
-TIndexScan \
-TIndexScanDesc \
-TIndexScanDescData \
-TIndexScanDescPtr \
-TIndexScanState \
-TIndexStmt \
-TIndexStrategy \
-TIndexStrategyData \
-TIndexTuple \
-TIndexTupleData \
-TIndexTupleForm \
-TInhInfo \
-TInhPaths \
-TInheritsTupleForm \
-TInsertIndexResult \
-TInsertIndexResultData \
-TInsertStmt \
-TIntArray \
-TInt_yy_size_t \
-TInt_yy_state_type \
-TInvalidationEntry \
-TInvalidationEntryData \
-TInvalidationMessage \
-TInvalidationMessageData \
-TInvalidationUserData \
-TIpcMemoryId \
-TIpcMemoryKey \
-TIpcSemaphoreId \
-TIpcSemaphoreKey \
-TItem \
-TItemId \
-TItemIdData \
-TItemIdFlags \
-TItemLength \
-TItemOffset \
-TItemPointer \
-TItemPointerData \
-TIter \
-TJInfo \
-TJoin \
-TJoinKey \
-TJoinMethod \
-TJoinPath \
-TJoinState \
-TJunkFilter \
-TKEYMAP_ENTRY \
-TKEYMAP_ENTRY_ARRAY \
-TKeymap \
-TLINE \
-TLOCK \
-TLOCKCTL \
-TLOCKT \
-TLOCKTAB \
-TLOCKTAG \
-TLOCK_TYPE \
-TLRelId \
-TLSEG \
-TLargeObjectDesc \
-TLeftistContext \
-TLeftistContextData \
-TLibCCopyLength \
-TList \
-TListenStmt \
-TLoadStmt \
-TLocalInvalid \
-TLocationIndex \
-TLockInfo \
-TLockInfoData \
-TLockTableId \
-TLogRelationContents \
-TLogRelationContentsData \
-TLookupEnt \
-TMASK \
-TMInfo \
-TMarkData \
-TMaterial \
-TMaterialState \
-TMdfdVec \
-TMemoryContext \
-TMemoryContextMethods \
-TMergeJoin \
-TMergeJoinState \
-TMergeOrder \
-TMergePath \
-TMsgType \
-TName \
-TNameData \
-TNestLoop \
-TNestLoopState \
-TNode \
-TNodeTag \
-TNotifyStmt \
-TOffset \
-TOffsetNumber \
-TOid \
-TOidInt2 \
-TOidInt2Data \
-TOidInt4 \
-TOidInt4Data \
-TOidName \
-TOidNameData \
-TOpType \
-TOpaque \
-TOpaqueData \
-TOper \
-TOperator \
-TOperatorTupleForm \
-TOprInfo \
-TOrderKey \
-TOrderType \
-TOrderedElem \
-TOrderedElemData \
-TOrderedSet \
-TOrderedSetData \
-TOverflowPageAddress \
-TOverflowTuple \
-TOverflowTupleData \
-TPATH \
-TPG_LOCK_LEVEL \
-TPGconn \
-TPGlobjfuncs \
-TPGnotify \
-TPGresAttDesc \
-TPGresAttValue \
-TPGresult \
-TPOLYGON \
-TPQArgBlock \
-TPQNotifyList \
-TPQconninfoOption \
-TPQprintOpt \
-TPROC \
-TPROC_HDR \
-TPROC_QUEUE \
-TPacket \
-TPacketLen \
-TPacketState \
-TPage \
-TPageHeader \
-TPageHeaderData \
-TPageManagerMode \
-TPageOffset \
-TParam \
-TParamExecData \
-TParamListInfo \
-TParamListInfoData \
-TParamNo \
-TParamString \
-TParseState \
-TPasswordPacket \
-TPasswordPacketV0 \
-TPath \
-TPathOrder \
-TPlan \
-TPoint \
-TPointer \
-TPool \
-TPort \
-TPortal \
-TPortalBlock \
-TPortalBlockData \
-TPortalBuffer \
-TPortalD \
-TPortalEntry \
-TPortalHashEnt \
-TPortalHeapMemory \
-TPortalMemoryContext \
-TPortalVariableMemory \
-TPredInfo \
-TPrivateMem \
-TProcState \
-TProcedureStmt \
-TProcessingMode \
-TProjectionInfo \
-TProtocolVersion \
-TPsortstate \
-TPsqlSettings \
-TQuery \
-TQueryDesc \
-TQueryTreeList \
-TREAD_ROUTINE \
-TRTSTACK \
-TRTSTATE \
-TRTScanList \
-TRTScanListData \
-TRTreePageOpaque \
-TRTreePageOpaqueData \
-TRTreeScanOpaque \
-TRTreeScanOpaqueData \
-TRangeTblEntry \
-TRangeVar \
-TRecipeStmt \
-TRegProcedure \
-TRel \
-TRelExpr \
-TRelIdCacheEnt \
-TRelNameCacheEnt \
-TRelation \
-TRelationBuildDescInfo \
-TRelationData \
-TRelationInfo \
-TRelationInvalidationData \
-TRelationList \
-TRelationPtr \
-TRelativeAddr \
-TRelativeTime \
-TRelid \
-TRemoveAggrStmt \
-TRemoveFuncStmt \
-TRemoveOperStmt \
-TRemoveStmt \
-TRenameStmt \
-TResTarget \
-TResdom \
-TResult \
-TResultState \
-TRetrieveIndexResult \
-TRetrieveIndexResultData \
-TRewriteInfo \
-TRewriteRule \
-TRuleLock \
-TRuleStmt \
-TSEGMENT \
-TSEG_OFFSET \
-TSEMA \
-TSHMEM_OFFSET \
-TSHM_QUEUE \
-TSISeg \
-TSISegEntry \
-TSISegOffsets \
-TSLock \
-TSPINLOCK \
-TSPITupleTable \
-TSPLITVEC \
-TScan \
-TScanDirection \
-TScanFunc \
-TScanKey \
-TScanKeyData \
-TScanKeyword \
-TSelectStmt \
-TSeqScan \
-TSeqTable \
-TSeqTableData \
-TSequenceTupleForm \
-TSharedInvalid \
-TSharedInvalidData \
-TSize \
-TSockAddr \
-TSort \
-TSortClause \
-TSortGroupBy \
-TSortState \
-TSplitNumber \
-TStartupPacket \
-TStrategyEvaluation \
-TStrategyEvaluationData \
-TStrategyExpression \
-TStrategyExpressionData \
-TStrategyMap \
-TStrategyMapData \
-TStrategyNumber \
-TStrategyOperator \
-TStrategyOperatorData \
-TStrategyTerm \
-TStrategyTermData \
-TStrategyTransformMap \
-TStrategyTransformMapData \
-TStream \
-TStreamPtr \
-TStringInfo \
-TStringInfoData \
-TSubLink \
-TSubLinkType \
-TSubPlan \
-TSuperQE \
-TSystemPortAddress \
-TTUPLE \
-TTXTRANGE \
-TTableID \
-TTableInfo \
-TTargetEntry \
-TTee \
-TTeeState \
-TTemp \
-TTempRelList \
-TTimeADT \
-TTimeInterval \
-TTimeIntervalData \
-TTimeSpan \
-TTransactionId \
-TTransactionState \
-TTransactionStateData \
-TTransactionStmt \
-TTrigger \
-TTriggerData \
-TTriggerDesc \
-TTriggerEvent \
-TTupleBlock \
-TTupleConstr \
-TTupleDesc \
-TTupleTable \
-TTupleTableData \
-TTupleTableSlot \
-TType \
-TTypeBlock \
-TTypeInfo \
-TTypeName \
-TTypeTupleForm \
-TTypeTupleFormData \
-TUNDO_LIST \
-TUnique \
-TUniqueState \
-TUpdateStmt \
-TUserAuth \
-TVAttList \
-TVAttListData \
-TVFunction \
-TVPageDescr \
-TVPageDescrData \
-TVPageList \
-TVPageListData \
-TVRelList \
-TVRelListData \
-TVRelStats \
-TVacAttrStats \
-TVacuumStmt \
-TValue \
-TVar \
-TVariableRelationContents \
-TVariableRelationContentsData \
-TVariableResetStmt \
-TVariableSetStmt \
-TVariableShowStmt \
-TVersionStmt \
-TVersionTupleForm \
-TVfd \
-TViewStmt \
-TXIDLookupEnt \
-TXIDTAG \
-TXidStatus \
-TYYSTYPE \
-TYY_BUFFER_STATE \
-TYY_CHAR \
-T_LockId_ \
-T_RuneEntry \
-T_RuneLocale \
-T_RuneRange \
-T_SPI_connection \
-T_SPI_plan \
-Taclitem \
-Tbits16 \
-Tbits32 \
-Tbits8 \
-Tbool \
-Tbool16 \
-Tbool32 \
-Tbool8 \
-Tbytea \
-Tcaddr_t \
-Tcat_t \
-Tcc_t \
-Tchar \
-TcharPP \
-Tclock_t \
-Tclockid_t \
-Tcset \
-Tdaddr_t \
-Tdatetkn \
-Tdev_t \
-Tdhalloc_ptr \
-Tdiv_t \
-Tdouble \
-Texecution_state \
-Tf_smgr \
-Tfd_mask \
-Tfd_set \
-Tfixpt_t \
-Tfloat \
-Tfloat32 \
-Tfloat32data \
-Tfloat4 \
-Tfloat64 \
-Tfloat64data \
-Tfloat8 \
-Tfpos_t \
-Tfunc_ptr \
-Tgid_t \
-Thashnode \
-Tino_t \
-Tint \
-Tint16 \
-Tint16_t \
-Tint16m_t \
-Tint2 \
-Tint28 \
-Tint32 \
-Tint32_t \
-Tint32m_t \
-Tint4 \
-Tint64_t \
-Tint64m_t \
-Tint8 \
-Tint8_t \
-Tint8m_t \
-TintP \
-Tjmp_buf \
-Tkey_t \
-Tldiv_t \
-Tmode_t \
-Tnlink_t \
-Toff_t \
-Toid8 \
-Tpg_pwd \
-Tpid_t \
-Tpqbool \
-Tpqsigfunc \
-Tptrdiff_t \
-Tqaddr_t \
-Tquad_t \
-Tregex_t \
-Tregister_t \
-Tregmatch_t \
-Tregoff_t \
-Tregproc \
-Trune_t \
-Tsegsz_t \
-Tsequence_magic \
-Tsig_atomic_t \
-Tsig_func \
-Tsig_t \
-Tsigjmp_buf \
-Tsigset_t \
-Tsize_t \
-Tslock_t \
-Tsmgrid \
-Tsop \
-Tsopno \
-Tspeed_t \
-Tssize_t \
-Tswblk_t \
-Ttcflag_t \
-Ttcp_seq \
-Ttext \
-Ttime_t \
-Tu_char \
-Tu_int \
-Tu_int16_t \
-Tu_int16m_t \
-Tu_int32_t \
-Tu_int32m_t \
-Tu_int64_t \
-Tu_int64m_t \
-Tu_int8_t \
-Tu_int8m_t \
-Tu_long \
-Tu_quad_t \
-Tu_short \
-Tuch \
-Tuid_t \
-Tuint \
-Tuint16 \
-Tuint32 \
-Tuint8 \
-Tushort \
-Tva_list \
-Tvm_offset_t \
-Tvm_size_t \
-Tvoid \
-Twchar_t \
-Tword16 \
-Tword32 \
-Tword8 \
-Tyy_size_t \
-Tyy_state_type \
/tmp/$$a >/tmp/$$ 2>&1
	if [ "$?" -ne 0 -o -s /tmp/$$ ]
	then	echo "$FILE"
		cat /tmp/$$
	fi
	cat /tmp/$$a |
	sed 's;^/\*\(DATA(.*\)\*/$;\1;' |
	sed 's;/\*---X_X;/* ---;g' |
	sed 's;^static[ 	][ 	]*;static ;g' | # workaround indent bug
	sed 's;^}[ 	][ 	]*/\*;}   /*;' |
	detab -t8 -qc |
	entab -t4 -qc |
	sed 's;^\([A-Za-z_][^	]*\)[ 	][ 	]*\*$;\1 *;' | # move trailing * in function return type
	cat >/tmp/$$ && cat /tmp/$$ >$FILE
done

# The 'for' loop makes these backup files useless
# so delete them
rm -f *a.BAK
