#!/bin/bash

set -e

d=$(mktemp -d)
cd "$d"

function cleanup {
  rm -r "$d"
}
trap cleanup EXIT

(
    function run {
        echo "$@"
        python -c "$@"
    }

    run 'from skyfield.api import load; load.timescale(); print(load.log)'
    ls -CF
    echo
    run 'from skyfield.api import load; load.timescale(builtin=False); print(load.log)'
    ls -CF
    echo
    run 'from skyfield.api import load; load.timescale(builtin=False); print(load.log)'
    ls -CF
    echo
    run 'from skyfield.api import load; load("de424s.bsp"); print(load.log)'
    ls -CF
    echo
    run 'from skyfield.api import load; load("de424s.bsp"); print(load.log)'
    ls -CF
    echo
    echo > de424s.bsp
    wc de424s.bsp
    run 'from skyfield.api import load; load("de424s.bsp", reload=True); print(load.log)'
    ls -CF
    wc de424s.bsp
    echo
    run 'from skyfield.api import load; load("de424s.bsp", reload=True, backup=True); print(load.log)'
    ls -CF

) | sed 's/\t/  /g' > actual_output

cat > expected_output <<-'EOF'
from skyfield.api import load; load.timescale(); print(load.log)
deltat.data
  Parsing builtin file with parse_deltat_data()
deltat.preds
  Parsing builtin file with parse_deltat_preds()
Leap_Second.dat
  Parsing builtin file with parse_leap_seconds()
actual_output

from skyfield.api import load; load.timescale(builtin=False); print(load.log)
./deltat.data
  Downloading ftp://cddis.nasa.gov/products/iers/deltat.data
  Parsing with parse_deltat_data()
./deltat.preds
  Downloading ftp://cddis.nasa.gov/products/iers/deltat.preds
  Parsing with parse_deltat_preds()
./Leap_Second.dat
  Downloading https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat
  Parsing with parse_leap_seconds()
Leap_Second.dat  actual_output  deltat.data  deltat.preds

from skyfield.api import load; load.timescale(builtin=False); print(load.log)
./deltat.data
  File already exists
  Parsing with parse_deltat_data()
./deltat.preds
  File already exists
  Parsing with parse_deltat_preds()
./Leap_Second.dat
  File already exists
  Parsing with parse_leap_seconds()
Leap_Second.dat  actual_output  deltat.data  deltat.preds

from skyfield.api import load; load("de424s.bsp"); print(load.log)
./de424s.bsp
  Downloading ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp/de424s.bsp
  Opening with SpiceKernel
Leap_Second.dat  actual_output  de424s.bsp  deltat.data  deltat.preds

from skyfield.api import load; load("de424s.bsp"); print(load.log)
./de424s.bsp
  File already exists
  Opening with SpiceKernel
Leap_Second.dat  actual_output  de424s.bsp  deltat.data  deltat.preds

1 0 1 de424s.bsp
from skyfield.api import load; load("de424s.bsp", reload=True); print(load.log)
./de424s.bsp
  File already exists
  Downloading ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp/de424s.bsp
  Opening with SpiceKernel
Leap_Second.dat  actual_output  de424s.bsp  deltat.data  deltat.preds
  20470  121070 6547456 de424s.bsp

from skyfield.api import load; load("de424s.bsp", reload=True, backup=True); print(load.log)
./de424s.bsp
  File already exists
  Downloading ftp://ssd.jpl.nasa.gov/pub/eph/planets/bsp/de424s.bsp
  Opening with SpiceKernel
Leap_Second.dat  de424s.bsp    deltat.data
actual_output   de424s.old1.bsp  deltat.preds
EOF

colordiff -u expected_output actual_output
