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
	echo_func "$FUNCNAME" "$@"
12
	before_import_cmds
13
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
14
	after_import_cmds
15
}
16

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

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

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

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

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

    
50
input_make ()
51
{
52
	echo_func "$FUNCNAME" "$@"
53
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
54
}
55

    
56
map_table ()
57
{
58
	echo_func "$FUNCNAME" "$@"
59
	input_make map.csv
60
	psql <<EOF
61
SELECT util.reset_map_table('pg_temp.map');
62
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
63
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
64
EOF
65
}
66

    
67
mk_derived ()
68
{
69
	echo_func "$FUNCNAME" "$@"
70
	"$root_dir"/schemas/VegCore/mk_derived
71
}
72

    
73
remake_VegBIEN_mappings ()
74
{
75
	echo_func "$FUNCNAME" "$@"
76
	echo_run rm header.csv map.csv # remake them
77
	yes|input_make test
78
}
79

    
80
postprocess () # overridable
81
{
82
	echo_func "$FUNCNAME" "$@"
83
	local file="$top_dir"/postprocess.sql
84
	if test -e "$file"; then psql "$@"; fi
85
}
86

    
87
import ()
88
{
89
	echo_func "$FUNCNAME" "$@"
90
	map_table
91
	postprocess
92
	mk_derived
93
}
94

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