-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
zImageOptimizer.sh
1776 lines (1509 loc) · 42.4 KB
/
zImageOptimizer.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Simple image optimizer for JPEG, PNG and GIF images.
# URL: https://github.com/zevilz/zImageOptimizer
# Author: Alexandr "zEvilz" Emshanov
# License: MIT
# Version: 0.10.6
sayWait()
{
local AMSURE
[ -n "$1" ] && echo "$@" 1>&2
read -n 1 -p "Press any key to continue..." AMSURE
echo "" 1>&2
}
cdAndCheck()
{
cd "$1" 2>/dev/null
if ! [ "$(pwd)" = "$1" ]; then
echo
$SETCOLOR_FAILURE
if [ -z "$2" ]; then
echo "Can't get up in a directory $1!" 1>&2
else
echo "$2" 1>&2
fi
$SETCOLOR_NORMAL
echo
exit 1
fi
}
checkDir()
{
if ! [ -d "$1" ]; then
echo
$SETCOLOR_FAILURE
if [ -z "$2" ]; then
echo "Directory $1 not found!" 1>&2
else
echo "$2" 1>&2
fi
$SETCOLOR_NORMAL
echo
exit 1
fi
}
checkDirPermissions()
{
cd "$1" 2>/dev/null
touch checkDirPermissions 2>/dev/null
if ! [ -f "$1/checkDirPermissions" ]; then
echo
$SETCOLOR_FAILURE
if [ -z "$2" ]; then
echo "Current user have no permissions to directory $1!" 1>&2
else
echo "$2" 1>&2
fi
$SETCOLOR_NORMAL
echo
exit 1
else
rm "$1/checkDirPermissions"
fi
}
checkParm()
{
if [ -z "$1" ]; then
echo
$SETCOLOR_FAILURE
echo "$2" 1>&2
$SETCOLOR_NORMAL
echo
exit 1
fi
}
inArray () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
installDeps()
{
PLATFORM="unknown"
PLATFORM_ARCH="unknown"
PLATFORM_SUPPORT=0
if [[ "$OSTYPE" == "linux-gnu" ]]; then
PLATFORM="linux"
PLATFORM_DISTRIBUTION="unknown"
PLATFORM_VERSION="unknown"
PLATFORM_PKG="unknown"
if [ $(uname -m) == 'x86_64' ]; then
PLATFORM_ARCH=64
else
PLATFORM_ARCH=32
fi
# First test against Fedora / RHEL / CentOS / generic Redhat derivative
if [ -r /etc/rc.d/init.d/functions ]; then
source /etc/rc.d/init.d/functions
[ zz`type -t passed 2>/dev/null` == "zzfunction" ] && PLATFORM_PKG="redhat"
PLATFORM_DISTRIBUTION=$(cat /etc/redhat-release | cut -d ' ' -f1)
if [ $PLATFORM_DISTRIBUTION == "Fedora" ]; then
PLATFORM_VERSION=$(grep -oE '[0-9]+' /etc/redhat-release)
if [ $PLATFORM_VERSION -ge $MIN_VERSION_FEDORA ]; then
PLATFORM_SUPPORT=1
fi
fi
if [ $PLATFORM_DISTRIBUTION == "Red" ]; then
RHEL_RECHECK=$(cat /etc/redhat-release | cut -d ' ' -f1-4)
if [ "$RHEL_RECHECK" == "Red Hat Enterprise Linux" ]; then
PLATFORM_DISTRIBUTION="RHEL"
PLATFORM_VERSION=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d '.' -f1)
if [ $PLATFORM_VERSION -ge $MIN_VERSION_RHEL ]; then
PLATFORM_SUPPORT=1
fi
fi
fi
if [ $PLATFORM_DISTRIBUTION == "CentOS" ]; then
PLATFORM_VERSION=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d '.' -f1)
if [ $PLATFORM_VERSION -ge $MIN_VERSION_CENTOS ]; then
PLATFORM_SUPPORT=1
fi
fi
# Then test against SUSE (must be after Redhat,
# I've seen rc.status on Ubuntu I think? TODO: Recheck that)
# elif [ -r /etc/rc.status ]; then
# source /etc/rc.status
# [ zz`type -t rc_reset 2>/dev/null` == "zzfunction" ] && PLATFORM_PKG="suse"
# Then test against Debian, Ubuntu and friends
elif [ -r /lib/lsb/init-functions ]; then
source /lib/lsb/init-functions
[ zz`type -t log_begin_msg 2>/dev/null` == "zzfunction" ] && PLATFORM_PKG="debian"
PLATFORM_DISTRIBUTION=$(lsb_release -i | cut -d ':' -f2 | sed 's/\s//')
PLATFORM_VERSION=$(lsb_release -r | cut -d ':' -f2 | sed 's/\s//' | sed 's/\..*//')
if [ $PLATFORM_DISTRIBUTION == "Debian" ]; then
if [ $PLATFORM_VERSION -ge $MIN_VERSION_DEBIAN ]; then
PLATFORM_SUPPORT=1
fi
fi
if [ $PLATFORM_DISTRIBUTION == "Ubuntu" ]; then
if [ $PLATFORM_VERSION -ge $MIN_VERSION_UBUNTU ]; then
PLATFORM_SUPPORT=1
fi
fi
# Then test against Gentoo
# elif [ -r /etc/init.d/functions.sh ]; then
# source /etc/init.d/functions.sh
# [ zz`type -t ebegin 2>/dev/null` == "zzfunction" ] && PLATFORM_PKG="gentoo"
# For Slackware we currently just test if /etc/slackware-version exists
# and isn't empty (TODO: Find a better way :)
# elif [ -s /etc/slackware-version ]; then
# PLATFORM_PKG="slackware"
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
PLATFORM="macos"
PLATFORM_PKG="dmg"
PLATFORM_DISTRIBUTION="MacOS"
PLATFORM_ARCH=$(getconf LONG_BIT)
PLATFORM_VERSION="$(defaults read loginwindow SystemVersionStampAsString)"
if [[ $(echo $PLATFORM_VERSION | cut -d '.' -f1) -ge $MIN_VERSION_MACOS ]]; then
PLATFORM_SUPPORT=1
fi
elif [[ "$OSTYPE" == "FreeBSD"* ]]; then
PLATFORM="freebsd"
PLATFORM_PKG="pkg"
PLATFORM_DISTRIBUTION="FreeBSD"
PLATFORM_ARCH=$(getconf LONG_BIT)
PLATFORM_VERSION=$(freebsd-version | cut -d '-' -f1)
if [[ $(echo $PLATFORM_VERSION | cut -d '.' -f1) -ge $MIN_VERSION_FREEBSD ]]; then
PLATFORM_SUPPORT=1
fi
fi
# Hook: after-check-platform
includeExtensions after-check-platform
if [ $DEBUG -eq 1 ]; then
echo "Platform info:"
echo
echo "PLATFORM: $PLATFORM"
echo "PLATFORM_PKG: $PLATFORM_PKG"
echo "PLATFORM_DISTRIBUTION $PLATFORM_DISTRIBUTION"
echo "PLATFORM_ARCH: $PLATFORM_ARCH"
echo "PLATFORM_VERSION: $PLATFORM_VERSION"
echo "PLATFORM_SUPPORT: $PLATFORM_SUPPORT"
echo
sayWait
fi
if [ $PLATFORM_SUPPORT -eq 1 ]; then
echo "Installing dependencies..."
# Hook: before-install-deps
includeExtensions before-install-deps
if [ $PLATFORM == "linux" ]; then
# Hook: before-install-deps-linux
includeExtensions before-install-deps-linux
if [ $PLATFORM_PKG == "debian" ]; then
# Hook: before-install-deps-debian
includeExtensions before-install-deps-debian
$SUDO apt-get update
$SUDO apt-get install $DEPS_DEBIAN -y
# Hook: after-install-deps-debian
includeExtensions after-install-deps-debian
elif [ $PLATFORM_PKG == "redhat" ]; then
# Hook: before-install-deps-redhat
includeExtensions before-install-deps-redhat
if [ $PLATFORM_DISTRIBUTION == "Fedora" ]; then
# Hook: before-install-deps-redhat-fedora
includeExtensions before-install-deps-redhat-fedora
$SUDO dnf install epel-release -y
$SUDO dnf install $DEPS_REDHAT -y
# Hook: after-install-deps-redhat-fedora
includeExtensions after-install-deps-redhat-fedora
elif [ $PLATFORM_DISTRIBUTION == "RHEL" ]; then
# Hook: before-install-deps-redhat-rhel
includeExtensions before-install-deps-redhat-rhel
$SUDO yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$PLATFORM_VERSION.noarch.rpm -y
echo
echo -n "Enabling rhel-$PLATFORM_VERSION-server-optional-rpms repository..."
$SUDO subscription-manager repos --enable rhel-$PLATFORM_VERSION-server-optional-rpms
$SUDO yum install $DEPS_REDHAT -y
# Hook: after-install-deps-redhat-rhel
includeExtensions after-install-deps-redhat-rhel
else
# Hook: before-install-deps-redhat-other
includeExtensions before-install-deps-redhat-other
$SUDO yum install epel-release -y
$SUDO yum install $DEPS_REDHAT -y
# Hook: after-install-deps-redhat-other
includeExtensions after-install-deps-redhat-other
fi
# Hook: after-install-deps-redhat
includeExtensions after-install-deps-redhat
if [[ $PLATFORM_DISTRIBUTION == "CentOS" && $PLATFORM_VERSION -eq 6 || $PLATFORM_DISTRIBUTION == "RHEL" && $PLATFORM_VERSION -eq 6 ]]; then
for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
if [ -f "${BINARY_PATHS_ARRAY[$p]}/pngcrush" ]; then
ISSET_pngcrush=1
fi
done
if ! [ -z $ISSET_pngcrush ] && [ $ISSET_pngcrush -eq 0 ]; then
wget https://downloads.sourceforge.net/project/pmt/pngcrush/old-versions/1.8/1.8.0/pngcrush-1.8.0.tar.gz
tar -zxvf pngcrush-1.8.0.tar.gz
rm pngcrush-1.8.0.tar.gz
cd pngcrush-1.8.0
make
$SUDO cp pngcrush /bin/
cd ../
rm -rf pngcrush-1.8.0
fi
for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
if [ -f "${BINARY_PATHS_ARRAY[$p]}/advpng" ]; then
ISSET_advpng=1
fi
done
if ! [ -z $ISSET_advpng ] && [ $ISSET_advpng -eq 0 ]; then
$SUDO yum install zlib-devel gcc-c++ -y
wget https://github.com/amadvance/advancecomp/releases/download/v2.0/advancecomp-2.0.tar.gz
tar -zxvf advancecomp-2.0.tar.gz
rm advancecomp-2.0.tar.gz
cd advancecomp-2.0
./configure
make
$SUDO make install
cd ../
rm -rf advancecomp-2.0
fi
fi
fi
# for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
# if [ -f "${BINARY_PATHS_ARRAY[$p]}/djpeg" ]; then
# ISSET_djpeg=1
# fi
# done
# for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
# if [ -f "${BINARY_PATHS_ARRAY[$p]}/cjpeg" ]; then
# ISSET_cjpeg=1
# fi
# done
# if [[ $ISSET_djpeg -eq 0 || $ISSET_cjpeg -eq 0 ]]; then
# git clone https://github.com/mozilla/mozjpeg.git
# cd mozjpeg/
# autoreconf -fiv
# ./configure
# if [ $PLATFORM_PKG == "debian" ]; then
# make deb
# $SUDO dpkg -i mozjpeg_*.deb
# else
# make
# $SUDO make install
# fi
# cd ../
# rm -rf mozjpeg
# fi
if ! [ -z $ISSET_pngout ] && [ $ISSET_pngout -eq 0 ]; then
wget http://static.jonof.id.au/dl/kenutils/pngout-20150319-linux.tar.gz
tar -xf pngout-20150319-linux.tar.gz
rm pngout-20150319-linux.tar.gz
if [ $PLATFORM_ARCH == 64 ]; then
$SUDO cp pngout-20150319-linux/x86_64/pngout /bin/pngout
else
$SUDO cp pngout-20150319-linux/i686/pngout /bin/pngout
fi
rm -rf pngout-20150319-linux
fi
# Hook: after-install-deps-linux
includeExtensions after-install-deps-linux
elif [ $PLATFORM == "macos" ]; then
# Hook: before-install-deps-macos
includeExtensions before-install-deps-macos
checkHomebrew
brew install $DEPS_MACOS
# Hook: after-install-deps-macos
includeExtensions after-install-deps-macos
elif [ $PLATFORM == "freebsd" ]; then
# Hook: before-install-deps-freebsd
includeExtensions before-install-deps-freebsd
# for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
# if [ -f "${BINARY_PATHS_ARRAY[$p]}/git" ]; then
# ISSET_git=1
# else
# ISSET_git=0
# fi
# done
# if [[ $ISSET_git -eq 0 ]]; then
# cd /usr/ports/devel/git/
# make BATCH=yes install clean
# fi
for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
if [ -f "${BINARY_PATHS_ARRAY[$p]}/wget" ]; then
ISSET_wget=1
else
ISSET_wget=0
fi
done
if [ $ISSET_wget -eq 0 ]; then
cd /usr/ports/ftp/wget/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_jpegoptim ] && [ $ISSET_jpegoptim -eq 0 ]; then
cd /usr/ports/graphics/jpegoptim/
make BATCH=yes install clean
fi
if ! [[ -z $ISSET_djpeg || -z $ISSET_cjpeg || -z $ISSET_jpegtran ]] && [[ $ISSET_djpeg -eq 0 || $ISSET_cjpeg -eq 0 || $ISSET_jpegtran -eq 0 ]]; then
cd /usr/ports/graphics/jpeg/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_pngcrush ] && [ $ISSET_pngcrush -eq 0 ]; then
cd /usr/ports/graphics/pngcrush/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_optipng ] && [ $ISSET_optipng -eq 0 ]; then
cd /usr/ports/graphics/optipng/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_advpng ] && [ $ISSET_advpng -eq 0 ]; then
cd /usr/ports/archivers/advancecomp/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_gifsicle ] && [ $ISSET_gifsicle -eq 0 ]; then
cd /usr/ports/graphics/gifsicle/
make BATCH=yes install clean
fi
if ! [ -z $ISSET_pngout ] && [ $ISSET_pngout -eq 0 ]; then
cd ~
wget http://static.jonof.id.au/dl/kenutils/pngout-20150319-bsd.tar.gz
tar -xf pngout-20150319-bsd.tar.gz
rm pngout-20150319-bsd.tar.gz
if [ $PLATFORM_ARCH == 64 ]; then
$SUDO cp pngout-20150319-bsd/amd64/pngout /bin/pngout
else
$SUDO cp pngout-20150319-bsd/i686/pngout /bin/pngout
fi
rm -rf pngout-20150319-bsd
fi
# Hook: after-install-deps-freebsd
includeExtensions after-install-deps-freebsd
fi
# Hook: after-install-deps
includeExtensions after-install-deps
else
echo "Your platform is not supported! Please install dependaces manually."
echo "Info: $GIT_URL#manual-installing-dependences"
echo
fi
}
checkBashVersion()
{
if [[ $(echo $BASH_VERSION | cut -d '.' -f1) -lt $BASH_MIN_VERSION ]]; then
echo
$SETCOLOR_FAILURE
echo "Detected unsupported version of bash - ${BASH_VERSION}!"
echo "${BASH_MIN_VERSION}.* required."
$SETCOLOR_NORMAL
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "1. Install new version and exit"
echo "0. Exit (default)"
echo
echo -n "Enter selection [0] > "
read item
case "$item" in
0) echo
echo "Exiting..."
echo
exit 0
;;
1) echo
installBashMacOS
echo "Exiting..."
echo
exit 0
;;
*) echo
echo "Exiting..."
echo
exit 0
;;
esac
else
echo
exit 0
fi
fi
}
installBashMacOS()
{
checkHomebrew
brew install bash
if [ -z $(grep '/usr/local/bin/bash' /private/etc/shells) ]; then
$SUDO bash -c "echo '/usr/local/bin/bash' >> /private/etc/shells"
fi
if [ -f '~/.bash_profile' ]; then
if [ -z $(grep 'alias bash="/usr/local/bin/bash"' ~/.bash_profile) ]; then
bash -c "echo 'alias bash=\"/usr/local/bin/bash\"' >> ~/.bash_profile"
fi
else
bash -c "echo 'alias bash=\"/usr/local/bin/bash\"' > ~/.bash_profile"
fi
bash -c 'alias bash="/usr/local/bin/bash"'
}
checkHomebrew()
{
for p in "${!BINARY_PATHS_ARRAY[@]}" ; do
if [ -f "${BINARY_PATHS_ARRAY[$p]}/brew" ]; then
ISSET_brew=1
else
ISSET_brew=0
fi
done
if [ $ISSET_brew -eq 0 ]; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
}
getTimeMarkerPath()
{
TIME_MARKER_PATH=$(echo "$TIME_MARKER_PATH" | sed 's/\/$//')
if [ -z $TIME_MARKER ]; then
if [ -z $TIME_MARKER_PATH ]; then
echo "$DIR_PATH/$TIME_MARKER_NAME"
else
echo "$TIME_MARKER_PATH/$TIME_MARKER_NAME"
fi
else
if [[ $TIME_MARKER == *\/* ]]; then
echo "$TIME_MARKER"
else
if [ -z $TIME_MARKER_PATH ]; then
echo "$DIR_PATH/$TIME_MARKER"
else
echo "$TIME_MARKER_PATH/$TIME_MARKER"
fi
fi
fi
}
checkUserTimeMarker()
{
if [[ $TIME_MARKER =~ ^-?.*\/$ ]]; then
echo
$SETCOLOR_FAILURE
echo "Time marker filename not set in given path!" 1>&2
$SETCOLOR_NORMAL
echo
exit 1
fi
}
checkTimeMarkerPermissions()
{
if [[ "$OSTYPE" == "darwin"* ]]; then
TIME_MARKER_MODIFIED=$(stat -t %s -f %m -- "$1")
else
TIME_MARKER_MODIFIED=$(date -r "$1" +%s)
fi
touch -m "$1" 2>/dev/null
if [[ "$OSTYPE" == "darwin"* ]]; then
TIME_MARKER_MODIFIED_NEW=$(stat -t %s -f %m -- "$1")
else
TIME_MARKER_MODIFIED_NEW=$(date -r "$1" +%s)
fi
if [ $TIME_MARKER_MODIFIED -eq $TIME_MARKER_MODIFIED_NEW ]; then
echo
$SETCOLOR_FAILURE
echo "Current user have no permissions to modify time marker!" 1>&2
$SETCOLOR_NORMAL
echo
exit 1
else
if date --version >/dev/null 2>/dev/null ; then
touch -t $(date '+%Y%m%d%H%M.%S' -d @$TIME_MARKER_MODIFIED) "$1" > /dev/null # GNU version of date
else
touch -t $(date -r $TIME_MARKER_MODIFIED +%Y%m%d%H%M.%S) "$1" > /dev/null # Non GNU version of date
fi
fi
}
updateTimeMarker()
{
if [ $NEW_ONLY -eq 1 ]; then
touch -m "$TIME_MARKER_FULL_PATH" > /dev/null
if [ $TIME_MARKER_ISSET -eq 1 ]; then
echo "Time marker updated."
else
echo "Time marker created."
fi
echo
fi
}
fixTimeMarker()
{
if [ $NEW_ONLY -eq 1 ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
TIME_MARKER_MODIFIED_TIME=$(stat -t %s -f %m -- "$TIME_MARKER_FULL_PATH")
else
TIME_MARKER_MODIFIED_TIME=$(date -r "$TIME_MARKER_FULL_PATH" +%s)
fi
TIME_MARKER_MODIFIED_TIME=$(echo "$TIME_MARKER_MODIFIED_TIME+1" | bc)
if date --version >/dev/null 2>/dev/null ; then
touch -t $(date '+%Y%m%d%H%M.%S' -d @$TIME_MARKER_MODIFIED_TIME) "$TIME_MARKER_FULL_PATH" > /dev/null # GNU version of date
else
touch -t $(date -r $TIME_MARKER_MODIFIED_TIME +%Y%m%d%H%M.%S) "$TIME_MARKER_FULL_PATH" > /dev/null # Non GNU version of date
fi
fi
}
updateModifyTime()
{
if [ $NEW_ONLY -eq 1 ]; then
touch "$IMAGE" -r "$TIME_MARKER_FULL_PATH" > /dev/null
fi
}
optimJpegoptim()
{
jpegoptim --strip-all "$1" > /dev/null
}
optimJpegtran()
{
jpegtran -progressive -copy none -optimize "$1" > /dev/null
}
optimXjpeg()
{
# decompress in temp file
djpeg -outfile "$TMP_PATH/$(basename "$1")" "$1" > /dev/null 2>/dev/null
if [ -f "$TMP_PATH/$(basename "$1")" ]; then
SIZE_CHECK=$(wc -c "$TMP_PATH/$(basename "$1")" | awk '{print $1}')
if [[ SIZE_CHECK -gt 0 ]]; then
# compress and replace original file if temp file exists and not empty
cjpeg -quality 95 -optimize -progressive -outfile "$1" "$TMP_PATH/$(basename "$1")" > /dev/null
fi
fi
# cleanup
if [ -f "$TMP_PATH/$(basename "$1")" ]; then
rm "$TMP_PATH/$(basename "$1")"
fi
}
optimPngcrush()
{
IMAGE="$1"
IMAGE_DIR=$(dirname "$IMAGE")
cd "$IMAGE_DIR"
pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -reduce -q -s -ow "$IMAGE" > /dev/null
}
optimOptipng()
{
OPTIPNG_V=$(optipng -v | head -n1 | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | cut -d '.' -f2)
if ! [ -z $OPTIPNG_V ]; then
if [ $OPTIPNG_V -ge 7 ]; then
optipng -strip all -o7 -q "$1" > /dev/null
else
optipng -o7 -q "$1" > /dev/null
fi
else
optipng -o7 -q "$1" > /dev/null
fi
}
optimPngout()
{
pngout -q -y -k0 -s0 "$1" > /dev/null
}
optimAdvpng()
{
advpng -z -4 "$1" > /dev/null
}
optimGifsicle()
{
gifsicle --optimize=3 -b "$1" > /dev/null
#gifsicle --optimize=3 --lossy=30 -b "$IMAGE" # for lossy optimize
}
optimJPG()
{
#if [[ $ISSET_djpeg -eq 1 && $ISSET_cjpeg -eq 1 ]]; then
# optimXjpeg "$1"
#fi
if [[ $ISSET_jpegoptim -eq 1 ]]; then
optimJpegoptim "$1"
fi
if [[ $ISSET_jpegtran -eq 1 ]]; then
optimJpegtran "$1"
fi
}
optimPNG()
{
if [[ $ISSET_pngcrush -eq 1 ]]; then
optimPngcrush "$1"
fi
if [[ $ISSET_optipng -eq 1 ]]; then
optimOptipng "$1"
fi
if [[ $ISSET_pngout -eq 1 ]]; then
optimPngout "$1"
fi
if [[ $ISSET_advpng -eq 1 ]]; then
optimAdvpng "$1"
fi
}
optimGIF()
{
if [[ $ISSET_gifsicle -eq 1 ]]; then
optimGifsicle "$1"
fi
}
readableSize()
{
if [ "$1" -ge 1000000000 ]; then
echo -n $(echo "scale=1; $1/1024/1024/1024" | bc | sed 's/^\./0./')"Gb"
elif [ "$1" -ge 1000000 ]; then
echo -n $(echo "scale=1; $1/1024/1024" | bc | sed 's/^\./0./')"Mb"
else
echo -n $(echo "scale=1; $1/1024" | bc | sed 's/^\./0./')"Kb"
fi
}
readableTime()
{
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d hours ' $H
(( $M > 0 )) && printf '%d minutes ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
printf '%d seconds\n' $S
}
findExclude()
{
if ! [ -z "$EXCLUDE_LIST" ]; then
EXCLUDE_LIST=$(echo $EXCLUDE_LIST | sed 's/,$//g' | sed 's/^,//g' | sed 's/,/\\|/g')
grep -v "$EXCLUDE_LIST"
else
grep -v ">>>>>>>>>>>>>"
fi
}
checkEnabledExtensions()
{
if ! [ -z "$ENABLED_EXTENSIONS" ]; then
cd "$SCRIPT_PATH"
if [ -d extensions ]; then
if [[ "$ENABLED_EXTENSIONS" != "all" ]]; then
if ! [[ "$ENABLED_EXTENSIONS" =~ ^[[:alnum:],_-]+$ ]]; then
echo
$SETCOLOR_FAILURE
echo "Wrong format of extensions list!"
$SETCOLOR_NORMAL
echo
exit 1
else
ENABLED_EXTENSIONS=$(echo $ENABLED_EXTENSIONS | sed 's/,$//g' | sed 's/^,//g' | sed 's/,/\ /g')
ENABLED_EXTENSIONS_ARR=($ENABLED_EXTENSIONS)
echo
echo "Checking selected extensions..."
for ENABLED_EXTENSION in ${ENABLED_EXTENSIONS_ARR[@]}; do
echo -n "${ENABLED_EXTENSION}..."
if ! [[ -z $(grep -lr "^#\ Extension:\ $ENABLED_EXTENSION$" extensions | tr '\n' ' ' | sed 's/\ $//') ]]; then
$SETCOLOR_SUCCESS
echo "[FOUND]"
$SETCOLOR_NORMAL
else
$SETCOLOR_FAILURE
echo "[NOT FOUND]"
$SETCOLOR_NORMAL
fi
done
fi
else
echo
echo "Enabled all extensions."
fi
else
echo
$SETCOLOR_FAILURE
echo "Extensions dir not found!"
$SETCOLOR_NORMAL
echo
exit 1
fi
fi
}
includeExtensions()
{
if ! [ -z "$ENABLED_EXTENSIONS" ]; then
cd "$SCRIPT_PATH"
if ! [ -z "$1" ] && [ -d extensions ]; then
local EXTF_LIST=$(grep -lr "^#\ Hook:\ $1$" extensions | tr '\n' ' ' | sed 's/\ $//')
if ! [ -z "$EXTF_LIST" ]; then
local EXTF_ARR=("$EXTF_LIST")
for EXTF in $EXTF_ARR; do
if [[ "$ENABLED_EXTENSIONS" == "all" ]]; then
. "$EXTF"
else
local EXTF_EXTENSION=$(grep -Eo '^#\ Extension:\ [[:alnum:]_-]+$' "$EXTF" | cut -d ' ' -f3)
if inArray "$EXTF_EXTENSION" "${ENABLED_EXTENSIONS_ARR[@]}"; then
. "$EXTF"
fi
fi
done
fi
fi
fi
}
joinBy()
{
local d=$1
shift
echo -n "$1"
shift
printf "%s" "${@/#/$d}"
}
lockDir()
{
if [ -f "${TMP_PATH}/${LOCK_FILE_NAME}" ]; then
sed "/^$/d" "${TMP_PATH}/${LOCK_FILE_NAME}" > "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" && \
mv "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" "${TMP_PATH}/${LOCK_FILE_NAME}"
echo "$DIR_PATH" >> "${TMP_PATH}/${LOCK_FILE_NAME}"
else
echo "$DIR_PATH" > "${TMP_PATH}/${LOCK_FILE_NAME}"
fi
}
unlockDir()
{
if [ -f "${TMP_PATH}/${LOCK_FILE_NAME}" ]; then
sed "/^$/d" "${TMP_PATH}/${LOCK_FILE_NAME}" > "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" && \
mv "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" "${TMP_PATH}/${LOCK_FILE_NAME}"
if [[ $(wc -l "${TMP_PATH}/${LOCK_FILE_NAME}" | sed 's/^[\ ]*//' | cut -d ' ' -f1) -gt 1 ]]; then
grep -v "^${DIR_PATH}$" "${TMP_PATH}/${LOCK_FILE_NAME}" > "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" && \
mv "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" "${TMP_PATH}/${LOCK_FILE_NAME}"
else
rm "${TMP_PATH}/${LOCK_FILE_NAME}"
fi
fi
}
checkDirLock()
{
if [ -f "${TMP_PATH}/${LOCK_FILE_NAME}" ]; then
sed "/^$/d" "${TMP_PATH}/${LOCK_FILE_NAME}" > "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" && \
mv "${TMP_PATH}/${LOCK_FILE_NAME}.tmp" "${TMP_PATH}/${LOCK_FILE_NAME}"
if [[ $(grep "^${DIR_PATH}$" "${TMP_PATH}/${LOCK_FILE_NAME}") == "$DIR_PATH" ]]; then
echo "The directory is already locked by another script run! Exiting..."
echo
exit 0
fi
fi
}
savePerms()
{
if [[ "$OSTYPE" == "linux-gnu" ]]; then
CUR_OWNER=$(stat -c "%U:%G" "$IMAGE")
CUR_PERMS=$(stat -c "%a" "$IMAGE")
else
CUR_OWNER=$(ls -l "$IMAGE" | awk '{print $3":"$4}')
CUR_PERMS=$(stat -f "%Lp" "$IMAGE")
fi
}
restorePerms()
{
chown $CUR_OWNER "$IMAGE"
chmod $CUR_PERMS "$IMAGE"
}
usage()
{
echo
echo "Usage: bash $0 [options]"
echo
echo "Simple image optimizer for JPEG, PNG and GIF images."
echo
echo "Options:"
echo
echo " -h, --help Shows this help."
echo
echo " -v, --version Shows script version."
echo
echo " -p <dir>, Specify full path to input directory with "
echo " --path=<dir> or without slash in the end of path."
echo
echo " -q, --quiet Execute script without any questions and users "
echo " actions."
echo
echo " -l, --less Don't show optimizing process."
echo
echo " -c, --check-only Check tools with an opportunity to install "
echo " dependences. All options will be ignored "
echo " with this option (except for -h|--help and "
echo " -v|--version)."
echo
echo " -t <period>, Period for which to look for files by last "
echo " --time=<period> modified time. Must be set in minutes (10m, 30m "
echo " etc.) or hours (1h, 10h etc.) or days (1d, 30d "
echo " etc.). It is impossible to use this option with "
echo " -n|--new-only option. (test)"
echo
echo " -n, --new-only Looking for images newer than special time "
echo " marker file. It is automatically created or "
echo " modified in the end of optimizing using this "
echo " option. Recommended for cron usage to avoid "
echo " repeated optimization already optimized files. "
echo " By default time marker creates in working "
echo " directory which set in -p|--path option. It is "
echo " impossible to use this option with -t|--time "
echo " option. (test)"
echo
echo " -m <name>, Custom full path or name of time marker file. "
echo " --time-marker=<name>, Must be name of file (for changes time marker "
echo " -m <path>, name) or full path for custom time marker file "
echo " --time-marker=<path> in custom directory. Working only with "
echo " -n|--new-only option. (test)"
echo
echo " -tmp <dir>, Custom directory path for temporary files. "
echo " --tmp-path=<dir> Default value located in TMP_PATH variable "
echo " (/tmp by default)"
echo
echo " -e <list>, Comma separated parts list of paths to files "
echo " --exclude=<list> for exclusion from search. The script removes "
echo " from the search files in the full path of which "
echo " includes any value from the list."
echo
echo " -ext <list>, Comma separated list of script's extensions to "
echo " --extensions=<list> enable. Script's extensions disabled by default. "
echo " Use \"all\" to enable all found extensions."
echo
echo " --unlock Manually delete target dir from lockfile if "
echo " previous script launch was interrupted "
echo " incorrectly or killed by system. You must use "
echo " this option with -p|--path option."
echo
}
# Define default script vars
BASH_MIN_VERSION=4
TMP_PATH="/tmp"
GIT_URL="https://github.com/zevilz/zImageOptimizer"
DEBUG=0
HELP=0
SHOW_VERSION=0
NO_ASK=0
LESS=0
CHECK_ONLY=0
PERIOD=0
NEW_ONLY=0
TIME_MARKER=""
EXCLUDE_LIST=""