flipje

foto-importer-ubuntu

Jul 20th, 2011
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.27 KB | None | 0 0
  1. #!/bin/bash
  2. # +-----------------------------------------------------------------------------------------+
  3. # | Too tired to sleep after Dour festival... Need some coding Now! =]                      |
  4. # | This script imports pictures from my camera                                             |
  5. # | It also keeps an original in ${BASEDIR}/originals                                       |
  6. # |                                                                                         |
  7. # | July 2011 flip hess [email protected]                                                 |
  8. # +-----------------------------------------------------------------------------------------+
  9.  
  10.  
  11. #############################################################################################
  12. # Global variables:
  13. #############################################################################################
  14.  
  15.   PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  16.   SCRIPT_PATH="${0}"
  17.   ARG1="${1}"
  18.   ARG2="${2}"
  19.  
  20.   TIME_STAMP="$( date '+%Y%m%d')"
  21.  
  22.   BASEDIR='/media/bigdisk/pictures'
  23.  
  24.   ORIGINALS="${BASEDIR}/originals"
  25.   RAW="${ORIGINALS}/${TIME_STAMP}/RAW"
  26.   JPG="${ORIGINALS}/${TIME_STAMP}/JPG"
  27.  
  28.   TMP="/var/tmp/foto-importer"
  29.  
  30.   # X settings:
  31.   USER=$(whoami)
  32.   PID=$(pgrep -u ${USER} nautilus)
  33.   DBUS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/${PID}/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
  34.   DBUS_SESSION_BUS_ADDRESS=${DBUS}
  35.  
  36. #############################################################################################
  37. # Functions:
  38. #############################################################################################
  39.  
  40.   ##################################
  41.   # exit function
  42.   ##################################
  43.   function die()
  44.   {
  45.     notify-send 'Camera import error!' "${1}" -i /usr/share/icons/gnome/48x48/status/messagebox_critical.png || echo "${1}"
  46.     echo "${1}"
  47.     mv ${TMP} ${TMP}.old || echo "Failed to move ${TMP} to ${TMP}.old!"
  48.     exit 1
  49.   }
  50.  
  51.   ##################################
  52.   # check for arguments en env
  53.   ##################################
  54.   function fImportCheck()
  55.   {
  56.     # check for basedir
  57.     [ -d ${BASEDIR} ] || die "basedir ${BASEDIR} not found! Exiting ${SCRIPT_PATH}!"
  58.  
  59.     # check for originals
  60.     [ -d ${ORIGINALS} ] || die "basedir ${ORIGINALS} not found! Exiting ${SCRIPT_PATH}!"  
  61.  
  62.     # check for shotwell:
  63.     [ -x $( which shotwell ) ] || die "This script ${SCRIPT_PATH} depends on shotwell please install it by running \"sudo apt-get install shotwell\""
  64.  
  65.     # dit script werkt met timestamp data in files en mag dus niet precies een paar seconden voor 00:00 afgetrapt worden
  66.     [ $(date +'%H%M%S') -gt 235955 ] && ( echo "waiting for day switch, please wait 10 seconds" ; sleep 10 )
  67.  
  68.     return 0
  69.   }
  70.  
  71.   ##################################
  72.   # function checks for digital camera:
  73.   ##################################
  74.   function fCamcheck()
  75.   {
  76.     # if camcheck is succesfull start fMain:
  77.     CONNECTED="$( find ${HOME}/.gvfs  -maxdepth 1 -type d -name '*gphoto2*' )"
  78.  
  79.     # generate output:
  80.     if [ -n "${CONNECTED}" ] && [ -d "${CONNECTED}" ]  
  81.     then
  82.  
  83.       # cd to memorycard:
  84.       cd "${CONNECTED}" || die "Failed to cd to fotodir! Exiting ${SCRIPT_PATH}!"
  85.  
  86.       # create file tree listing:
  87.       find * -type f > ${TMP} || die "Failed to create tmpfile ${TMP}! Exiting ${SCRIPT_PATH}!"
  88.  
  89.       # if ${TMP}.old does not exist, touch it:
  90.       if [ ! -f ${TMP}.old ] ; then
  91.         echo "plop" > ${TMP}.old || die "Failed to regenerate ${TMP}.old. please rerun script!  Exiting ${SCRIPT_PATH}!"
  92.       fi
  93.  
  94.       # diff with old listing, if no difference, then exit script
  95.       $(diff -qq ${TMP} ${TMP}.old $> /dev/null ) && { mv ${TMP} ${TMP}.old ; exit 1 || die "Failed to move tmpfile ${TMP}! Exiting ${SCRIPT_PATH}!"; }
  96.  
  97.       # send notification:
  98.       { 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; }
  99.  
  100.       # start import function:
  101.       fImport "${CONNECTED}"
  102.     else
  103.       { 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; }
  104.     fi
  105.  
  106.     return 0
  107.  
  108.   }
  109.  
  110.   ##################################
  111.   # The import function.
  112.   ##################################
  113.   function fImport()
  114.   {
  115.  
  116.     # set var from fCamchecker
  117.     local CONNECTED="${@}"
  118.  
  119.     # check for empty vars:
  120.     [ -n "${CONNECTED}" ] || die "Empty function arguments variable detected! Isn't it cute? But it's wonk! Exiting ${SCRIPT_PATH}!"
  121.  
  122.     # run checker
  123.     fImportCheck
  124.  
  125.     # check if there is allready a RAW dir, else create a raw+follow number
  126.     COUNT=0
  127.     while [ -d "${RAW}${COUNT}" ]
  128.     do
  129.       COUNT=$(( ${COUNT} ++1 ))
  130.     done
  131.  
  132.     RAW="${RAW}${COUNT}"
  133.     JPG="${JPG}${COUNT}"    
  134.  
  135.     if [ ! -d "${RAW}" ] && [ ! -d "${JPG}" ]
  136.     then
  137.       # create datestamp dir in picture folder
  138.       mkdir -p "${RAW}" || die "Failed to create ${RAW}! Exiting ${SCRIPT_PATH}!"
  139.       # create datestamp dir in originals folder
  140.       mkdir -p ${JPG} || die "Failed to create ${JPG}! Exiting ${SCRIPT_PATH}!"
  141.     else
  142.       die "Something went wrong, the dir already exists"
  143.     fi
  144.  
  145.     # verify
  146.     [ -d ${JPG} ] || die "${JPG} not found or not executable! please check permissions! Exiting ${SCRIPT_PATH}!"
  147.     [ -d ${RAW} ] || die "${RAW} not found or not executable! please check permissions! Exiting ${SCRIPT_PATH}!"
  148.  
  149.     # cd to source and copy pictures from camera to originals folder
  150.     cd "${CONNECTED}" || die "Failed to cd to ${CONNECTED}! Exiting ${SCRIPT_PATH}!"
  151.      
  152.     # copy files
  153.     find  "$( pwd )" -maxdepth 1 -mindepth 1 -type d -exec cp -rv {} ${ORIGINALS}/${TIME_STAMP} \; || die "Failed to copy dir to ${ORIGINALS}/${TIME_STAMP}"
  154.  
  155.     # diff directories
  156.     { [ $( ls |wc -l) = 1 ] && DIR="$(ls)"; } || die "Not all directories copied. There is more then just a FOTO dir: \"$( ls | xargs echo )\""
  157.    
  158.     SOURCE="$( find ${DIR} -type f | wc -l )"
  159.     DEST="$(find ${ORIGINALS}/${TIME_STAMP}/${DIR} -type f | wc -l )"
  160.  
  161.     if [ ${SOURCE} = ${DEST} ] ; then    
  162.       # remove copied files:
  163.       find "$( pwd )" -type f -name '*.JPG' -delete && find "$( pwd )" -type f -name '*.NEF' -delete || die "Failed to remove pictures on camera! Exiting ${SCRIPT_PATH}!"
  164.     else
  165.       die "SOURCE ${CONNECTED}/${DIR} and DEST ${ORIGINALS}/${TIME_STAMP}/${DIR} are not identical! Exiting ${SCRIPT_PATH}!"
  166.     fi
  167.  
  168.     # notify import succeeded
  169.     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}"
  170.  
  171.     # filter pictures to jpg and raw:
  172.     for DEST in $( ls ${ORIGINALS}/${TIME_STAMP} | grep -Ev '(RAW|JPG)' )
  173.     do
  174.       # set file permissions
  175.       chmod 777 ${ORIGINALS}/${TIME_STAMP}/${DEST} || die "Failed to set file permissions! Exiting ${SCRIPT_PATH}!"
  176.       chmod -Rv --preserve-root 755 ${ORIGINALS}/${TIME_STAMP}/${DEST}/* || die "Failed to set file permissions! Exiting ${SCRIPT_PATH}!"
  177.      
  178.       # find and filter to dest dirs:
  179.       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}!"
  180.       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}!"
  181.    
  182.       # remove source dir if empty:
  183.       if [ $( find ${ORIGINALS}/${TIME_STAMP}/${DEST} -type f | wc -l ) = 0 ]
  184.       then
  185.         rm -rf ${ORIGINALS}/${TIME_STAMP}/${DEST} || die "Failed to remove ${ORIGINALS}/${TIME_STAMP}/${DEST} after filtering files! Exiting ${SCRIPT_PATH}!"
  186.       fi
  187.     done
  188.  
  189.     # start shotwell
  190.     shotwell > /dev/null 2>&1 &
  191.  
  192.     # remove tempfile
  193.     mv -f ${TMP} ${TMP}.old || die "Failed to move ${TMP} to ${TMP}.old...  Exiting ${SCRIPT_PATH}!"
  194.  
  195.     return 0
  196.  
  197.   }
  198.  
  199.  
  200.  
  201.   ##################################
  202.   # The help function.
  203.   ##################################
  204.   function fShowUsage()
  205.   {
  206.     echo -e "\n\
  207.    Foto importer\n\
  208.    \n\
  209.    Usage : ${SCRIPT_PATH} \n\
  210.    \n\
  211.    Detects if camera is connected and imports pictures\n\
  212.    \n\
  213.    Flip Hess 2011 - [email protected]\n\
  214.    \n\
  215.    "
  216.  
  217.     return 0
  218.  
  219.   }
  220.  
  221. #############################################################################################
  222. # Do the magic:
  223. #############################################################################################
  224.  
  225.   # check for notify osd utils:
  226.   [ -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\""
  227.  
  228.   # only start if not running:
  229.   if [ -e /tmp/foto-importer ]
  230.   then
  231.      echo "Failed to run  ${SCRIPT_PATH}, tempfile present in /tmp"
  232.      exit 1
  233.   fi
  234.  
  235.   # check for arguments
  236.   [ ${#} = 0 ] || { fShowUsage ; exit 1; }
  237.  
  238.   # start the camcheck function
  239.   fCamcheck "${@}"
  240.  
  241.   # Exit with previous return code:
  242.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment