Project

General

Profile

1
#!/bin/bash
2

    
3
# Bash script to create a new postgis database and prep it with GADM
4
# data for geovalidation purposes.
5
#
6
# Basic workflow:
7
#  1. Load geoscrub input data into database
8
#  2. Scrub geoscrub input data with the geonames.sql script
9
#  3. Scrub geoscrub input data with the geovalidate.sql script
10
# 
11
# Paul Sarando
12
# iPlant Collaborative
13
# Oct 2013
14

    
15
DB_NAME="geoscrub"
16
DB_USER="bien"
17
DB_HOST_OPT=""
18
SCRIPT_DIR="$(dirname $0)"
19

    
20
function usage {
21
    echo "Usage: $0 [OPTIONS]" >&2
22
    echo "Valid Options:" >&2
23
    echo "-d, --dbname=DBNAME      database name psql commands will connect to" >&2
24
    echo "-h, --host=HOSTNAME      database server host or socket directory" >&2
25
    echo "-U, --username=USERNAME  database user name" >&2
26
    exit 1;
27
}
28

    
29
while [[ $# -gt 0  ]]; do
30
    case "$1" in
31
        -\? | --help)
32
            usage
33
            ;;
34
        -h)
35
            if [[ -z $2  ]];  then
36
                echo "Option $1 requires an argument." >&2
37
                usage
38
            fi
39
            DB_HOST_OPT="-h $2"
40
            shift 2
41
            ;;
42
        --host=*)
43
            DB_HOST_OPT="-h ${1#*=}"
44
            shift
45
            ;;
46
        -U)
47
            if [[ -z $2  ]];  then
48
                echo "Option $1 requires an argument." >&2
49
                usage
50
            fi
51
            DB_USER="$2"
52
            shift 2
53
            ;;
54
        --username=*)
55
            DB_USER="${1#*=}"
56
            shift
57
            ;;
58
        -d)
59
            if [[ -z $2  ]];  then
60
                echo "Option $1 requires an argument." >&2
61
                usage
62
            fi
63
            DB_NAME="$2"
64
            shift 2
65
            ;;
66
        --dbname=*)
67
            DB_NAME="${1#*=}"
68
            shift
69
            ;;
70
        *)
71
            echo "Invalid option: $1" >&2
72
            usage
73
            ;;
74
    esac
75
done
76

    
77
function run_sql_script {
78
    local SCRIPT=$1
79

    
80
    psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" --set ON_ERROR_STOP=1 < "$SCRIPT"
81
    if [[ $? != 0 ]]; then
82
        echo "Error while executing SQL script ${SCRIPT}"
83
        exit 1
84
    fi
85
}
86

    
87
"${SCRIPT_DIR}"/load-geoscrub-input.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
88
if [[ $? != 0 ]]; then
89
    echo "Could not load ${DB_NAME} database with geonames.org data."
90
    exit 1
91
fi
92

    
93
echo "Scrubbing input with geonames data..."
94
run_sql_script "${SCRIPT_DIR}/geonames.sql"
95

    
96
echo "Scrubbing input with geovalidate data..."
97
run_sql_script "${SCRIPT_DIR}/geovalidate.sql"
98

    
99
echo "Input successfully scrubbed."
100
echo "Scrubbed input available in the geoscrub table of the ${DB_NAME} database."
101

    
(8-8/26)