#!/usr/bin/php
<?php
/*
  rm's filename2sfv - a script to create SFV files from filename-embedded checksums
  Copyright (C) 2009-2014 Roman Mamedov <https://romanrm.net/>

  This program is free software: you can redistribute it and/or modify it under the terms of
  the GNU General Public License version 3 as published by the Free Software Foundation.
  See http://www.gnu.org/licenses/gpl-3.0.html for more details.
*/

$version = '2014-04-10';
$OUTFILE = 'filename2sfv.sfv';

date_default_timezone_set('UTC');

$sfv  = "; Generated by filename2sfv v.$version <https://romanrm.net/filename2sfv>\n";
$sfv .= ";           at ".@date('Y-m-d H:i:s')." UTC\n;\n";

$file_cnt = 0;
foreach(glob('*') as $filename) {
  if(preg_match('/[\[\(]([a-fA-F0-9]{8})[\]\)]/', $filename, $matches)) {
    $crc = strtolower($matches[1]);
    $sfv .= "$filename $crc\n";
    $file_cnt++;
  }
}
if($file_cnt>0) {
  if(@file_put_contents($OUTFILE, $sfv)) {
    if($file_cnt==1) $s = ""; else $s = "s";
    echo "$file_cnt checksum$s written to $OUTFILE.\n";
  } else {
    echo "Unable to create output file!\n";
    exit(1);
  }
} else {
  echo "No filenames with checksums found in the current dir.\n";
  exit(1);
}

?>
