#! /bin/bash
PGPASSFILE=".pgpass"
export PGPASSFILE
TMPFILE=`mktemp itemsite.XXXXXXXXX --tmpdir=/tmp`
DUMPFILE="${TMPFILE}"
SQLFILE="${TMPFILE}.sql"
data/pg_dump -U admin -h localhost -p 5432 --data-only --column-inserts --table=itemsite --file=${TMPFILE} pelican
echo "BEGIN TRANSACTION;" > $SQLFILE
sed -e 's/^SET/-- SET/' -e 's/ false,/ 0,/g' -e 's/ false)/ 0)/' -e 's/ true,/ 1,/g' -e 's/ true)/ 1)/' < $TMPFILE >> $SQLFILE
echo "COMMIT TRANSACTION;" >> $SQLFILE
sqlite3 data/xtuple.db <<EOF
drop table if exists itemsite;
CREATE TABLE itemsite (
    itemsite_id integer NOT NULL,
    itemsite_item_id integer NOT NULL,
    itemsite_warehous_id integer,
    itemsite_qtyonhand REAL NOT NULL,
    itemsite_reorderlevel REAL NOT NULL,
    itemsite_ordertoqty REAL NOT NULL,
    itemsite_cyclecountfreq integer NOT NULL,
    itemsite_datelastcount date,
    itemsite_datelastused date,
    itemsite_loccntrl boolean NOT NULL,
    itemsite_safetystock REAL NOT NULL,
    itemsite_minordqty REAL NOT NULL,
    itemsite_multordqty REAL NOT NULL,
    itemsite_leadtime integer NOT NULL,
    itemsite_abcclass character(1),
    itemsite_issuemethod character(1),
    itemsite_controlmethod character(1),
    itemsite_active boolean NOT NULL,
    itemsite_plancode_id integer NOT NULL,
    itemsite_costcat_id integer NOT NULL,
    itemsite_eventfence integer NOT NULL,
    itemsite_sold boolean NOT NULL,
    itemsite_stocked boolean NOT NULL,
    itemsite_freeze boolean NOT NULL,
    itemsite_location_id integer NOT NULL,
    itemsite_useparams boolean NOT NULL,
    itemsite_useparamsmanual boolean NOT NULL,
    itemsite_soldranking integer DEFAULT 1,
    itemsite_createpr boolean,
    itemsite_location text,
    itemsite_location_comments text,
    itemsite_notes text,
    itemsite_perishable boolean NOT NULL,
    itemsite_nnqoh REAL DEFAULT 0 NOT NULL,
    itemsite_autoabcclass boolean NOT NULL,
    itemsite_ordergroup integer DEFAULT 0 NOT NULL,
    itemsite_disallowblankwip boolean NOT NULL,
    itemsite_maxordqty REAL DEFAULT 0.0 NOT NULL,
    itemsite_mps_timefence integer DEFAULT 0 NOT NULL,
    itemsite_createwo boolean NOT NULL,
    itemsite_warrpurc boolean NOT NULL,
    itemsite_autoreg boolean,
    itemsite_costmethod character(1) NOT NULL,
    itemsite_value REAL NOT NULL,
    itemsite_ordergroup_first boolean NOT NULL,
    itemsite_supply_itemsite_id integer,
    itemsite_planning_type character(1) NOT NULL,
    itemsite_wosupply boolean NOT NULL,
    itemsite_posupply boolean NOT NULL
);
EOF
sqlite3 data/xtuple.db < $SQLFILE
