#!/usr/local/bin/bash
# $Id: fordxo,v 1.48 2025/09/21 04:39:25 grog Exp grog $
#
# Link all .ORF files to Oly directory.
# Input is [-d] [-f] [-j] [-m] [-M file] [files]
# XXX gone -d deletes old contents.
# -1 Fake an OM System OM-1.
#    Now done automatically with file names starting in A
# -f links even if dest file exists.
# -j links .JPG and .tif files
# -k don't deletes old contents.
# -m greps from Makejpeg file.
# -M <file> greps from specified file.
# files are an explicit list of files.
# If -m is specified, files are ignored.
# If files are not specified, all files without corresponding JPEGs are linked.
#
# Return TRUE if a converted image exists for this base name.
convertednames=".jpeg .jpg .tiff .tif .JPEG .JPG  _DxO.jpeg  _DxO.jpg  _DxO.tiff  _DxO.tif  _DxO.JPEG  _DxO.JPG"
isconverted ()
{
  base=$1
  for name in $convertednames; do
    converted=$base$name
      if [ -e $converted ]; then
	echo $converted;
	return
      fi
  done
  echo
 }
CLEAN=yes
if [ "$1" = "-d" ]; then     # link .JPG files
  echo "You don't need -d any more"
  shift
fi
if [ "$1" = "-f" ]; then     # do everything, even if we have a result file
  BANG=yes
  shift
fi
if [ "$1" = "-j" ]; then     # link .JPG files
  echo "You don't need -j any more"
  shift
fi
if [ "$1" = "-k" ]; then     # don't delete contents first
  CLEAN=
  shift
fi
if [ "$1" = "-m" ]; then   # Makejpeg
  FILEBASES=`tail +2 Makejpeg | sed 's: .*::; s:.JPG::'`
  for i in $FILEBASES; do
    if [ -f orig/$i.ORF ]; then
       FILES="$FILES orig/$i.ORF"
    elif [ -f orig/$i.JPG ]; then
       FILES="$FILES orig/$i.JPG orig/$i.jpg"
    else
       echo "Can't find a candidate for" $i
    fi
  done
elif [ "$1" = "-M" ]; then   # specified file, no header
  FILES=`sed < $2 's:_DxO .*:.ORF:; s:.JPG:*:; s:^:orig/:'`
elif [ -f "$1" ]; then
  FILES=$*
else				# default: no specification, all .ORFs and .dops, maybe .JPGs
  FILES=`ls orig/JPEG/*.JPG orig/JPEG/* orig/JPEG/*.tif orig/JPEG/*.TIF orig/*.ORF orig/*.dop orig/*.NEF orig/*.CR2 2>/dev/null`
  TIFFS=`ls orig/*.tif orig/*.tiff 2>/dev/null`
  for i in $TIFFS; do
      echo Looking for ${i/ORF/tiff} and ${i/.ORF/_DxO.tiff}
      #      if [ ! -e ${i/ORF/tiff} -a ! -e ${i/.ORF/_DxO.tiff} ]; then
      if [ ! -e ${i/.ORF/_DxO.tiff} ]; then
	FILES="$FILES $i"
      else
	  echo Found ${i/.ORF/_DxO.tiff}
      fi
  done
# debug  echo TIFFS $TIFFS FILES $FILES
fi
echo $LINKING $FILES
ME=`whoami`
if [ "$OLY" = "" ]; then
  if [ "$ME" = "grog" ]; then
    OLY=/Photos/2-grog;
  elif [ "$ME" = "yvonne" ]; then
    OLY=/Photos/3-yvonne;
  else
    OLY=/Photos/00-`whoami`
  fi
fi
mkdir -p ${OLY}
# For Microsoft
chmod 777 ${OLY}
if [ "$CLEAN" = "yes" ]; then
  rm -rf ${OLY}/* ${OLY}/.n*
fi
# echo $FILES
CHOSEN=0
for i in `ls -rt $FILES`; do
  base=$i
  for ext in .ORF .NEF .CR2 .JPG .JPEG .jpg .tif .tiff; do
    base=`basename $base $ext`
  done
  base=orig/$base
# XXX what's this crap for?
# DST=$OLY/`printf %03d $CHOSEN`-`basename $i`
  DST=$OLY
  if [ "$JPG" = "yes" ]; then
# XXX make sure we don't have issues converting JPEG to TIFF.
    if [ \( "$BANG" = "yes" \) -o \( ! -e $base.jpeg -a ! -e $base.JPEG -a ! -e $base.tiff \) ]; then
      ln $i $DST
      CHOSEN=`expr $CHOSEN + 1`
    fi
  else				# raw image
#    echo converted `isconverted $base`
#    echo $base, $base.jpeg,  $base.jpg, $base.tiff, $base.tif, $base.JPEG, $base.JPG foo
    if [ "$BANG" = "yes" ]; then
      doit=YES
    elif [ `isconverted $base` ]; then
      doit=
    else
	doit=$base
    fi
#    if [ \( "$BANG" = "yes" \) -o \( ! -e $base.jpeg -a ! -e $base.jpg -a ! -e $base.tiff -a ! -e $base.tif -a ! -e $base.JPEG -a ! -e $base.JPG \) ]; then
#    if [ \( "$BANG" = "yes" \) -o `isconverted $base` ]; then
# test "$BANG" = "yes"
# test "$BANG" = "yes" -o ! $doit
# test "$BANG" = "yes" -o ! $doit = ""
#   echo doit $base: $doit
    if [ $doit ]; then
      FILE=`basename $i`
      if [ "${FILE:0:1}" = "A" -a "${FILE: -3:3}" != "dop" ]; then # OM-1 Mark II, fake Mark I
	exiftool -model="OM-1" -o $DST/$FILE $i
       else
	 ln $i $DST
       fi
       CHOSEN=`expr $CHOSEN + 1`
    else
	echo Destination file for $base already exists
    fi
  fi
done
# Now remove JPEGs for which we have a raw file.
# Cheat: converted raw images end in .jpg and not .JPG.
cd $OLY
for i in `ls *.jpg 2> /dev/null`; do
  echo --- $i
  for ext in .ORF .NEF .CR2; do
# debug echo 146 ${i/.JPG/$ext}
# Why doesn't this work with -o?
    if [ -e ${i/.jpg/$ext} ]; then
      echo $ext
      rm $i
    fi
  done
done
# And again for capitals.  How can I do this more elegantly?
for i in `ls *.JPG 2> /dev/null`; do
  echo --- $i
  for ext in .ORF .NEF .CR2; do
# debug echo 146 ${i/.JPG/$ext}
# Why doesn't this work with -o?
    if [ -e ${i/.JPG/$ext} ]; then
      echo $ext
      rm $i
    fi
  done
done
echo `ls *.ORF *.CR2 *.jpg *.JPG *.NEF 2> /dev/null | wc -l` of $CHOSEN files to process
cd -
