#!/bin/bash

# Default images directory. If image not supplied, will choose a random
# image from this folder. 
imagesDir="$HOME/Pictures/"
# Default resolution of image. Format: WWWWxHHHH, e.g. 1920x1080
resolution="1920x1080"
# Location that created image will be in
outputDir="$HOME/Pictures/"
plain=0
number=1

display_help() {
    echo "Usage: $0 [options] [image...]" >&2
    echo
    echo "   image...                    zero or more paths to images. If none are provided"
    echo "                               the images will be randomly chosen from --directory"
    echo "   -r, --resolution=           make image of resolution WxH (set to monitor size)"
    echo "   -d, --directory=            directory from which to select random image (if "
    echo "                               image not provided). Default: \"\$HOME/Pictures/\""
    echo "   -o, --output=               directory which the created image will be written to."
    echo "                               Default: \"\$HOME/Pictures/\""
    echo "   -b, --blur                  if this option, then the image will have a "
    echo "                               blurred version of the same image behind. Note: this"
    echo "                               option is mutually exclusive with -p|--plain, and is"
    echo "                               the default option"
    echo "   -p, --plain                 if this option, then the image will not have a"
    echo "                               blurred image behind, but rather a shadow and a"
    echo "                               transparent background"
    echo "   -n, --number=               the number of images to choose randomly if image is"
    echo "                               not provided. By default, number=1. This is ignored
                                         if image... is not blank"
    exit 1
}

images=()

while [ "$#" -gt 0 ]; do
  case "$1" in
    -h|--help) display_help;;

    -r) resolution="$2"; shift 2;;
    -i) image="$2"; shift 2;;
    -d) imagesDir="$2"; shift 2;;
    -o) outputDir="$2"; shift 2;;
    -b) plain=0; shift 1;; 
    -p) plain=1; shift 1;; 
    -n) number="$2"; shift 2;;

    --resolution=*) resolution="${1#*=}"; shift 1;;
    --image=*) image="${1#*=}"; shift 1;;
    --directory=*) imagesDir="${1#*=}"; shift 1;;
    --output=*) outputDir="${1#*=}"; shift 1;;
    --resolution|--image|--directory|--output) echo "$1 requires an argument" >&2; exit 1;;
    --blur) plain=0; shift 1;;
    --plain) plain=1; shift 1;;
    --number=*) number="${1#*=}"; shift 1;;
    
    -*) echo "unknown option: $1" >&2; display_help;;
    *) images+=( "$1" ); shift 1;;
  esac
done

imagesDir="${imagesDir/#\~/$HOME}"
outputDir=${outputDir%/}/
outputDir="${outputDir/#\~/$HOME}"

if [ ${#images[@]} -eq 0 ]; then
  while IFS= read -r line; do
    images+=( "$line" )
  done < <( find "${imagesDir}" -type f | sort -R | tail -"$number" )
fi

for imageMaybePartial in "${images[@]}"; do
  image="$(readlink -f "$imageMaybePartial")"

  imageNameContainingPath=${image//"/"/"|"}
  newimageWithExt="${outputDir}BACKGROUND-${imageNameContainingPath}"
  newimage=${newimageWithExt%.*}".png"
  echo "Making \"$(basename "${image}")\" for background."

  if [ $plain -eq 1 ]; then
    new_width=$(( ${resolution%x*} * 9 / 10 ))
    new_height=$(( ${resolution#*x} * 9 / 10 ))
    new_resolution="${new_width}x${new_height}"

    convert "${image}" -resize "${new_resolution}>"  resized.png 
    convert resized.png \( +clone -background black -shadow 70x10+0+4 \) +swap -background transparent -layers merge +repage "${newimage}"

    rm resized.png
  else
    convert "${image}" -resize "${resolution}"^ resized.png
    convert resized.png -gravity center -crop "${resolution}"+0+0 +repage cropped.png
    convert cropped.png -blur 0x25 blurred.png
    convert "${image}" -resize "${resolution}>" resized.png
    composite resized.png -gravity center blurred.png "${newimage}"

    rm resized.png
    rm cropped.png
    rm blurred.png
  fi

  echo "Created " "${newimage}"
done
