Project

General

Profile

1
#!/bin/bash -e
2

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

    
7
import ()
8
{
9
	echo_func "$FUNCNAME" "$@"
10
	before_import_cmds
11
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
12
	after_import_cmds
13
}
14
fi ####
15

    
16
. "$(dirname "${BASH_SOURCE[0]}")"/import.run
17

    
18
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
19
bin_dir="$root_dir"/bin
20

    
21
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
22
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
23
export schema table
24

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

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

    
52
map_table ()
53
{
54
	echo_func "$FUNCNAME" "$@"
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 ()
64
{
65
	echo_func "$FUNCNAME" "$@"
66
	"$root_dir"/schemas/VegCore/mk_derived
67
}
68

    
69
remake_VegBIEN_mappings ()
70
{
71
	echo_func "$FUNCNAME" "$@"
72
	echo_run rm header.csv map.csv # remake them
73
	yes|input_make test
74
}
75

    
76
postprocess () # overridable
77
{
78
	echo_func "$FUNCNAME" "$@"
79
	local file="$top_dir"/postprocess.sql
80
	if test -e "$file"; then psql "$@"; fi
81
}
82

    
83
import ()
84
{
85
	echo_func "$FUNCNAME" "$@"
86
	map_table
87
	postprocess
88
	mk_derived
89
}
(40-40/51)