better options management + interactive mode from cp/mv added

This commit is contained in:
Francois B. (Makoto) 2018-06-24 01:58:36 +02:00
parent a1b1ce6683
commit f9fc889ac1

98
wpfind
View file

@ -7,10 +7,10 @@
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# app version number # app version number
version="0.1.2" version="0.1.3"
# logfile # logfile
logfile="wpfind.log" logfile="wpfind.log"
touch wpfind.log touch $logfile
# script dir # script dir
currentDir=$(pwd) currentDir=$(pwd)
# bin date # bin date
@ -20,6 +20,7 @@ outputDir=$currentDir/wpfiles
inputDir=$currentDir inputDir=$currentDir
verbosemode=no verbosemode=no
minw="1920" minw="1920"
iMode=""
# colors const used in the script # colors const used in the script
UNDERLINE=$(tput sgr 0 1) UNDERLINE=$(tput sgr 0 1)
@ -42,9 +43,9 @@ BOLDCYAN=${BOLD}${CYAN}
BOLDBLANC=${BOLD}${BLANC} BOLDBLANC=${BOLD}${BLANC}
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# show help & informations # show app logo
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
function usage() { function logo() {
printf "\n" printf "\n"
printf "$BOLDJAUNE"; printf "$BOLDJAUNE";
printf "██╗ ██╗██████╗ ███████╗██╗███╗ ██╗██████╗ \n"; printf "██╗ ██╗██████╗ ███████╗██╗███╗ ██╗██████╗ \n";
@ -62,6 +63,13 @@ function usage() {
printf "Author : Francois B (Makoto)\n" printf "Author : Francois B (Makoto)\n"
printf "Licence : GPLv3\n" printf "Licence : GPLv3\n"
printf "\n" printf "\n"
}
#------------------------------------------------------------------------------
# show help & informations
#------------------------------------------------------------------------------
function usage() {
printf "$NORMAL"
printf "Usage : wpfind [options]\n" printf "Usage : wpfind [options]\n"
printf " -c : copy wallpapers found in the specified directory\n" printf " -c : copy wallpapers found in the specified directory\n"
printf " -m : move wallpapers found in the specified directory\n" printf " -m : move wallpapers found in the specified directory\n"
@ -91,12 +99,10 @@ function timeElapsed () { #beginProcess endProcess
function depInstalled () { function depInstalled () {
if ! which detox >/dev/null; then if ! which detox >/dev/null; then
printf "$NORMAL""Detox installed : ""$BOLDROUGE""NO""$NORMAL""\n" printf "$NORMAL""Detox installed : ""$BOLDROUGE""NO""$NORMAL""\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1 exit 1
fi fi
if ! which identify >/dev/null; then if ! which identify >/dev/null; then
printf "$NORMAL""Imagemagick installed : ""$BOLDROUGE""NO""$NORMAL""\n" printf "$NORMAL""Imagemagick installed : ""$BOLDROUGE""NO""$NORMAL""\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1 exit 1
fi fi
} }
@ -107,13 +113,11 @@ function depInstalled () {
function isValidDirectory() { #directory #I/O function isValidDirectory() { #directory #I/O
# check if the given outputDir is a valid directory # check if the given outputDir is a valid directory
if [[ ! -d $1 ]]; then if [[ ! -d $1 ]]; then
printf "$NORMAL""-$2 setting is not a valid directory\n" printf "$BOLDROUGE""Error" "$NORMAL"" : -$2 setting is not a valid directory\n"
printf "$NORMAL""Bye...\n""$NORMAL"
exit 1 exit 1
fi fi
} }
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# find wp # find wp
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -121,18 +125,11 @@ function wpFind () {
# cleaning filenames using detox # cleaning filenames using detox
detox -r $1/* &>> $logfile 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 # browse recursively files and dir
for pic in $1/*; do for pic in $1/*; do
# if directory # if directory
if [ -d "$pic" ]; then if [ -d "$pic" ]; then
# if not outputDir # and if not outputDir
if [ "$pic" != "$outputDirToCheck" ]; then if [ "$pic" != "$outputDirToCheck" ]; then
# browse sub-dir # browse sub-dir
wpFind "$pic" wpFind "$pic"
@ -151,20 +148,20 @@ function wpFind () {
if [ "$pic2move" = "no" ]; then if [ "$pic2move" = "no" ]; then
# if verbose mode # if verbose mode
if [ "$verbosemode" = "yes" ]; then if [ "$verbosemode" = "yes" ]; then
printf "cp $pic $outputDir\n" printf "cp $iMode $pic $outputDir\n"
fi fi
cp $pic $outputDir cp $iMode $pic $outputDir
date2log=$($date +'%Y-%m-%d %H:%M:%S') date2log=$($date +'%Y-%m-%d %H:%M:%S')
printf "$date2log - cp $pic $outputDir\n" >> $logfile printf "$date2log - cp $iMode $pic $outputDir\n" >> $logfile
# if move mode # if move mode
elif [ "$pic2move" = "yes" ]; then elif [ "$pic2move" = "yes" ]; then
# if verbose mode # if verbose mode
if [ "$verbosemode" = "yes" ]; then if [ "$verbosemode" = "yes" ]; then
printf "mv $pic $outputDir\n" printf "mv $iMode $pic $outputDir\n"
fi fi
mv $pic $outputDir mv $iMode $pic $outputDir
date2log=$($date +'%Y-%m-%d %H:%M:%S') date2log=$($date +'%Y-%m-%d %H:%M:%S')
printf "$date2log - mv $pic $outputDir\n" >> $logfile printf "$date2log - mv $iMode $pic $outputDir\n" >> $logfile
fi fi
picmoved=$(($picmoved+1)) picmoved=$(($picmoved+1))
fi fi
@ -233,23 +230,58 @@ function main() {
timeElapsed beginProcess endProcess timeElapsed beginProcess endProcess
} }
#------------------------------------------------------------------------------
# no parameters # no parameters
[[ $# -lt 1 ]] && usage && exit [[ $# -lt 1 ]] && logo && usage && exit
while getopts ":c,m,h,v,s,i:,o:,w:" option; do while getopts ":c,m,h,v,s,i:,o:,w:" option; do
case "$option" in case "$option" in
c) pic2move=no ;; c)
m) pic2move=yes ;; pic2move=no
o) outputDir=$OPTARG ;; ;;
i) inputDir=$OPTARG ;; m)
w) minw=$OPTARG ;; pic2move=yes
h) usage; exit ;; ;;
v) verbosemode=yes ;; o)
:) echo "Error : Option $OPTARG : missing argument"; usage; exit 1 ;; outputDir=$OPTARG
\?) echo "Error : $OPTARG : invalid option"; usage; exit 1 ;; ;;
i)
inputDir=$OPTARG
;;
w)
minw=$OPTARG
;;
h)
logo
usage
exit 0
;;
s)
iMode="-i"
;;
v)
verbosemode=yes
;;
:)
printf "$BOLDROUGE""Error""$NORMAL : Option $OPTARG : missing argument\n\n"
usage
exit 1
;;
\?)
printf "$BOLDROUGE""Error""$NORMAL"" : $OPTARG : invalid option\n\n"
usage
exit 1
;;
esac esac
done done
# if no mode choosed but other options set
if [ -z ${pic2move} ]; then
printf "$BOLDROUGE""Error""$NORMAL"" : please use copy mode (-c) or move mode (-m)\n\n"
exit 1
fi
main main
exit 0 exit 0