#!/bin/sh

# Copyright (c) 2026 Bible Code Lab
# You may share and modify this code only for the purpose of improvement.
# Modifications intended to alter, bias, or manipulate computational results,
# or to introduce malicious behavior, are strictly prohibited.
# This software is provided "as is", without warranty of any kind.
# 
# SCRIPT NAME:  isaiah-53-esl.sh
#
# DESCRIPTION:  Test the two ELS codes of Isaiah 53:8-54:1 using regular 
#               expressions. It verifies this code against two versions of the 
#               Hebrew Bible:
#               - Miqra According to the Masorah (MAM): only 1 ELS found
#               - Leningrad Codex: both ELS found
#
# AUTHOR:       Bible Code Lab
#
# DATE:         2025-11-24
#
# VERSION:      1.1
#
# TESTED ON: 
#               - 6.12.63+deb13-amd64 GNU/Linux
#               - GNU sed v4.9
#               - GNU grep v3.11
#               - GNU wget v1.25.0
#
# USAGE:        ./isaiah-53-esl.sh
#
#               If the patterns matches (the ELS is found), the sed command 
#               should print the Hebrew letters it is compossed of. Otherwise, 
#               it won't print anything.
#
# NOTES:        For more info go to:
#               https://beholdthestone.com/archives/770
# 

ISAIAH_MAM_URL="https://raw.githubusercontent.com/TorahBibleCodes/TorahBibleCodes/refs/heads/master/texts/text_MAM_12isaiah.csv"
ISAIAH_LENINGRAD_URL="https://raw.githubusercontent.com/TorahBibleCodes/TorahBibleCodes/refs/heads/master/texts/text_leningrad_12isaiah.json"

ISAIAH_MAM_FILENAME="isaiah-mam.csv"
ISAIAH_LENINGRAD_FILENAME="isaiah-leningrad.json"

EXPECTED_SHA256_MAM="82ca0083b1a9754f78f14f5ed3e4e443f7688739280c1564d2729ead7559caf9"
EXPECTED_SHA256_LENINGRAD="818d32d13f6e62a4d11396e31681bd9db76bab6df4fbbbc8625b44cdf1a774c4"

SUFFIX="formatted.txt"

# Test ELS
# $1 Source text
testELS() {
  OUTPUT_FILE="$1-$SUFFIX"

  # Removes every chapter that is not in the Hebrew classic alphabet
  grep -oP '[\x{05D0}-\x{05EA}]+' $1 | tr -d '\n' > $OUTPUT_FILE
  
  printf "Jesus is my name: "
  sed -E -n "s/^.+(\xD7\x99).{19}(\xD7\x9E).{19}(\xD7\xA9).{19}(\xD7\xA2).{19}(\xD7\x95).{19}(\xD7\xA9).{19}(\xD7\x99).+$/\7\6\5\4\3\2\1/p" $OUTPUT_FILE
  printf "\n"

  printf "I was crucified: "
  sed -E -n "s/^.+(\xD7\x9F).{51}(\xD7\xA5).{51}(\xD7\x9C).{51}(\xD7\x91).{51}(\xD7\xAA).{51}(\xD7\x99).+$/\1\2\3\4\5\6/p" $OUTPUT_FILE
  printf "\n"
}

# Download and verify SHA256 Sum
# $1 - URL
# $2 - Filename
# $3 - Expected SHA
download() {
  wget --no-use-server-timestamps -O "$2" "$1"
  COMPUTED_SHA256=$(sha256sum "$2" | cut -f 1 -d " ")
  if [ $COMPUTED_SHA256 != $3 ]; then
    printf "Error: SHA256 sum mismatch!\n"
    printf "Expected: $EXPECTED_SHA256\n"
    printf "Computed: $COMPUTED_SHA256\n"
    exit 1
  fi
}

download $ISAIAH_MAM_URL $ISAIAH_MAM_FILENAME $EXPECTED_SHA256_MAM
printf "Matching ELS in Miqra according to the Masorah\n"
testELS $ISAIAH_MAM_FILENAME
printf "\n"

download $ISAIAH_LENINGRAD_URL $ISAIAH_LENINGRAD_FILENAME $EXPECTED_SHA256_LENINGRAD
printf "Matching ELS in Leningrad Codex\n"
testELS $ISAIAH_LENINGRAD_FILENAME
printf "\n"

