#!/bin/bash # @Author: jguer # @Date: 2015-12-25 21:48:40 # @Last Modified by: jguer # @Last Modified time: 2015-12-26 13:52:52 # @Description: Wallpaper folder organizer # @Credits: tuxtweaks for getopts code #REQUIRES # - exiv2 # - imagemagick #TESTED ON # Archlinux, zsh #TODO # - Add Queue to files who get read before already sorted SCRIPT=`basename ${BASH_SOURCE[0]}` #Set fonts for Help. NORM=`tput sgr0` BOLD=`tput bold` REV=`tput smso` OPT_VER=0 #Verbosity OPT_REP=0 #Replace Original Folder COUNTER=0 #File Counter DIR_TO_COPY=0 #Help function function HELP { echo -e \\n"Help documentation for ${BOLD}${SCRIPT}.${NORM}"\\n echo -e "${REV}Basic usage:${NORM} ${BOLD}$SCRIPT ${NORM}"\\n echo "Command line switches are optional. The following switches are recognized." echo "${REV}-v${NORM} --Sets the value for option ${BOLD}verbosity${NORM} to 1. Default is ${BOLD}0${NORM}." echo "${REV}-r${NORM} --Sets the value for option ${BOLD}replace_folder${NORM} to 1. Default is ${BOLD}0${NORM}." echo -e "${REV}-h${NORM} --Displays this help message. No further functions are performed."\\n echo -e "Example: ${BOLD}$SCRIPT -v -r file.ext ${NORM}"\\n exit 1 } #Wallpaper sorting function function sort_wallpapers { Dims=0 NewName=0 NewUri=0 OldUri=0 PlaceHolder="/tmp/FixedWallpapers/converted.jpg"; cd $DIR_TO_COPY cp -Rf . /tmp/Wallpapers/ cd /tmp/Wallpapers for file in *.{jpg,png}; do if [ ! -e "${file}" ] && [ ! -s "${file}" ]; then #if file exists and isn't NULL continue fi iter_count=1 if [ "${file##*.}" = "png" ]; then #if file is png convert "$file" -quality 95 "$PlaceHolder" &>/dev/null OldUri=$PlaceHolder else OldUri=$file fi #Get jpg dimensions Dims="$(exiv2 -q "${OldUri}" | grep "Image size")" Dims="${Dims#*:}" Dims="$(echo -e "${Dims}" | tr -d '[[:space:]]')" if [ -z "${Dims// }" ]; then #if Dimensions can't be read continue fi if [[ ${file} == ${Dims}-*.jpg ]]; then NewUri="/tmp/FixedWallpapers/${file}" mv "$OldUri" "$NewUri" else #Count Wallpapers in fixed with same dimensions to ensure good labeling for iter in /tmp/FixedWallpapers/${Dims}*; do if [ -e ${iter} ] && [ -s ${iter} ]; then iter_count=$((iter_count+1)); fi done NewName="$Dims-$iter_count" NewUri="/tmp/FixedWallpapers/${NewName}.jpg" if [ $OPT_VER = 1 ]; then echo "Old Name: $file" echo "New Name: ${NewName}.jpg" fi jpegtran -copy none -progressive "$OldUri" > "$NewUri" &> /dev/null if [ "${file##*.}" = "png" ]; then rm "$PlaceHolder" fi fi COUNTER=$((COUNTER+1)); done rm -rf /tmp/Wallpapers if [ $OPT_REP = 1 ]; then rm -rf $DIR_TO_COPY mv /tmp/FixedWallpapers $DIR_TO_COPY if [ $OPT_VER = 1 ]; then echo "Replaced original folder" fi else if [ $OPT_VER = 1 ]; then echo "Fixed Wallpapers in /tmp/FixedWallpapers" fi xdg-open /tmp/FixedWallpapers fi } #Check the number of arguments. If none are passed, print help and exit. NUMARGS=$# if [ $NUMARGS -eq 0 ]; then HELP fi ### Start getopts code ### #Parse command line flags while getopts vrh OPT; do case $OPT in v) #set option "a" OPT_VER=1 ;; r) #set option "b" OPT_REP=1 ;; h) #show help HELP ;; help) #show help HELP ;; \?) #unrecognized option - show help echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed." echo -e "Use ${BOLD}$SCRIPT -h${NORM} to see the help documentation."\\n exit 2 ;; esac done # get rid of the just-finished flag arguments shift $(($OPTIND-1)) if [ $OPT_VER = 1 ]; then echo "Verbose Mode" echo "Replace original folder is $OPT_REP" echo "Removing folders from /tmp for fresh start." fi ### Main loop ### rm -rf /tmp/Wallpapers rm -rf /tmp/FixedWallpapers mkdir /tmp/Wallpapers mkdir /tmp/FixedWallpapers while (( "$#" )); do if [ -d $1 ]; then DIR_TO_COPY=$1 sort_wallpapers fi shift #Move on to next input directory. done if [ $OPT_VER = 1 ]; then echo "Read $COUNTER files" fi ### End main loop ###