#!/bin/bash
# https://docs.sheetjs.com/docs/demos/engines/rhino

cd /tmp
rm -rf sheetjs-java
mkdir -p sheetjs-java
cd sheetjs-java

curl -L -o rhino.jar https://repo1.maven.org/maven2/org/mozilla/rhino/1.7.15/rhino-1.7.15.jar

curl -LO https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js
curl -LO https://docs.sheetjs.com/pres.xlsx

curl -LO https://docs.sheetjs.com/rhino/SheetJSRhino.zip
unzip SheetJSRhino.zip

cat >SheetJSRhino.java <<EOF
/* sheetjs (C) 2013-present  SheetJS -- https://sheetjs.com */
/* vim: set ts=2: */
import com.sheetjs.SheetJS;
import com.sheetjs.SheetJSFile;
import com.sheetjs.SheetJSSheet;

public class SheetJSRhino {
  public static void main(String args[]) throws Exception {
    try {
      SheetJS sjs = new SheetJS();

      /* open file */
      SheetJSFile xl = sjs.read_file(args[0]);

      /* get sheetnames */
      String[] sheetnames = xl.get_sheet_names();
      System.err.println(sheetnames[0]);

      /* convert to CSV */
      SheetJSSheet sheet = xl.get_sheet(0);
      String csv = sheet.get_csv();
      System.out.println(csv);
    } catch(Exception e) { throw e; } finally { SheetJS.close(); }
  }
}
EOF

for n in 1.8 {9..23}; do
  export JAVA_HOME=`/usr/libexec/java_home -v $n`
  java -version
  find . -name \*.class | while read x; do rm $x; done

  javac -cp ".:rhino.jar" SheetJSRhino.java
  jar -cf SheetJS.jar SheetJSRhino.class com/sheetjs/*.class xlsx.full.min.js

  java -cp ".:SheetJS.jar:rhino.jar" SheetJSRhino pres.xlsx
done