#!/bin/bash
# Apply patches and run scripts to do bulk data transformation.

CATALOG_HEADERS="pg_proc.h pg_type.h pg_attribute.h pg_class.h pg_attrdef.h pg_constraint.h pg_inherits.h pg_index.h pg_operator.h pg_opfamily.h pg_opclass.h pg_am.h pg_amop.h pg_amproc.h pg_language.h pg_largeobject_metadata.h pg_largeobject.h pg_aggregate.h pg_statistic_ext.h pg_statistic.h pg_rewrite.h pg_trigger.h pg_event_trigger.h pg_description.h pg_cast.h pg_enum.h pg_namespace.h pg_conversion.h pg_depend.h pg_database.h pg_db_role_setting.h pg_tablespace.h pg_pltemplate.h pg_authid.h pg_auth_members.h pg_shdepend.h pg_shdescription.h pg_ts_config.h pg_ts_config_map.h pg_ts_dict.h pg_ts_parser.h pg_ts_template.h pg_extension.h pg_foreign_data_wrapper.h pg_foreign_server.h pg_user_mapping.h pg_foreign_table.h pg_policy.h pg_replication_origin.h pg_default_acl.h pg_init_privs.h pg_seclabel.h pg_shseclabel.h pg_collation.h pg_partitioned_table.h pg_range.h pg_transform.h pg_sequence.h pg_publication.h pg_publication_rel.h pg_subscription.h pg_subscription_rel.h"

PATCH_DIR=/path/to/patchset
REPO_DIR=/path/to/postgresql

cd $REPO_DIR

# Infrastructure
echo
git am $PATCH_DIR/*0001*.patch

# Convert DATA() and DESCR() statements to .dat files
pushd $REPO_DIR/src/include/catalog

echo
echo "Converting header DATA() to .dat files"
perl $PATCH_DIR/*convert_header2dat.pl $CATALOG_HEADERS

echo
echo "Rewriting raw .dat files to standard format"
perl -I ../../backend/catalog  rewrite_dat.pl  *.dat

echo
git add *.dat
git commit -m"Mechanical data conversion" \
-m'Perform the conversion. The data has been stripped of all double-quotes
for readability, since the Perl hash literals have single quotes everywhere.
Later commits restore them where needed.'

rm *.bak
popd

# Hand edits of data files
echo
git am $PATCH_DIR/*0002*.patch

# Update catalog scripts
echo
git am $PATCH_DIR/*0003*.patch

# Remove DATA() and DESCR() lines
pushd $REPO_DIR/src/include/catalog

for f in $CATALOG_HEADERS;
do
sed -i '/^\(DATA\|DESCR\|SHDESCR\)/d' $f;
done;

popd

git commit -am"Mechanically remove DATA() and DESCR() lines from headers"

# Clean up previous operation
echo
git am $PATCH_DIR/*0004*.patch

# Move symbols to generated definition headers
echo
git am  $PATCH_DIR/*0005*.patch

