# download
wget https://github.com/mrubyc/mrubyc/archive/refs/tags/release3.4.1.zip
wget https://github.com/ch32-rs/wchisp/releases/download/nightly/wchisp-linux-x64.tar.gz
#wget https://github.com/cnlohr/ch32fun/archive/refs/heads/master.zip
wget -O master.zip https://github.com/cnlohr/ch32fun/archive/c2152512a248f860dff848f4b4f89fa9c6c6f473.zip
#
# ch32fun
mkdir mrubyc_ch32fun
unzip master.zip
mv ch32fun-c2152512a248f860dff848f4b4f89fa9c6c6f473 ch32fun-master
cd ch32fun-master/minichlink
make
cd ../../
mv ch32fun-master/ch32fun/ mrubyc_ch32fun/
mv ch32fun-master/extralibs/ mrubyc_ch32fun/
# for usb
awk '/void HandleDataOut/{a=1}a&&/^}/{$0="\tpoll_input();\n"$0;a=0}/ANYPRINTF/&&!u{$2=$2" 0 &&";u=1}1' mrubyc_ch32fun/extralibs/fsusb.c >temp.tmp && mv temp.tmp mrubyc_ch32fun/extralibs/fsusb.c
awk '/void HandleDataOut/{a=1}a&&/^}/{$0="\tpoll_input();\n"$0;a=0}1' mrubyc_ch32fun/extralibs/usbd.c >temp.tmp && mv temp.tmp mrubyc_ch32fun/extralibs/usbd.c
#
# mruby/c
mkdir mrubyc_ch32fun/mrubyc
unzip release3.4.1.zip
# setting
cd mrubyc-release3.4.1/src
cat ../hal/minimal.h hal.h | awk '/define MRBC_SRC/{$0=$0"\n#include \"ch32fun.h\""}1' |sed 's/delay/Delay_Ms/' > hal.h
sed -i -e 's@#endif@#ifdef __cplusplus\n}\n#endif\n#endif@' -e 's@#define hal_init@#ifdef __cplusplus\nextern "C" {\n#endif\n#define hal_init@' hal.h
sed -i -e 's/#define MRBC_USE_FLOAT 2/#if defined(CH32X03x)\n#define MRBC_USE_FLOAT 0\n#elif defined(CH32V20x)\n#define MRBC_USE_FLOAT 1\n#else\n#define MRBC_USE_FLOAT 2\n#endif/' -e 's@// #define MRBC_REQUIRE_32BIT_ALIGNMENT@#define MRBC_REQUIRE_32BIT_ALIGNMENT@' vm_config.h
sed -i -e 's@#include "mrubyc.h"@#include "mrubyc.h"\n#include "rrt0.h"@' rrt0.c
# for float
awk '/->d/{$0="#if MRBC_USE_FLOAT\n"$0"\n#endif"}1' boxing_no.h >temp.tmp && mv temp.tmp boxing_no.h
awk -v RS="" '/MRBC_TT_FLOAT/ && !/MRBC_USE_FLOAT/{$0="#if MRBC_USE_FLOAT\n"$0"\n#endif"}/return val->d;\n\}/{$0="#if MRBC_USE_FLOAT\n  return val->d;\n#else\n  return 0;\n#endif\n}"}{$0=$0"\n"}1' value.c >temp.tmp && mv temp.tmp value.c
awk '/MRBC_CLASS\(Float/{$0="#if MRBC_USE_FLOAT\n"$0"\n#else\n  0,\n#endif"}1' class.c >temp.tmp && mv temp.tmp class.c
awk '/define MRBC_TO_INT/{$0="#if MRBC_USE_FLOAT\n"$0;a=NR}a&&NR==a+3{$0="#else\n#define MRBC_TO_INT(val) (val).i\n#endif\n"$0}1' value.h >temp.tmp && mv temp.tmp value.h
# for string
awk '/MRBC_CLASS\(String/{$0="#if MRBC_USE_STRING\n"$0"\n#else\n  0,\n#endif"}1' class.c >temp.tmp && mv temp.tmp class.c
cat c_object.c |awk '/void mrbc_object_inspect/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/static int set_sym_name_by_id/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' >temp.tmp && mv temp.tmp c_object.c
cat c_object.h |awk '/void mrbc_object_inspect/{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp c_object.h
cat error.c |awk '/static void c_exception_message/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/TT_STRING/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/}/{$0=$0"\n#endif";a=0;b=1}b&&/}$/{$0="#if MRBC_USE_STRING\n"$0"\n#endif";b=0}1' |awk '/c_exception_message /{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp error.c
cat rrt0.c |awk '/in case of Task.get\("TasName"\)/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/}/{$0=$0"\n#endif";a=0}1' |awk '/void c_task_name/||/void c_task_set_name/||/void c_task_status/||/void c_task_create/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&/^}/{$0=$0"\n#endif";a=0}1' |awk '/c_task_name_list /||/c_task_name /||/c_task_set_name /||/c_task_status /||/c_task_create /{$0="#if MRBC_USE_STRING\n"$0"\n#endif"}1' >temp.tmp && mv temp.tmp rrt0.c
# printf
cat console.c | awk "/case 'B'/{\$0=\$0\"\n  case 'o':\";print;r=4}r{r-=1;next}1" |sed 's/, 4)/, pf->fmt.type*9\/2%8)/' |awk 'a==0 && /int pad_width/{$0=$0"\n    if(pf->fmt.flag_zero && pf->fmt.precision<pad_width) pf->fmt.precision=pad_width;";a=1}1' |ruby -ple "sub(/\"0123456789ABCDEF\"\\[(.+)\\]/){\"((#\$1) < 10) ? (#\$1)+'0' : (#\$1)+'A'-10\"}" |awk "/buf\\[0\\] = '\\\\\\\\';\$/{\$0=\"//\"\$0}/void mrbc_char_to_s/{a=1}a&&/^{/{\$0=\$0\"\n  buf[0]='\\\\\\\\';\";a=0}1" >temp.tmp && mv temp.tmp console.c
sed -i -e 's/snprintf/mrbc_snprintf/' value.c
sed -i -e 's/int mrbc_printf_float/int mrbc_printf_float2/' -e 's/pf->fmt.precision &&/pf->fmt.precision > 0 \&\&/' -e 's/pf->fmt.precision != 0/pf->fmt.precision > 0/' -e 's/(struct RPrintfFormat){0};/(struct RPrintfFormat){0};\n  pf->fmt.precision=-1;/' -e "s/fstr == '\.' ) {/fstr == '.' ) {\n    pf->fmt.precision=0;/" console.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/floatformat.c >>console.c
# reduce size
cat c_object.c | awk "/case 'B'/{print;b=6}b{b-=1;next}/case 'X'/{print;x=6}x{x-=1;next}1" |sed 's/, 3)/, pf.fmt.type*9\/2%8)/' >temp.tmp && mv temp.tmp c_object.c
cat c_string.c | awk '/int is_space/{a=b=1}a && /^{/{$0="/*"$0;a=0}b && /^}/{$0=$0"*/\n{ return (8 < ch && ch < 14); }";b=0}1' >temp.tmp &&  mv temp.tmp c_string.c
cat alloc.c | awk '/int nlz16/{$0="/*"$0;b=1}b && /^}/{$0=$0"*/";b=0}/int nlz8/{c=1}c && /^}/{$0=$0"\nstatic inline int nlz16(uint16_t x)\n{ return ((x >> 8) == 0 )? 8+nlz8(x&0xff): nlz8(x>>8); }";c=0}1' >temp.tmp && mv temp.tmp alloc.c
# simple functions
sed -i -e "s@strpbrk(buf, \".e\")@strchr(buf, \'.\') || strchr(buf, \'e\')@" value.c
sed -i -e 's/atol(mrbc_string_cstr(&v\[i\]));/mrbc_atoi( mrbc_string_cstr(\&v[i]),10 );/' c_object.c
sed -i -e 's/atof/mrbc_atof/' c_string.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/atof.c >> value.c
awk '/atoi/{$0=$0"\nmrbc_float_t mrbc_atof(const char *s);"}1' value.h >temp.tmp && mv temp.tmp value.h
#
sed -i -e 's@extern@#if defined(CH32V20x)\n  extern@' -e 's@(mrblib_bytecode);@(mrblib_bytecode);\n  #endif@' class.c
sed -i -e 's@#include "rrt0.h"@#if defined(CH32V20x)\n#include "rrt0.h"\n#endif@' mrubyc.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_gpio.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_gpio.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_pwm.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_pwm.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_adc.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_adc.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_i2c.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_i2c.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_spi.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_spi.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_uart.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32fun_uart.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/mrbc_get.h
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/mrbc_get.c
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/hal.c
# make_output_buffer
wget -O - https://github.com/HirohitoHigashi/mrubyc-nucleo-F401RE/raw/refs/heads/chapter05_i2c/Core/mrubyc/stm32f4_i2c.c | ruby -e "print '#include \"mrubyc.h\"'+ARGF.read.split(/===+/)[1]" > outputbuffer.c
awk '/MRBC_TT_STRING:/{$0="#if MRBC_USE_STRING\n"$0;a=1}a&&!b&&/break/{$0=$0"\n#endif\n    case MRBC_TT_SYMBOL:\n      bufsiz += strlen(mrbc_symbol_cstr(&v[i]));\n      break;";a=0;b=1}a&&b&&/break/{$0=$0"\n#endif\n    case MRBC_TT_SYMBOL:\n      memcpy( pbuf, mrbc_symbol_cstr(&v[i]), strlen(mrbc_symbol_cstr(&v[i])) );\n      pbuf += mrbc_string_size(&v[i]);\n      break;\n";a=0;b=0}1' outputbuffer.c >temp.tmp && mv temp.tmp outputbuffer.c
wget -O - https://www.ipc.shimane-u.ac.jp/weda/mrubyc/arrayalloc.c >> outputbuffer.c
# autogen
make autogen # require ruby
cd ../..
mv mrubyc-release3.4.1/src/ mrubyc_ch32fun/mrubyc/
mv mrubyc-release3.4.1/support/ mrubyc_ch32fun/mrubyc/
mv mrubyc-release3.4.1/mrblib/ mrubyc_ch32fun/mrubyc/
#
# flash tools
tar xzvf wchisp-linux-x64.tar.gz
cp wchisp-linux-x64/wchisp mrubyc_ch32fun/
cp ch32fun-master/minichlink/minichlink mrubyc_ch32fun/
chmod +x mrubyc_ch32fun/wchisp
chmod +x mrubyc_ch32fun/minichlink
# CH32X035
mkdir mrubyc_ch32fun/ch32x035
cp ch32fun-master/examples_usb/USBFS/usbfs_cdc_tty/usb_config.h mrubyc_ch32fun/ch32x035
cp ch32fun-master/examples_usb/USBFS/usbfs_cdc_tty/funconfig.h mrubyc_ch32fun/ch32x035
cd mrubyc_ch32fun/ch32x035
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32x035.c
wget -O Makefile https://www.ipc.shimane-u.ac.jp/weda/mrubyc/Makefile.035
cd ../..
#
# CH32V203
mkdir mrubyc_ch32fun/ch32v203
cp ch32fun-master/examples_usb/USBD/usbd_cdc_tty/usb_config.h mrubyc_ch32fun/ch32v203
cp ch32fun-master/examples_usb/USBD/usbd_cdc_tty/funconfig.h mrubyc_ch32fun/ch32v203
cd mrubyc_ch32fun/ch32v203
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32v203.c
wget -O Makefile https://www.ipc.shimane-u.ac.jp/weda/mrubyc/Makefile.203
wget https://www.ipc.shimane-u.ac.jp/weda/mrubyc/combine.rb
cd ../..
#
cd mrubyc_ch32fun/ch32fun
awk '/\(\.rodata/{$0="#if TARGET_MCU_LD != 2 /* v20x */\n"$0"\n#endif"}/\.storage\)/{$0="\t\t\t*(.rodata)\n\t\t\t*(.rodata*)\n"$0}1' ch32fun.ld >temp.tmp && mv temp.tmp ch32fun.ld
cd ../..
#
# CH32V203 SWD
mkdir mrubyc_ch32fun/ch32v203swd
cp ch32fun-master/examples_v20x/debugprintfdemo/funconfig.h mrubyc_ch32fun/ch32v203swd
cd mrubyc_ch32fun/ch32v203swd
wget -O ch32v203.c https://www.ipc.shimane-u.ac.jp/weda/mrubyc/ch32v203swd.c
wget -O Makefile https://www.ipc.shimane-u.ac.jp/weda/mrubyc/Makefile.203
cd ../..

