#!/usr/bin/env bash
set -euo pipefail
plain="${1:-task5_plain.txt}"
outdir="${2:-task5_out}"
byte_index="${3:-55}"
block_size="${4:-16}"
span="${5:-2}"
block=$(( (byte_index - 1) / block_size ))
start_block=$(( block - span ))
if [ "$start_block" -lt 0 ]; then start_block=0; fi
start=$(( start_block * block_size ))
length=$(( (span * 2 + 1) * block_size ))
for f in \
  "$plain" \
  "$outdir/task5_ecb_dec.txt" \
  "$outdir/task5_cbc_dec.txt" \
  "$outdir/task5_cfb_dec.txt" \
  "$outdir/task5_ofb_dec.txt"
do
  xxd -g 1 -s "$start" -l "$length" "$f"
  printf '\n'
done
cmp -l "$plain" "$outdir/task5_ecb_dec.txt" | head -20 || true
printf '\n'
cmp -l "$plain" "$outdir/task5_cbc_dec.txt" | head -20 || true
printf '\n'
cmp -l "$plain" "$outdir/task5_cfb_dec.txt" | head -20 || true
printf '\n'
cmp -l "$plain" "$outdir/task5_ofb_dec.txt" | head -20 || true
