#!/bin/bash

export TEMP_JAVA_NAME=Xyz
export TEMP_JAVA_FILE=/tmp/$TEMP_JAVA_NAME.java
export TEMP_CLASS_FILE=/tmp/$TEMP_JAVA_NAME.class
export TARGET_FILE=/tmp/class_buf.rs

function compile_java() {
    echo "package rust;"                                         >  $TEMP_JAVA_FILE
    echo ""                                                      >> $TEMP_JAVA_FILE
    echo "public class $TEMP_JAVA_NAME {"                        >> $TEMP_JAVA_FILE
    echo "    public static void hello() {"                      >> $TEMP_JAVA_FILE
    echo "        System.out.println(\"Hello, World!\");"        >> $TEMP_JAVA_FILE
    echo "    }"                                                 >> $TEMP_JAVA_FILE
    echo ""                                                      >> $TEMP_JAVA_FILE
    echo "    public static int calc(int a, int b) {"            >> $TEMP_JAVA_FILE
    echo "        return a + b;"                                 >> $TEMP_JAVA_FILE
    echo "    }"                                                 >> $TEMP_JAVA_FILE
    echo "}"                                                     >> $TEMP_JAVA_FILE
    
    work_dir=$(pwd)
    cd /tmp
    javac $TEMP_JAVA_NAME.java
    cd $work_dir
}

function dump_bytes() {
    class_size=$(ls -l $TEMP_CLASS_FILE | cut -d" " -f 5)
    echo "// $TEMP_JAVA_FILE"                                                                 >> $TARGET_FILE
    echo "#[allow(non_upper_case_globals, non_snake_case)]"                                   >> $TARGET_FILE
    echo "pub(crate) const CLASS_NAME_$TEMP_JAVA_NAME: &str = \"rust/$TEMP_JAVA_NAME\";"      >> $TARGET_FILE
    echo "#[allow(non_upper_case_globals, non_snake_case)]"                                   >> $TARGET_FILE
    echo "pub(crate) const CLASS_$TEMP_JAVA_NAME: [u8; $class_size] = ["                      >> $TARGET_FILE
    hexdump -C $TEMP_CLASS_FILE                                                               | \
        sed "/^.\{8\}$/d"                                                                     | \
        sed "s/^.\{10\}//g"                                                                   | \
        sed "s/  |.*$//g"                                                                     | \
        sed "s/  / /g"                                                                        | \
        sed "s/ \{2,\}//g"                                                                    | \
        sed "s/ /,0x/g"                                                                       | \
        sed "s/^/0x/g"                                                                        | \
        sed "s/,/, /g"                                                                        | \
        sed "s/$/,/g"                                                                         | \
        sed "s/^/    /g"                                                                      >> $TARGET_FILE
    echo "];"                                                                                 >> $TARGET_FILE
}

rm -rf $TARGET_FILE

echo "/// Auto generated by java2u8vec.sh"                           >  $TARGET_FILE
echo "/// Do not edit by hands"                                      >> $TARGET_FILE
echo "/// Java class bytecode, JDK8"                                 >> $TARGET_FILE
echo ""                                                              >> $TARGET_FILE

compile_java
dump_bytes

cp $TARGET_FILE sample/src/samples/s008_class_buf.rs
