Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | Too tired to sleep after Dour festival... Need some coding Now! =] |
- # | This script imports pictures from my camera |
- # | It also keeps an original in ${BASEDIR}/originals |
- # | |
- # | July 2011 flip hess [email protected] |
- # +-----------------------------------------------------------------------------------------+
- #############################################################################################
- # Global variables:
- #############################################################################################
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- ARG1="${1}"
- ARG2="${2}"
- TIME_STAMP="$( date '+%Y%m%d')"
- BASEDIR='/media/bigdisk/pictures'
- ORIGINALS="${BASEDIR}/originals"
- RAW="${ORIGINALS}/${TIME_STAMP}/RAW"
- JPG="${ORIGINALS}/${TIME_STAMP}/JPG"
- TMP="/var/tmp/foto-importer"
- # X settings:
- USER=$(whoami)
- PID=$(pgrep -u ${USER} nautilus)
- DBUS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/${PID}/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
- DBUS_SESSION_BUS_ADDRESS=${DBUS}
- #############################################################################################
- # Functions:
- #############################################################################################
- ##################################
- # exit function
- ##################################
- function die()
- {
- notify-send 'Camera import error!' "${1}" -i /usr/share/icons/gnome/48x48/status/messagebox_critical.png || echo "${1}"
- echo "${1}"
- mv ${TMP} ${TMP}.old || echo "Failed to move ${TMP} to ${TMP}.old!"
- exit 1
- }
- ##################################
- # check for arguments en env
- ##################################
- function fImportCheck()
- {
- # check for basedir
- [ -d ${BASEDIR} ] || die "basedir ${BASEDIR} not found! Exiting ${SCRIPT_PATH}!"
- # check for originals
- [ -d ${ORIGINALS} ] || die "basedir ${ORIGINALS} not found! Exiting ${SCRIPT_PATH}!"
- # check for shotwell:
- [ -x $( which shotwell ) ] || die "This script ${SCRIPT_PATH} depends on shotwell please install it by running \"sudo apt-get install shotwell\""
- # dit script werkt met timestamp data in files en mag dus niet precies een paar seconden voor 00:00 afgetrapt worden
- [ $(date +'%H%M%S') -gt 235955 ] && ( echo "waiting for day switch, please wait 10 seconds" ; sleep 10 )
- return 0
- }
- ##################################
- # function checks for digital camera:
- ##################################
- function fCamcheck()
- {
- # if camcheck is succesfull start fMain:
- CONNECTED="$( find ${HOME}/.gvfs -maxdepth 1 -type d -name '*gphoto2*' )"
- # generate output:
- if [ -n "${CONNECTED}" ] && [ -d "${CONNECTED}" ]
- then
- # cd to memorycard:
- cd "${CONNECTED}" || die "Failed to cd to fotodir! Exiting ${SCRIPT_PATH}!"
- # create file tree listing:
- find * -type f > ${TMP} || die "Failed to create tmpfile ${TMP}! Exiting ${SCRIPT_PATH}!"
- # if ${TMP}.old does not exist, touch it:
- if [ ! -f ${TMP}.old ] ; then
- echo "plop" > ${TMP}.old || die "Failed to regenerate ${TMP}.old. please rerun script! Exiting ${SCRIPT_PATH}!"
- fi
- # diff with old listing, if no difference, then exit script
- $(diff -qq ${TMP} ${TMP}.old $> /dev/null ) && { mv ${TMP} ${TMP}.old ; exit 1 || die "Failed to move tmpfile ${TMP}! Exiting ${SCRIPT_PATH}!"; }
- # send notification:
- { notify-send "Digital camera detected" "Detected a connected photo device\n Now creating: \n originals folder and adding new pics to shotwell library" -i /usr/share/icons/gnome/48x48/devices/camera.png; }
- # start import function:
- fImport "${CONNECTED}"
- else
- { notify-send "No Digital camera detected" "No Cam Detected\nPlease rerun script after a few seconds, or check your camera connection and battery\n" -i /usr/share/icons/gnome/48x48/devices/camera.png; }
- fi
- return 0
- }
- ##################################
- # The import function.
- ##################################
- function fImport()
- {
- # set var from fCamchecker
- local CONNECTED="${@}"
- # check for empty vars:
- [ -n "${CONNECTED}" ] || die "Empty function arguments variable detected! Isn't it cute? But it's wonk! Exiting ${SCRIPT_PATH}!"
- # run checker
- fImportCheck
- # check if there is allready a RAW dir, else create a raw+follow number
- COUNT=0
- while [ -d "${RAW}${COUNT}" ]
- do
- COUNT=$(( ${COUNT} ++1 ))
- done
- RAW="${RAW}${COUNT}"
- JPG="${JPG}${COUNT}"
- if [ ! -d "${RAW}" ] && [ ! -d "${JPG}" ]
- then
- # create datestamp dir in picture folder
- mkdir -p "${RAW}" || die "Failed to create ${RAW}! Exiting ${SCRIPT_PATH}!"
- # create datestamp dir in originals folder
- mkdir -p ${JPG} || die "Failed to create ${JPG}! Exiting ${SCRIPT_PATH}!"
- else
- die "Something went wrong, the dir already exists"
- fi
- # verify
- [ -d ${JPG} ] || die "${JPG} not found or not executable! please check permissions! Exiting ${SCRIPT_PATH}!"
- [ -d ${RAW} ] || die "${RAW} not found or not executable! please check permissions! Exiting ${SCRIPT_PATH}!"
- # cd to source and copy pictures from camera to originals folder
- cd "${CONNECTED}" || die "Failed to cd to ${CONNECTED}! Exiting ${SCRIPT_PATH}!"
- # copy files
- find "$( pwd )" -maxdepth 1 -mindepth 1 -type d -exec cp -rv {} ${ORIGINALS}/${TIME_STAMP} \; || die "Failed to copy dir to ${ORIGINALS}/${TIME_STAMP}"
- # diff directories
- { [ $( ls |wc -l) = 1 ] && DIR="$(ls)"; } || die "Not all directories copied. There is more then just a FOTO dir: \"$( ls | xargs echo )\""
- SOURCE="$( find ${DIR} -type f | wc -l )"
- DEST="$(find ${ORIGINALS}/${TIME_STAMP}/${DIR} -type f | wc -l )"
- if [ ${SOURCE} = ${DEST} ] ; then
- # remove copied files:
- find "$( pwd )" -type f -name '*.JPG' -delete && find "$( pwd )" -type f -name '*.NEF' -delete || die "Failed to remove pictures on camera! Exiting ${SCRIPT_PATH}!"
- else
- die "SOURCE ${CONNECTED}/${DIR} and DEST ${ORIGINALS}/${TIME_STAMP}/${DIR} are not identical! Exiting ${SCRIPT_PATH}!"
- fi
- # notify import succeeded
- notify-send 'Camera import succeeded!' "The import of pictures from you attached camera has succeeded" -i /usr/share/icons/gnome/48x48/status/messagebox_info.png || echo "${1}"
- # filter pictures to jpg and raw:
- for DEST in $( ls ${ORIGINALS}/${TIME_STAMP} | grep -Ev '(RAW|JPG)' )
- do
- # set file permissions
- chmod 777 ${ORIGINALS}/${TIME_STAMP}/${DEST} || die "Failed to set file permissions! Exiting ${SCRIPT_PATH}!"
- chmod -Rv --preserve-root 755 ${ORIGINALS}/${TIME_STAMP}/${DEST}/* || die "Failed to set file permissions! Exiting ${SCRIPT_PATH}!"
- # find and filter to dest dirs:
- find ${ORIGINALS}/${TIME_STAMP}/${DEST} -type f -name '*NEF' -exec chmod 640 {} \; -exec mv -vf {} ${RAW} \; || die "Failed to move NEF files to ${RAW}! Exiting ${SCRIPT_PATH}!"
- find ${ORIGINALS}/${TIME_STAMP}/${DEST} -type f -name '*JPG' -exec chmod 640 {} \; -exec mv -vf {} ${JPG} \; || die "Failed to move JPG files to ${JPG}! Exiting ${SCRIPT_PATH}!"
- # remove source dir if empty:
- if [ $( find ${ORIGINALS}/${TIME_STAMP}/${DEST} -type f | wc -l ) = 0 ]
- then
- rm -rf ${ORIGINALS}/${TIME_STAMP}/${DEST} || die "Failed to remove ${ORIGINALS}/${TIME_STAMP}/${DEST} after filtering files! Exiting ${SCRIPT_PATH}!"
- fi
- done
- # start shotwell
- shotwell > /dev/null 2>&1 &
- # remove tempfile
- mv -f ${TMP} ${TMP}.old || die "Failed to move ${TMP} to ${TMP}.old... Exiting ${SCRIPT_PATH}!"
- return 0
- }
- ##################################
- # The help function.
- ##################################
- function fShowUsage()
- {
- echo -e "\n\
- Foto importer\n\
- \n\
- Usage : ${SCRIPT_PATH} \n\
- \n\
- Detects if camera is connected and imports pictures\n\
- \n\
- \n\
- "
- return 0
- }
- #############################################################################################
- # Do the magic:
- #############################################################################################
- # check for notify osd utils:
- [ -x $( which notify-send ) ] || die "This script ${SCRIPT_PATH} depends on NotifyOSD and utils, please install it by running \"sudo apt-get install libnotify-bin\""
- # only start if not running:
- if [ -e /tmp/foto-importer ]
- then
- echo "Failed to run ${SCRIPT_PATH}, tempfile present in /tmp"
- exit 1
- fi
- # check for arguments
- [ ${#} = 0 ] || { fShowUsage ; exit 1; }
- # start the camcheck function
- fCamcheck "${@}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment