wpfind/wpfind
2018-03-27 23:10:57 +02:00

255 lines
8.1 KiB
Bash
Executable file

#!/bin/bash
#------------------------------------------------------------------------------
# WPFind
# author : Francois B. aka Makotosan/Shakasan
# email : shakasan@sirenacorp.be
# licence : GPLv3
#------------------------------------------------------------------------------
# app version number
version="0.1.2"
# logfile
logfile="wpfind.log"
touch wpfind.log
# script dir
currentDir=$(pwd)
# bin date
date='/bin/date'
# defaults options
outputDir=$currentDir/wpfiles
inputDir=$currentDir
verbosemode=no
minw="1920"
# colors const used in the script
UNDERLINE=$(tput sgr 0 1)
BOLD=$(tput bold)
ROUGE=$(tput setaf 1)
VERT=$(tput setaf 2)
JAUNE=$(tput setaf 3)
BLEU=$(tput setaf 4)
MAUVE=$(tput setaf 5)
CYAN=$(tput setaf 6)
BLANC=$(tput setaf 7)
NORMAL=$(tput sgr0)
INV=$(tput smso)
BOLDROUGE=${BOLD}${ROUGE}
BOLDVERT=${BOLD}${VERT}
BOLDJAUNE=${BOLD}${JAUNE}
BOLDBLEU=${BOLD}${BLEU}
BOLDMAUVE=${BOLD}${MAUVE}
BOLDCYAN=${BOLD}${CYAN}
BOLDBLANC=${BOLD}${BLANC}
#------------------------------------------------------------------------------
# show help & informations
#------------------------------------------------------------------------------
function usage() {
printf "\n"
printf "$BOLDJAUNE";
printf "██╗ ██╗██████╗ ███████╗██╗███╗ ██╗██████╗ \n";
printf "██║ ██║██╔══██╗██╔════╝██║████╗ ██║██╔══██╗\n";
printf "██║ █╗ ██║██████╔╝█████╗ ██║██╔██╗ ██║██║ ██║\n";
printf "██║███╗██║██╔═══╝ ██╔══╝ ██║██║╚██╗██║██║ ██║\n";
printf "╚███╔███╔╝██║ ██║ ██║██║ ╚████║██████╔╝\n";
printf " ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ \n";
printf "$BOLDVERT";
printf "\n"
printf "Find wallpapers recursively in a dir and sub-directories...\n"
printf "\n"
printf "$NORMAL"
printf "Version : $version\n"
printf "Author : Francois B (Makoto)\n"
printf "Licence : GPLv3\n"
printf "\n"
printf "Usage : wpfind [options]\n"
printf " -c : copy wallpapers found in the specified directory\n"
printf " -m : move wallpapers found in the specified directory\n"
printf " -i <directory_to_analyze_to_find_wallpapers> (default: current directory)\n"
printf " -o <wallpapers_found_save_directory> (default: wpfiles inside the current directory)\n"
printf " -w <minimum_pixel_width> (default: 1920px)\n"
printf " -v : verbose mode\n"
printf " -h : show help & informations\n"
}
#------------------------------------------------------------------------------
# compute total elapsed time to process all files
#------------------------------------------------------------------------------
function timeElapsed () { #beginProcess endProcess
totaltime=$(($endProcess - $beginProcess))
heures=$(($totaltime / 3600))
tmp=$(($totaltime % 3600 ))
minutes=$(($tmp / 60 ))
secondes=$(($tmp % 60 ))
printf "Total time : $heures h $minutes min $secondes sec\n"
}
#------------------------------------------------------------------------------
# check if detox and Imagemagick are installed
#------------------------------------------------------------------------------
function depInstalled () {
if ! which detox >/dev/null; then
printf "$NORMAL""Detox installed : ""$BOLDROUGE""NO""$NORMAL""\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1
fi
if ! which identify >/dev/null; then
printf "$NORMAL""Imagemagick installed : ""$BOLDROUGE""NO""$NORMAL""\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1
fi
}
#------------------------------------------------------------------------------
# check if valid directory
#------------------------------------------------------------------------------
function isValidDirectory() { #directory #I/O
# check if the given outputDir is a valid directory
if [[ ! -d $1 ]]; then
printf "$NORMAL""-$2 setting is not a valid directory\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1
fi
}
#------------------------------------------------------------------------------
# find wp
#------------------------------------------------------------------------------
function wpFind () {
# cleaning filenames using detox
detox -r $1/* &>> $logfile
# check and remove if necessary an ending / to the outinputDir path
length=${#outputDir}
last_char=${outputDir:length-1:1}
if [[ $last_char == "/" ]]; then
outputDirToCheck=${outputDir:0:length-1}
fi
# browse recursively files and dir
for pic in $1/*; do
# if directory
if [ -d "$pic" ]; then
# if not outputDir
if [ "$pic" != "$outputDirToCheck" ]; then
# browse sub-dir
wpFind "$pic"
fi
# if file
elif [ -f "$pic" ]; then
# if file type is a picture
if [[ $(file --mime-type -b "$pic") == image/*g ]]; then
pic_w_size=$(identify -format %w $pic)
pic_h_size=$(identify -format %h $pic)
# if picture width > height
if [ "$pic_w_size" -gt "$pic_h_size" ]; then
# if picture width <= minimum width
if [ "$pic_w_size" -gt "$(($minw-1))" ]; then
# if copy mode
if [ "$pic2move" = "no" ]; then
# if verbose mode
if [ "$verbosemode" = "yes" ]; then
printf "cp $pic $outputDir\n"
fi
cp $pic $outputDir
date2log=$($date +'%Y-%m-%d %H:%M:%S')
printf "$date2log - cp $pic $outputDir\n" >> $logfile
# if move mode
elif [ "$pic2move" = "yes" ]; then
# if verbose mode
if [ "$verbosemode" = "yes" ]; then
printf "mv $pic $outputDir\n"
fi
mv $pic $outputDir
date2log=$($date +'%Y-%m-%d %H:%M:%S')
printf "$date2log - mv $pic $outputDir\n" >> $logfile
fi
picmoved=$(($picmoved+1))
fi
fi
fi
fi
done
}
#------------------------------------------------------------------------------
# Main
#------------------------------------------------------------------------------
function main() {
beginProcess=$($date +'%s')
printf "$NORMAL""Analyzing...(this may take a (very)(long) while)\n""$NORMAL"
# check if detox is installed
depInstalled
# create the outputDir if doesn't exist
mkdir -p $outputDir
# check and add if necessary an ending / to the outputDir path
length=${#outputDir}
last_char=${outputDir:length-1:1}
if [[ $last_char != "/" ]]; then
outputDir="$outputDir/"
fi
# check and remove if necessary an ending / to the inputDir path
length=${#inputDir}
last_char=${inputDir:length-1:1}
if [[ $last_char == "/" ]]; then
inputDir=${inputDir:0:length-1}
fi
# check if the given outputDir is a valid directory
isValidDirectory $outputDir "o"
# check if the given inputDir is a valid directory
isValidDirectory $inputDir "i"
# some var init
picmoved=0
minw=$(($minw-1))
# begin the process
wpFind $inputDir
# display final status
if [ "$pic2move" = "yes" ]; then
printf "$NORMAL""Pictures moved : ""$BOLDVERT""$picmoved\n""$NORMAL"
elif [ "$pic2move" = "no" ]; then
printf "$NORMAL""Pictures copied : ""$BOLDVERT""$picmoved\n""$NORMAL"
fi
# display additional informations with the status
if [ "$verbosemode" = "yes" ]; then
printf "$NORMAL""Minimal width : $(($minw+1))\n""$NORMAL"
printf "$NORMAL""Logfile : wpfind.log\n""$NORMAL"
printf "$NORMAL""Analyzed dir : $inputDir\n""$NORMAL"
printf "$NORMAL""Wallpapers dir : $outputDir\n""$NORMAL"
fi
printf "\n" >> $logfile
# display the time elapsed for the complete process
endProcess=$($date +'%s')
timeElapsed beginProcess endProcess
}
# no parameters
[[ $# -lt 1 ]] && usage && exit
while getopts ":c,m,h,v,s,i:,o:,w:" option; do
case "$option" in
c) pic2move=no ;;
m) pic2move=yes ;;
o) outputDir=$OPTARG ;;
i) inputDir=$OPTARG ;;
w) minw=$OPTARG ;;
h) usage; exit ;;
v) verbosemode=yes ;;
:) echo "Error : Option $OPTARG : missing argument"; usage; exit 1 ;;
\?) echo "Error : $OPTARG : invalid option"; usage; exit 1 ;;
esac
done
main
exit 0