#!/bin/bash

# based on https://ubuntuforums.org/showthread.php?t=899179

flags=""
if [ "$1" == "-i" ]; then
  flags="-i"
  shift
fi

if [ $# -lt 2 ]; then
  echo "Usage: $(basename $0) [-i] searchterm file1 [..fileN]"
  exit 1
fi

term="$1"
shift

for file in "$@"; do
  if [ ! -r "$file" ]; then
    echo "odfgrep: $file: File not found"
    continue
  fi
  unzip -ca "$file" content.xml | grep -ql $flags "$term"
  if [ $? -eq 0 ]; then
    echo "$file"
  fi
done
