#!/bin/bash

# exit on err
set -e

if [ $# -le 1 ]; then
    echo "Usage: genISR.sh <ISRgen.c> <ISRgen.inc>"
    exit 1
fi

# varibles
ISRS_GEN_C=$1
ISRS_GEN_ASM=$2

ISRS_WITH_ERROR_CODE="8 10 11 12 13 14 17 21 29 30"

# Gen C File
# Later(TM), scratch that... NOW
echo "// THIS FILE IS AUTOGENERATED VIA genISR.sh, because im pacient but not THAT pacient good lord its 500 lines of pure hell also like i have no braincells left..." > $ISRS_GEN_C
echo "#include \"idt.h\"" >> $ISRS_GEN_C
echo "#include \"gdt.h\"" >> $ISRS_GEN_C
echo "" >> $ISRS_GEN_C

for i in $(seq 0 255); do
    echo "void __attribute((cdecl)) ISR${i}();" >> $ISRS_GEN_C
done

echo "" >> $ISRS_GEN_C
echo "void ISR_InitializeGates()" >> $ISRS_GEN_C
echo "{" >> $ISRS_GEN_C

for i in $(seq 0 255); do
    echo "    IDT_SetGate(${i}, ISR${i}, GDT_CODE_SEGMENT, IDT_FLAG_RING0 | IDT_FLAG_GATE_32BIT_INT);" >> $ISRS_GEN_C
done


echo "}" >> $ISRS_GEN_C
# die die die die die die die die die go my braincells

# Generate ASM
echo "; THIS FILE IS AUTOGENERATED VIA genISR.sh i could've done this once myself but shell script is my fren" > $ISRS_GEN_ASM

for i in $(seq 0 255); do
    if echo "$ISRS_WITH_ERROR_CODE" | grep -q "\b${i}\b"; then
        echo "ISR_ERRORCODE ${i}" >> $ISRS_GEN_ASM
    else
        echo "ISR_NOERRORCODE ${i}" >> $ISRS_GEN_ASM
    fi
echo "I Have No Braincells^10, its 4:30 in the morning... anyways your files were made..."
done