#!/bin/bash

sourceSuffixes="odt odp odg odm sxw"
: > debian/erased_pdfs.log

for d in doc Documentation pcb_calculator; do
    find $d -name '*.pdf' -print |
    while read p; do
	ok=0
	dir=$(dirname "$p")
	b=$(basename "$p")
	for s in $sourceSuffixes; do
	    sourcePattern="${b/.pdf/*.$s}"
	    found=$(find $dir -iname "$sourcePattern")
	    if  [ -n "$found" ]; then ## found source file
		ok=1
	    fi
	done
	if [ -n "$(find $dir -iname '${b/.pdf/}')" ]; then ## found directory
	    ok=1
	fi
	if [ $ok = 0 ]; then
	    echo $p is sourceless, erasing it | tee -a debian/erased_pdfs.log
	    rm "$p"
	fi
    done
done
