HERE=$(cd "$(dirname ${BASH_SOURCE})"; pwd)
input=$1
echo $input
directory=`echo $input | sed -E "s/(.*)(\/)(.*$)/\1/g"`
file=`echo $input | sed -E "s/(.*)(\/)(.*$)/\3/g"`
file_name=`echo $file| sed -E "s/(.*)(\.+)(.*$)/\1/g"`
file_suffix=`echo $file| sed -E "s/(.*)(\.+)(.*$)/\3/g"`
echo input:$input
echo directory:$directory
echo file_name:$file_name
echo file_suffix:$file_suffix

cd $HERE
  dimensions=$(magick identify -format "%w %h" "$input")
  width=$(echo $dimensions | cut -d' ' -f1)
  height=$(echo $dimensions | cut -d' ' -f2)
  echo width:$width
  echo height:$height
cd -

cd $HERE/$directory
  ratio=1.777778 # 16 / 9
  echo ratio:$ratio
  
  crop_width=$(echo "$height * $ratio" | bc -l)
  echo crop_width:$crop_width
  final_width=$(printf "%.0f" $crop_width)
  echo final_width:$final_width
  full_file_name=$file_name.$file_suffix
  echo full_file_name:$full_file_name
  
  if [ ! -e $full_file_name ]; then
      echo "[WARN] file not found"
      exit
  fi
  magick $full_file_name -gravity center -crop ${final_width}x${height}+0+0 +repage -resize 480x270 _$full_file_name
  mv _$full_file_name $full_file_name
cd -
