#!/bin/sh

function  help ()
{
echo "Usage: $(basename "$0") [-hatv] [-l ratio] [-s ratio] [-d targetdir] file1 [file2 ...]"
echo " -a              add slinks to thumbnails and source pictures in the current directory"
echo " -l factor       enlarge the picture by factor x prepending lx_ to the new file"
echo " -s factor       shrink the picture by factor x prepending sx_ to the new file"
echo " -q factor       quality factor, for jpg"
echo " -t              test, do no file operation"
echo " -v              verbose, more comments displayed"
echo " -d directory    all scaled files will go to directory. Default is the source file's directory."
}

function doit ()
{
    vecho "Entering doit function"
    vecho "\${filearg}" is "$filearg"
    if [ $verbose == OUI ]
    then
	ls -ltru "$filearg"
    fi
    vecho "\${targetdir}/\${newfilename}" is "${targetdir}/${newfilename}"
#    ls -ltru "${targetdir}/${newfilename}"
    if  [[ $smaller = NON && $larger = NON ]]
    then
	echo ln -nfs "$filearg" "${targetdir}/${newfilename}"
	ln -nfs "$filearg" "${targetdir}/${newfilename}"
    else
	cp -dpT "$filearg" "${targetdir}/${newfilename}"
	chmod a+w "${targetdir}/${newfilename}"
    #    mogrify -auto-orient -scale ${wnew}x${hnew} -quality $quality "${targetdir}/${newfilename}"
        mogrify -scale ${wnew}x${hnew} -quality $quality "${targetdir}/${newfilename}"
    fi
    touch -r "$filearg" "${targetdir}/${newfilename}"
}

# read functions y_or_n and debug_echo
if [[ -n "${MYSOURCERDIR}" || ! -d ${MYSOURCERDIR} ]]
then
    MYSOURCERDIR=${HOME}/bin/sourcers
fi
source ${MYSOURCERDIR}/interact
envassert MYSOURCERDIR

# test extra help situations  
if  [[ z$1 = "z--help" ||  z$1 = "z-?" ]]
    then help ; exit 0;    
fi

dohelp=NON
testing=NON
verbose=NON
larger=NON
lratio=1
smaller=NON
sratio=1
destspec=NON
dest=
addlinks=NON
quality_spec=NON
quality=95
sratio=1
lratio=1

while getopts ":v" Option
do
  case $Option in
		v )  verbose=OUI;  ;;
  esac
done
OPTIND=1
while getopts "ad:htvl:q:s:" Option
do
  case $Option in
      h )  dohelp=OUI; vecho "Option h"  ;;
      a )  addlinks=OUI; vecho "Option a, soft links will be added as necessary in current directory" ;;
      t )  testing=OUI; vecho "Option t, no file operations performed, testing only" ;;
      v )  verbose=OUI; vecho "Option v, verbose mode" ;;
      s )  smaller=OUI; sratio="$OPTARG"; vecho "Option s, smaller is $smaller, sratio is $sratio" ;;
      l )  larger=OUI; lratio="$OPTARG"; vecho "Option l, larger is $larger, lratio is $lratio" ;;
      d )  destspec=OUI; dest="$OPTARG"; vecho "Option d, destspec is $destspec, dest is $dest" ;;
      q )  quality_spec=OUI; quality="$OPTARG"; vecho "Option d, quality_spec is $quality_spec, quality is $quality" ;;
      * )  dohelp=OUI; echo "Bad option $OPTARG" ;;
  esac
done

shift $(($OPTIND - 1))
 
# REtest help situations  
if  [ $dohelp = OUI ] 
    then help ; exit 1;    
fi
if  [[ $smaller = OUI && $larger = OUI ]] 
    then help ; exit 1;    
fi

if  [[ $sratio = 1 && $lratio = 1 ]] 
then
    smaller=NON
    larger=NON
fi

vecho smaller is $smaller
vecho sratio is $sratio
vecho larger is $larger
vecho lration is $lratio

# if  [[ $smaller = NON && $larger = NON ]] 
#     then help ; exit 1;    
# fi
if [ $# = 0 ]; then dohelp=OUI; echo "Nothing to transfer." ; fi
dimsorig=
declare -i worig
declare -i horig
declare -i wnew
declare -i hnew


for filearg in "$@"
do
 if [ -L "$filearg" ]
 then
   echo "$filearg : sorry, don't mogrify symlinks."
 continue
 fi
 if [ ! -f "$filearg" ]
 then
   echo "$filearg : sorry, only mogrify regular files."
 continue
 fi
 filename=$(basename "$filearg")
 if [ $destspec = NON ]
 then
  targetdir=$(dirname "$filearg")
 else
  targetdir=$dest
 fi
 vecho filearg is $filearg filename is ${filename} targetdir is ${targetdir}
 worig=$(identify -format "%w" "$filearg")
 horig=$(identify -format "%h" "$filearg")
 vecho ${filearg}: worig is ${worig} horig is ${horig}
 if [ $smaller = OUI ]
 then
     wnew=`expr ${worig} / $sratio` 
     hnew=`expr ${horig} / $sratio` 
     newfilename="s${sratio}_${filename}"
 elif [ $larger = OUI ]
 then
     wnew=`expr ${worig} \* $lratio` 
     hnew=`expr ${horig} \* $lratio` 
     newfilename="l${lratio}_${filename}"
 else
     echo "nor larger nor smaller";
     newfilename="s1_${filename}"
#     help ; exit 1;    
 fi
 vecho ${newfilename}: wnew is ${wnew} hnew is ${hnew}
if [ ! -s "${targetdir}/${newfilename}" ]
then
  doit
#  showdo cp -dp "$filearg" ${targetdir}/${newfilename}
#  showdo mogrify -scale ${wnew}x${hnew} -quality $quality ${targetdir}/${newfilename}
#  showdo touch -r "$filearg" ${targetdir}/${newfilename}
else 
 echo "${targetdir}/${newfilename} already exists."
 ls "${targetdir}/${newfilename}"

    y_or_n "Do you want to overwrite it"
    if [ $? = 0 ] 
    then
	doit
# 	showdo cp -dp "$filearg" ${targetdir}/${newfilename}
# 	showdo mogrify -scale ${wnew}x${hnew} -quality $quality ${targetdir}/${newfilename}
# 	showdo touch -r "$filearg" ${targetdir}/${newfilename}
    fi
fi
 if [ $addlinks = OUI ]
 then
  if [ $targetdir != . ]
  then
  ln -sf "${targetdir}/${newfilename}" .
  fi
  if [ $(dirname "$filearg") != . ]
  then
  ln -sf "$filearg" .
  fi
 fi
 done
