#!/bin/bash

PREFIX=$1

STRIP=true
if [ -n "$PREFIX" ]
then
  SEDPREFIX=$(echo $PREFIX | sed -e 's/\//\\\//g')
  SQLPREFIX=$(echo $PREFIX | sed -e "s/'/''/g")

  PREFIX="WHERE path LIKE '${SQLPREFIX}%'"
fi

if ${STRIP}
then
  sqlite3 ~/.DeDupe.sqlite "SELECT hex(checksum),size,path FROM (SELECT * FROM files WHERE checksum IN (SELECT checksum FROM (SELECT checksum, COUNT(*) as num FROM files ${PREFIX} GROUP BY checksum) WHERE num >= 2)) ${PREFIX} ORDER BY size;" | sed "s/|${SEDPREFIX}/|/"
else
  sqlite3 ~/.DeDupe.sqlite "SELECT hex(checksum),size,path FROM (SELECT * FROM files WHERE checksum IN (SELECT checksum FROM (SELECT checksum, COUNT(*) as num FROM files ${PREFIX} GROUP BY checksum) WHERE num >= 2)) ${PREFIX} ORDER BY size;"
fi
