#!/bin/bash

# March 8, 2026

rm -f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12
rm -f mumps.sqlite
rm -f tmp tmp1 tmp2
rm -f MumpsView.sql

echo "You must use the sqlite database option for this"

# Recreate sqlite3 database
echo "Initialize sqlite3 database"
echo "y" | mumps-sql-db-create

# Generate random data and sql stmts to load it.
# The order of the following is important.

randata.mps

insurance.mps > a1  
patientid.mps 100 >a2 
insbill.mps > a12
probtab.mps > a3 
labtab.mps > a6 
medtab.mps > a8
phys.mps > a9 
probs.mps 100 > a4 
labs.mps > a5 
medadmin.mps > a7
vit.mps > a10 
officevisit.mps > a11 

# Recreate sqlite3 database
echo "Initialize sqlite3 database"
echo "y" | mumps-sql-db-create

echo
echo "Load sql database"
echo

# Load generated data into sql tables

sqlite3 mumps.sqlite < a1 
sqlite3 mumps.sqlite < a2 
sqlite3 mumps.sqlite < a3 
sqlite3 mumps.sqlite < a4 
sqlite3 mumps.sqlite < a5 
sqlite3 mumps.sqlite < a6 
sqlite3 mumps.sqlite < a7 
sqlite3 mumps.sqlite < a8 
sqlite3 mumps.sqlite < a9 
sqlite3 mumps.sqlite < a10 
sqlite3 mumps.sqlite < a11 
sqlite3 mumps.sqlite < a12 

# rm a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12

echo
echo "Parse schema"
echo

# Parse the relational schema and generate select stmts
# and create a mumps table from them.

parse_schema.mps 

# Create SQL views of the mumps table

echo
echo "Create SQL views from Mumps table"
echo

MumpsToSQlViews.mps 

echo 
echo "Final Display sqlite3 schema"
echo

sqlite3 mumps.sqlite ".schema"
