Project

General

Profile

1
#!/bin/bash
2
set -o errexit
3

    
4
if false; then #### run script template:
5
#!/bin/bash
6
set -o errexit
7
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
8

    
9
import ()
10
{
11
	before_import_cmds
12
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
13
	after_import_cmds
14
}
15

    
16
run_cmd "$@"
17
fi ####
18

    
19
. "$(dirname "${BASH_SOURCE[0]}")"/util.run
20

    
21
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
22
bin_dir="$root_dir"/bin
23

    
24
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
25
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
26
export schema table
27

    
28
psql () # usage: ([file=...] [dir=...]; self)
29
{
30
	local dir="$dir"
31
	if test -n "$file"; then
32
		set -- --file "$file" "$@"
33
		: "${dir:=$(dirname "$file")}"
34
	fi
35
	: "${dir:=$top_dir}"
36
	
37
	(cat <<EOF
38
\cd $dir
39
\set schema "$schema"
40
\set table "$table"
41
\set table_str '''"$table"'''
42
SET search_path TO "$schema", util;
43
EOF
44
	cat)|
45
	echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
46
}
47

    
48
input_make ()
49
{
50
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
51
}
52

    
53
map_table ()
54
{
55
	input_make map.csv
56
	psql <<EOF
57
SELECT util.reset_map_table('pg_temp.map');
58
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
59
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
60
EOF
61
}
62

    
63
mk_derived () { "$root_dir"/schemas/VegCore/mk_derived; }
64

    
65
remake_VegBIEN_mappings ()
66
{
67
	echo_run rm header.csv map.csv # remake them
68
	yes|input_make test
69
}
70

    
71
postprocess () # overridable
72
{
73
	local file="$top_dir"/postprocess.sql
74
	if test -e "$file"; then psql "$@"; fi
75
}
76

    
77
import ()
78
{
79
	echo_run map_table
80
	echo_run postprocess
81
	echo_run mk_derived
82
}
83

    
84
run_cmd "$@"
(39-39/50)