#!/usr/bin/env bash

usage() {
    echo "Usage: $0 [DatePattern]"
    echo "DatePattern: YYYYMM or YYYYM or YYYYMMD"
    exit 1
}

DatePattern="${1}"
if [ -z "${DatePattern}" ]; then
    usage
fi
FilePattern="${DatePattern}*.alpaca_sim_md.db.gz"

cd $(realpath $(dirname $0))/..
mkdir -p ./data/equity
pushd ./data/equity

Cmd="rsync -ahvv cvtt@hs01.cvtt.vpn:/works/cvtt/md_archive/equity/alpaca_md/sim/${FilePattern} ./"
echo ${Cmd}
eval ${Cmd}
# -------------------------------------

for srcfname in $(ls *.db.gz); do
    dt="${srcfname:0:8}"
    tgtfile=${dt}.mktdata.ohlcv.db
    echo "${srcfname} -> ${tgtfile}"

    Cmd="gunzip -c $srcfname > temp.db && rm $srcfname"
    echo ${Cmd}
    eval ${Cmd}
    Cmd="rm -f ${tgtfile} && sqlite3 temp.db '.dump md_1min_bars' | sqlite3  ${tgtfile}"
    echo ${Cmd}
    eval ${Cmd}
done
rm temp.db
popd
