summaryrefslogtreecommitdiff
path: root/scripts/run.sh
blob: 463740bba518994d2fccc61038b359b462d553b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
# Input a number and the script picks the right data and runs the
# executable to produce the HTML web page, and then opens the page
# with the default web browser.
#
#   scripts/run.sh SYMBOL
#
# The SYMBOL does not have to contain TW or TWO; the script finds the
# correct one.  If both options exist, do both.
#
# Extra options are passed on to the analyser, e.g.
#   scripts/run.sh 2330 --interval week
#
# Environment: DATA_DIR (default data/tw), HTML_DIR (default data/html), OPEN_CMD (default `open`
# on macOS, `xdg-open` elsewhere). The page is written to $HTML_DIR/CODE.TW.html (or .TWO.html).
set -u
cd "$(dirname "$0")/.." || exit 1          # project root, so it works from any directory
data=${DATA_DIR:-data/tw}; htmldir=${HTML_DIR:-data/html}

[ $# -ge 1 ] || { sed -n '2,15p' "$0"; exit 2; }
sym=$1; shift

# An explicit suffix (2330.TW / 6488.TWO) selects that market only; otherwise try both.
case $sym in
  *.[Tt][Ww][Oo]) code=${sym%.*}; candidates=("$data/$code.TWO.csv");;
  *.[Tt][Ww])     code=${sym%.*}; candidates=("$data/$code.TW.csv");;
  *)              code=$sym;      candidates=("$data/$code.TW.csv" "$data/$code.TWO.csv");;
esac

files=(); for f in "${candidates[@]}"; do [ -s "$f" ] && files+=("$f"); done
if [ ${#files[@]} -eq 0 ]; then
  echo "no data for '$sym' in $data (looked for: ${candidates[*]})" >&2
  echo "download it first, e.g.: scripts/fetch.sh -d $data 2010-01-01 \$(date +%F) $code" >&2
  exit 1
fi

cargo build --release -q || { echo "build failed" >&2; exit 1; }
bin=target/release/stock
mkdir -p "$htmldir"

pages=()
for f in "${files[@]}"; do
  page="$htmldir/$(basename "$f" .csv).html"
  echo "== $(basename "$f" .csv)"
  "$bin" "$f" -o "$page" "$@" || { echo "analysis failed for $f" >&2; exit 1; }
  pages+=("$page")
done

if [ -n "${OPEN_CMD:-}" ]; then opener=$OPEN_CMD
elif command -v open >/dev/null; then opener=open
elif command -v xdg-open >/dev/null; then opener=xdg-open
else echo "no program to open pages with; open them yourself: ${pages[*]}"; exit 0; fi
for page in "${pages[@]}"; do $opener "$page"; done