#!/bin/bash
id=$1
declare -A sumTable=([A]=1 [B]=0 [C]=9 [D]=8 [E]=7 [F]=6 [G]=5 [H]=4 [I]=9 [J]=3 [K]=2 [L]=2 [M]=1 [N]=0 [O]=8 [P]=9 [Q]=8 [R]=7 [S]=6 [T]=5 [U]=4 [V]=3 [W]=1 [X]=3 [Z]=0)
if [[ $id =~ ^[A-Z][0-9]{8} ]]; then
no0=`echo $id |head -c1 |tail -c1
no1=`echo $id |head -c2 |tail -c1
no2=`echo $id |head -c3 |tail -c1
no3=`echo $id |head -c4 |tail -c1
no4=`echo $id |head -c5 |tail -c1
no5=`echo $id |head -c6 |tail -c1
no6=`echo $id |head -c7 |tail -c1
no7=`echo $id |head -c8 |tail -c1
no8=`echo $id |head -c9 |tail -c1
check=$((${sumTable[$no0]} + $no1*8 + $no2*7 + $no3*6 + $no4*5 + $no5*4 + $no6*3 + $no7*2 + $no8))
no9=$(((400 - $check) % 10))
id=$id$no9
else
echo "FORMAT ERROR: $id" >&2
exit
fi
if [[ -f "ad-data-$no0$no1/$id" ]]; then
echo 'File exist'
exit
fi
curl "
https://esa.ntpc.edu.tw/path" \
-d "idno=$id" \
2> /dev/null \
|head -c -1 \
|tail -c +2 \
|jq . \
|grep -v '"birthday": "' \
|grep -v '"birthday1": "'\
|grep -v '"AD": ' \
|tee -a ad-data-$no0$no1-all \
|tee ad-data-$no0$no1/$id