pbojinov
1/18/2013 - 12:49 PM

Improved code for snapping photos when committing to a git repo

Improved code for snapping photos when committing to a git repo

#!/bin/bash

function make_image_dir () {
  TARGET_DIR=$1
  if [ ! -d "${TARGET_DIR}" ]; then
    mkdir "${TARGET_DIR}"
  fi
}

function ignore_image_dir () {
  TARGET_DIR=$1
  GIT_IGNORE=$2
  grep -q "${TARGET_DIR}" "${GIT_IGNORE}"
  if [ $? -gt 0 ]; then
    echo "${TARGET_DIR}" >> "${GIT_IGNORE}"
  fi
}

function take_photo () {
  TARGET_DIR=$1
  DATE_STRING=`date '+%Y_%m_%d-%H_%M_%S'`
  imagesnap -q "${TARGET_DIR}/${USER}-${DATE_STRING}.jpg"
}

BASE_DIR=$GIT_DIR/..
IMAGE_DIR=commit_images

make_image_dir "$BASE_DIR/$IMAGE_DIR"
ignore_image_dir "$IMAGE_DIR" "$BASE_DIR/.gitignore"
take_photo "$BASE_DIR/$IMAGE_DIR"