-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex-pdf.sh
executable file
·65 lines (61 loc) · 3.07 KB
/
ex-pdf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /bin/bash
[ -d "./PDF" ] && echo -e "\e[1;31mPDF folder already exists!!\nABORTING!\e[0m" && exit 200
mkdir "./PDF" || exit 220
OIFS="$IFS"
IFS=$'\n'
function converter() {
local dir="$1"
local type="$2"
echo working in $dir
if [[ "$type" == "JPEG" ]]; then
find $dir -name '*.jp*' -print0 | sort -z -V | xargs -0r img2pdf -s 1080x2400 --rotation=ifvalid --fit into -o "./PDF/$(basename $dir)".pdf;
elif [[ "$type" == "PNG" ]]; then
find $dir -name '*.png' -print0 | sort -z -V | xargs -0r img2pdf -s 1080x2400 --rotation=ifvalid --fit into -o "./PDF/$(basename $dir)".pdf;
else
echo -e "Somefn's gone horribly wrong! Fuck this!"
exit 1
fi
}
if [[ "$#" == 0 ]] || [[ "$@" == *~F* ]]; then
[ "$#" -gt 1 ] && echo "do not use other options with 'F'" && exit 1
for dir in $(find * -maxdepth 0 -type d); do
count_JP=$(find "$dir" -maxdepth 1 -name '*.jp*' -type f -printf '.' | wc -c)
count_PNG=$(find "$dir" -maxdepth 1 -name '*.png' -type f -printf '.' | wc -c)
echo -e "No. of JPG/JPEG files\t$count_JP\nNo. of PNG files\t$count_PNG\n"
if [[ $count_JP != 0 && $count_PNG != 0 ]]; then
( echo -e "\nSomething is wrong!!!\nCheck $dir\nJPG=$count_JP\tPNG=$count_PNG\nno. of sub-directories in $dir=$(find $dir/* -maxdepth 0 -type d | wc -l)\nDiagnosis:\tBOTH JPG and PNG files exist in \" $dir \"" && break )
elif [[ $count_JP != 0 && $count_PNG == 0 ]]; then
converter "$dir" "JPEG"
elif [[ $count_PNG != 0 && $count_JP == 0 ]]; then
converter "$dir" "PNG"
else
echo -e "The directory \" $dir \" is empty!\nYou may need to check the name of directory.\nPaths with '[]' are not allowed.\n"
[[ "$@" == *~F* ]] && echo -e "\nLoop override set! Proceeding to next directory!" && continue
echo set '$1'=~F to Override Loop! && break
fi
done
exit
fi
[ "$#" -gt 2 ] && echo too many arguments && exit 1
[ "$1" == "." ] && dir=${PWD} || dir="$1"
if [ -d "$dir" ]; then
echo -e "\e[1;34mfound path\e[0m $dir"
if [[ $(find "$dir" -type f -name "*.jpg" | wc -l) -eq $(find "$dir" -type f | wc -l) ]]; then
echo jpeg files found
converter "$dir" "JPEG"
echo -e "\nJPG/JPEG:$(find "$dir" -name "*.jpg" -type f | wc -l)\tDIR = $dir \t Completed"
elif [[ $(find "$dir" -type f -name "*.png" | wc -l) -eq $(find "$dir" -type f | wc -l) ]]; then
echo png files found
converter "$dir" "PNG"
echo -e "\nPNG:$(find "$dir" -name "*.png" -type f | wc -l)\tDIR = $dir \t Completed"
else
echo -e "\n\n\e[1;31mCannot proceed!\nABORTING!\n\nOTHER FILES ARE PRESENT IN THE DIRECTORY!!\e[0m\nHELP:\n\tConvert all the images to PNG or JPG first or use -both flag!\n\n\tFiles of other types may be present in the folder."
exit 1
fi
echo EOF 1 && exit
fi
IFS="$OIFS"
echo EOF 2; exit
# This file is a part of REN.sh suite and is packaged along with it.
# All Licensing terms and conditions of REN.sh will apply to this file/program also.
# Copyright (C) 2023 devlinman