#!/bin/bash

DEFAULT_ZIP_NAME="nwt_s-BR2_E.brl.zip"
ZIP_NAME=${1:-$DEFAULT_ZIP_NAME}
DEFAULT_DIR_NAME="nwt2013brf"
DIR_NAME=${2:-$DEFAULT_DIR_NAME}

# List of bible book names from the ZIP file
brl_bible_books=(
  nwt_01_Ge_s-BR2_E.brl nwt_02_Ex_s-BR2_E.brl nwt_03_Le_s-BR2_E.brl
  nwt_04_Nu_s-BR2_E.brl nwt_05_De_s-BR2_E.brl nwt_06_Jos_s-BR2_E.brl
  nwt_07_Jg_s-BR2_E.brl nwt_08_Ru_s-BR2_E.brl nwt_09_1Sa_s-BR2_E.brl
  nwt_10_2Sa_s-BR2_E.brl nwt_11_1Ki_s-BR2_E.brl nwt_12_2Ki_s-BR2_E.brl
  nwt_13_1Ch_s-BR2_E.brl nwt_14_2Ch_s-BR2_E.brl nwt_15_Ezr_s-BR2_E.brl
  nwt_16_Ne_s-BR2_E.brl nwt_17_Es_s-BR2_E.brl nwt_18_Job_s-BR2_E.brl
  nwt_20_Pr_s-BR2_E.brl nwt_21_Ec_s-BR2_E.brl
  nwt_22_Ca_s-BR2_E.brl nwt_23_Isa_s-BR2_E.brl nwt_24_Jer_s-BR2_E.brl
  nwt_25_La_s-BR2_E.brl nwt_26_Eze_s-BR2_E.brl nwt_27_Da_s-BR2_E.brl
  nwt_28_Ho_s-BR2_E.brl nwt_29_Joe_s-BR2_E.brl nwt_30_Am_s-BR2_E.brl
  nwt_31_Ob_s-BR2_E.brl nwt_32_Jon_s-BR2_E.brl nwt_33_Mic_s-BR2_E.brl
  nwt_34_Na_s-BR2_E.brl nwt_35_Hab_s-BR2_E.brl nwt_36_Zep_s-BR2_E.brl
  nwt_37_Hag_s-BR2_E.brl nwt_38_Zec_s-BR2_E.brl nwt_39_Mal_s-BR2_E.brl
  nwt_40_Mt_s-BR2_E.brl nwt_41_Mr_s-BR2_E.brl nwt_42_Lu_s-BR2_E.brl
  nwt_43_Joh_s-BR2_E.brl nwt_44_Ac_s-BR2_E.brl nwt_45_Ro_s-BR2_E.brl
  nwt_46_1Co_s-BR2_E.brl nwt_47_2Co_s-BR2_E.brl nwt_48_Ga_s-BR2_E.brl
  nwt_49_Eph_s-BR2_E.brl nwt_50_Php_s-BR2_E.brl nwt_51_Col_s-BR2_E.brl
  nwt_52_1Th_s-BR2_E.brl nwt_53_2Th_s-BR2_E.brl nwt_54_1Ti_s-BR2_E.brl
  nwt_55_2Ti_s-BR2_E.brl nwt_56_Tit_s-BR2_E.brl nwt_57_Phm_s-BR2_E.brl
  nwt_58_Heb_s-BR2_E.brl nwt_59_Jas_s-BR2_E.brl nwt_60_1Pe_s-BR2_E.brl
  nwt_61_2Pe_s-BR2_E.brl nwt_62_1Jo_s-BR2_E.brl nwt_63_2Jo_s-BR2_E.brl
  nwt_64_3Jo_s-BR2_E.brl nwt_65_Jude_s-BR2_E.brl nwt_66_Re_s-BR2_E.brl
)

# List of bible book names to generate
brf_bible_books=(
  "Genesis" "Exodus" "Leviticus" "Numbers" "Deuteronomy" "Joshua"
  "Judges" "Ruth" "1 Samuel" "2 Samuel" "1 Kings" "2 Kings"
  "1 Chronicles" "2 Chronicles" "Ezra" "Nehemiah" "Esther" "Job"
  "Proverbs" "Ecclesiastes" "Song of Solomon" "Isaiah"
  "Jeremiah" "Lamentations" "Ezekiel" "Daniel" "Hosea" "Joel"
  "Amos" "Obadiah" "Jonah" "Micah" "Nahum" "Habakkuk" "Zephaniah"
  "Haggai" "Zechariah" "Malachi" "Matthew" "Mark" "Luke" "John"
  "Acts" "Romans" "1 Corinthians" "2 Corinthians" "Galatians"
  "Ephesians" "Philippians" "Colossians" "1 Thessalonians"
  "2 Thessalonians" "1 Timothy" "2 Timonthy" "Titus" "Philemon"
  "Hebrews" "James" "1 Peter" "2 Peter" "1 John" "2 John"
  "3 John" "Jude" "Revelation"
)

# The NWT 2013 edition extras names
brl_bible_extras=(
  nwt_00-1_E.brl nwt_00-2_E.brl nwt_00-3_E.brl 
  nwt_101_E.brl nwt_102_E.brl nwt_103_E.brl nwt_104_E.brl nwt_105_E.brl
  nwt_106_E.brl nwt_107_E.brl nwt_108_E.brl nwt_109_E.brl nwt_110_E.brl
  nwt_111_E.brl nwt_112_E.brl nwt_113_E.brl nwt_114_E.brl nwt_115_E.brl
  nwt_116_E.brl nwt_117_E.brl nwt_118_E.brl nwt_119_E.brl nwt_120_E.brl
  nwt_121_E.brl nwt_122_E.brl nwt_123_E.brl nwt_124_E.brl nwt_125_E.brl
)

if [ ! -f "$ZIP_NAME" ]; then
  echo "NWT Braille ZIP file '$ZIP_NAME' not found."
  echo "Usage: ./convert_nwt_brl_to_brf.sh [ZIP file] [output directory]"
  exit 1
fi

echo "Unpacking '$ZIP_NAME' to '$DIR_NAME'..."
unzip $ZIP_NAME -d $DIR_NAME > /dev/null
mkdir -p $DIR_NAME/books
mkdir -p $DIR_NAME/extras

echo "Converting files to usable format and names for NLS EReader..."
# We replace CHAPTER # by ch# for quicker search.
# Lines are wrapped at 75 chars because the NLS EReader would throw
# in line breaks when the lines reached 80 chars that wasted space
# and made reading fluently difficult.
for i in ${!brl_bible_books[@]};
do
  sed 's/\(,\*APT] \)/  */g' "$DIR_NAME/${brl_bible_books[$i]}" | fold -w 75 -s > "$DIR_NAME/books/${brf_bible_books[$i]}.brf"
done

# Special case for Psalms because it doesn't use "chapter" but for
# consistency in looking up scriptures we change it to ch# as well.
sed 's/\(,PSALM #\)/  *#/g' "$DIR_NAME/nwt_19_Ps_s-BR2_E.brl" | fold -w 75 -s > "$DIR_NAME/books/Psalms.brf"

for fn in ${brl_bible_extras[@]};
do
  fold -w 75 -s "$DIR_NAME/$fn" > "$DIR_NAME/extras/${fn%.brl}.brf"
done

echo "Removing source files..."
rm $DIR_NAME/*.brl

echo "Done!"

