-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-aras-cargo.sh
executable file
·4011 lines (3660 loc) · 179 KB
/
woocommerce-aras-cargo.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
#!/bin/bash
#
# shellcheck disable=SC2016,SC2015
#
# Copyright (C) 2021 Hasan CALISIR <[email protected]>
# Distributed under the GNU General Public License, version 2.0.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# WOOCOMMERCE - ARAS CARGO INTEGRATION
# ---------------------------------------------------------------------
# Written by : (hsntgm) Hasan ÇALIŞIR - [email protected]
# Version : 2.1.1
# --------------------------------------------------------------------
# Bash : 5.1.8
# WooCommerce : 5.5.1
# Wordpress : 5.7.2
# AST Plugin : 3.2.5
# Tested On : Ubuntu, Gentoo
# Year : 2021
# ---------------------------------------------------------------------
#
# The aim of this script is integration woocommerce and ARAS cargo.
# What is doing this script exactly?
# -This automation updates woocomerce order status processing to completed/shipped
# -(via WC REST), when the matching cargo tracking code is generated on the ARAS
# -cargo end (SOAP). Attachs cargo information (tracking number, track link etc.)
# -to order completed/shipped e-mail with the help of AST plugin (AST REST) and
# -notify customer. If you implemented two-way fulfillment workflow, script goes
# -one layer up and updates order status shipped to delivered and notify customer
# -via second mail. Simply you don't need to add cargo tracking number manually
# -and update order status via WooCommerce orders dashboard. The aim of script is
# -automating the process fully.
#
# Follow detailed installation instructions on github.
# =====================================================================
# @MISCELLANEOUS SETTINGS
# =====================================================================
export TZ='Europe/Istanbul'
renice 19 $$ > /dev/null 2> /dev/null
d_umask="$(umask)"
# Need for upgrade - DON'T EDIT MANUALLY
# =====================================================================
script_version="2.1.1"
# =====================================================================
# USER DEFINED-EDITABLE VARIABLES & OPTIONS
# =====================================================================
# Set estimated delivery time (default 5 day)
# Increase this value if sent packages take longer 5 days to reach the customer.
# If holidays and special days are the case increasing the value is recommended.
delivery_time=5
# Set levenshtein distance function approx. string matching (default max 3 characters)
# Setting higher values causes unexpected matching result
# If you miss the matches too much set higher values, max recommended is 4,5 char.
max_distance=3
# Main cron job schedule timer
# At every 24th minute past every hour from 9 through 19 on every day-of-week from Monday through Saturday.
cron_minute="*/30 9-19 * * 1-6"
# Updater cron job schedule timer
# At 09:19 on Sunday.
cron_minute_update="19 9 * * 0"
# Systemd job schedule timer
# At every 30th minute past every hour from 9 through 19 on every day-of-week from Monday through Saturday.
on_calendar="Mon..Sat 9..19:00/30:00 Europe/Istanbul"
# Logrotate configuration
# Keeping log file size small is important for performance (kb)
maxsize="35"
l_maxsize="35k"
# Set how many days you want to keep debug data
# Default 14 days
keep_debug="30"
# Logging paths
wooaras_log="/var/log/woo-aras/woocommerce_aras.log"
# Need for html mail template
company_name="E-Commerce Company"
company_domain="mycompany.com"
# Set ARAS cargo request date range --> last 10 days
# Supports Max 30 days.
# Keep date format!
t_date=$(date +%d/%m/%Y)
e_date=$(date +%d-%m-%Y -d "+1 days")
s_date=$(date +%d-%m-%Y -d "-10 days")
# Send mail command, adjust as you wish sendmail, mutt, ssmtp
send_mail_command="mail"
# Set 1 if you want to get error&success mails (recommended)
# Properly configure your mail server and send mail command before enable
# Set 0 to disable
send_mail_err="1"
# Set notify mail info
mail_to="order_info@${company_domain}"
mail_from="From: ${company_name} <aras_woocommerce@${company_domain}>"
mail_subject_suc="SUCCESS: WooCommerce - ARAS Cargo"
mail_subject_err="ERROR: WooCommerce - ARAS Cargo"
# Send mail functions
# If you use sendmail, mutt, ssmtp etc. you can adjust here with proper properties
send_mail_err () {
$send_mail_command -s "$mail_subject_err" -a "$mail_from" -a "MIME-Version: 1.0" -a "Content-Type: text/html; charset=UTF-8" "$mail_to"
}
send_mail_suc () {
$send_mail_command -s "$mail_subject_suc" -a "$mail_from" -a "MIME-Version: 1.0" -a "Content-Type: text/html; charset=UTF-8" "$mail_to"
}
# END
# =====================================================================
# This scripts adjusts itself according to the type of shell it is running
# Check shell is interactive or non-interactive
if [ -t 0 ]; then my_shell="interactive"; else my_shell="non-interactive"; fi
# Check smtp ports are listening to determine mail server running
if command -v lsof >/dev/null 2>&1; then
if lsof -Pi :587 -sTCP:LISTEN -t >/dev/null; then
check_mail_server=0
elif lsof -Pi :465 -sTCP:LISTEN -t >/dev/null; then
check_mail_server=0
elif lsof -Pi :2525 -sTCP:LISTEN -t >/dev/null; then
check_mail_server=0
elif lsof -Pi :25 -sTCP:LISTEN -t >/dev/null; then
check_mail_server=0
else
send_mail_err=0
check_mail_server=1
fi
elif command -v netstat >/dev/null 2>&1; then
if netstat -tulpn | grep -q ":25\|:587\|:465\|:2525"; then
check_mail_server=0
else
send_mail_err=0
check_mail_server=1
fi
elif command -v ss >/dev/null 2>&1; then
if ss -tulpn | grep -q ":25\|:587\|:465\|:2525"; then
check_mail_server=0
else
send_mail_err=0
check_mail_server=1
fi
fi
# Test connection & get public ip
if ! : >/dev/tcp/8.8.8.8/53; then
connection_error=1
else
# Get public IP
exec 3<> /dev/tcp/checkip.amazonaws.com/80
printf "GET / HTTP/1.1\r\nHost: checkip.amazonaws.com\r\nConnection: close\r\n\r\n" >&3
read -r my_ip < <(tail -n1 <&3)
fi
# Pretty send mail function
send_mail () {
if command -v "${send_mail_command}" >/dev/null 2>&1; then
if [[ "${send_mail_err}" -eq 1 && ! "${connection_error}" ]]; then
send_mail_err <<< "${1}" >/dev/null 2>&1
fi
fi
}
check_log () {
[[ -f "${wooaras_log}" ]] && return 0
return 1
}
# Notify broken installations via email
broken_installation () {
if [[ "${my_shell}" == "non-interactive" ]]; then
send_mail "${1}"
fi
}
# Log timestamp
timestamp () {
date +"%Y-%m-%d %T"
}
# Set terminal colors
setup_terminal () {
green=""; red=""; reset=""; cyan=""; magenta=""; yellow=""
bold=""; bgred=""; white=""
if [[ "${my_shell}" == "interactive" ]]; then
if command -v tput > /dev/null 2>&1; then
if [[ $(($(tput colors 2> /dev/null))) -ge 8 ]]; then
# Set color
green=$(tput setaf 2); red=$(tput setaf 1)
cyan=$(tput setaf 6); magenta=$(tput setaf 5); yellow=$(tput setaf 3)
white=$(tput setaf 7); bold=$(tput bold); bgred=$(tput setab 1); reset=$(tput sgr0)
fi
fi
BC=$'\e[32m'; EC=$'\e[0m'; m_tab=' '; m_tab_3=' '; m_tab_4=' '
else
return 1
fi
return 0
}
setup_terminal || echo > /dev/null
ugly_fatal () {
printf >&2 "\n${m_tab}%s ABORTED %s %s \n\n" "${bgred}${white}${bold}" "${reset}" "${*}"
exit 1
}
# Display usage instruction
usage () {
echo -e "\n${m_tab}${cyan}# WOOCOMMERCE - ARAS CARGO INTEGRATION BASIC USAGE${reset}"
echo -e "${m_tab}${magenta}---------------------------------------------------------------------${reset}"
echo -e "\n${m_tab}${cyan}# Clone git repository under /home/* (non-root user)${reset}"
echo -e "${m_tab}${magenta}git clone https://github.com/hsntgm/woocommerce-aras-kargo.git${reset}"
echo -e "\n${m_tab}${cyan}# Setup needs sudo privileges${reset}"
echo -e "${m_tab}${magenta}sudo ./woocommerce-aras-cargo.sh --setup${reset}"
echo -e "\n${m_tab}${cyan}# Uninstalling needs sudo privileges${reset}"
echo -e "${m_tab}${magenta}sudo ./woocommerce-aras-cargo.sh --uninstall${reset}"
echo -e "\n${m_tab}${cyan}# For all other options you can work without sudo privileges${reset}"
echo -e "${m_tab}${magenta}Check './woocommerce-aras-cargo.sh --help' for details.${reset}"
echo -e "\n${m_tab}${cyan}# For detailed installation instruction please visit github page.${reset}"
echo -e "${m_tab}${magenta}https://github.com/hsntgm/woocommerce-aras-kargo${reset}\n"
echo -e "${m_tab}${magenta}---------------------------------------------------------------------${reset}\n"
}
# Display script controls
help () {
echo -e "\n${m_tab}${cyan}# WOOCOMMERCE - ARAS CARGO INTEGRATION HELP"
echo -e "${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}#${m_tab}--setup |-s !!USE ${magenta}'woo-aras-setup.sh' ${cyan}instead of calling directly"
echo -e "${m_tab}#${m_tab}--twoway-enable |-t enable twoway fulfillment workflow"
echo -e "${m_tab}#${m_tab}--twoway-disable |-y only disable twoway fulfillment workflow without uninstall custom order status package as script will continue to work default one-way"
echo -e "${m_tab}#${m_tab}--disable |-i completely disable/inactivate script without uninstallation (for debugging purpose)"
echo -e "${m_tab}#${m_tab}--enable |-a enable/activate script if previously disabled"
echo -e "${m_tab}#${m_tab}--uninstall |-d NEED SUDO! completely remove installed bundles aka twoway custom order status package, cron jobs, systemd services, logrotate, logs"
echo -e "${m_tab}#${m_tab}--upgrade |-u upgrade script to latest version"
echo -e "${m_tab}#${m_tab}--options |-o show user defined/adjustable options currently in use"
echo -e "${m_tab}#${m_tab}--usage |-U display basic usage of this script"
echo -e "${m_tab}#${m_tab}--status |-S display automation status"
echo -e "${m_tab}#${m_tab}--dependencies |-p display prerequisites & dependencies"
echo -e "${m_tab}#${m_tab}--force-shipped |-f forces orders mark as shipped, useful If there are orders in the processing status although they have been delivered by Aras"
echo -e "${m_tab}#${m_tab}--debug-shipped |-g search logs in shipped data via search criteria [date] [ORDER_ID\|TRACKING_NUMBER]"
echo -e "${m_tab}#${m_tab}--debug-delivered |-z search logs in delivered data via search criteria [date] [ORDER_ID\|TRACKING_NUMBER]"
echo -e "${m_tab}#${m_tab}--version |-v display script info"
echo -e "${m_tab}#${m_tab}--help |-h display help"
echo -e "${m_tab}# ---------------------------------------------------------------------${reset}\n"
}
# Display script hard dependencies (may not included in default linux installations)
dependencies () {
echo -e "\n${m_tab}${magenta}# WOOCOMMERCE - ARAS CARGO INTEGRATION DEPENDENCIES"
echo -e "${m_tab}${magenta} (may not included in default linux installations)${reset}"
echo -e "${cyan}${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}# curl"
echo -e "${m_tab}# perl-Text::Fuzzy >= 0.29"
echo -e "${m_tab}# jq >= 1.6"
echo -e "${m_tab}# php"
echo -e "${m_tab}# php-soap"
echo -e "${m_tab}# gnu sed >= 4"
echo -e "${m_tab}# gnu awk >= 5"
echo -e "${m_tab}# whiptail (as also known (newt,libnewt))"
echo -e "${m_tab}# english locale"
echo -e "${m_tab}# ---------------------------------------------------------------------"
echo -e "\n${m_tab}${magenta}# WOOCOMMERCE - ARAS CARGO INTEGRATION RECOMMENDED TOOLS${reset}"
echo -e "${cyan}${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}# mail --> If you use mutt, ssmtp, sendmail etc. please edit user defined variables."
echo -e "${m_tab}# ---------------------------------------------------------------------"
echo -e "\n${m_tab}${magenta}# WOOCOMMERCE - ARAS CARGO INTEGRATION NEEDED APPLICATION VERSIONS${reset}"
echo -e "${cyan}${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}# Wordpress >= 5"
echo -e "${m_tab}# WooCommerce >= 5"
echo -e "${m_tab}# WooCommerce AST Plugin >= 3.2.5"
echo -e "${m_tab}# Bash >= 5"
echo -e "${m_tab}# ---------------------------------------------------------------------"
echo -e "\n${m_tab}${magenta}# WOOCOMMERCE - ARAS CARGO INTEGRATION REQUIREMENTS DURING SETUP${reset}"
echo -e "${cyan}${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}# WooCommerce REST API Key (v3)"
echo -e "${m_tab}# WooCommerce REST API Secret (v3)"
echo -e "${m_tab}# Wordpress Site URL (format in www.my-ecommerce.com)"
echo -e "${m_tab}# ARAS SOAP API Password"
echo -e "${m_tab}# ARAS SOAP API Username"
echo -e "${m_tab}# ARAS SOAP Endpoint URL (wsdl) (get from ARAS commercial user control panel)"
echo -e "${m_tab}# ARAS SOAP Merchant Code"
echo -e "${m_tab}# ---------------------------------------------------------------------${reset}\n"
}
# Version Info
version () {
echo -e "\n${m_tab}${cyan}# WOOCOMMERCE - ARAS CARGO INTEGRATION VERSION"
echo -e "${m_tab}# ---------------------------------------------------------------------"
echo -e "${m_tab}# Written by : Hasan ÇALIŞIR - [email protected]"
echo -e "${m_tab}# Version : 2.1.1"
echo -e "${m_tab}# Bash : 5.1.8"
echo -e "${m_tab}# ---------------------------------------------------------------------\n"
}
# Display user defined/adjustable options currently in use
options () {
echo ""
while read -r opt
do
[[ "$opt" =~ ^#.* ]] && opt_color="${cyan}" || opt_color="${magenta}"
echo -e "${opt_color}$(echo "$opt" | sed 's/^/ /')${reset}"
done < <(sed -n '/^# USER DEFINED/,/^# END/p' 2>/dev/null "${0}")
echo ""
}
# @EARLY CRITICAL CONTROLS --> means that no spend cpu time anymore
# =====================================================================
# Prevent errors cause by uncompleted upgrade
# Detect to make sure the entire script is available, fail if the script is missing contents
if [[ "$(tail -n 1 "${0}" | head -n 1 | cut -c 1-7)" != "exit \$?" ]]; then
echo -e "\n${red}*${reset} ${red}Script is incomplete, please force upgrade manually${reset}"
echo -e "${cyan}${m_tab}#####################################################${reset}\n"
help
broken_installation "Script is incomplete, please force upgrade manually"
check_log && echo "$(timestamp): Script is incomplete, please force upgrade manually" >> "${wooaras_log}"
exit 1
fi
# Check OS is supported
if [[ "$(uname -s)" != "Linux" ]]; then
echo -e "\n${red}*${reset} ${red}Unsupported operating system: $OSTYPE${reset}"
echo -e "${cyan}${m_tab}#####################################################${reset}\n"
broken_installation "Unsupported operating system: $OSTYPE"
check_log && echo "$(timestamp): Unsupported operating system: $OSTYPE" >> "${wooaras_log}"
exit 1
fi
# Accept only one argument except debug arguments
if [[ "${1}" != "-g" && "${1}" != "--debug-shipped" && "${1}" != "-z" && "${1}" != "--debug-delivered" ]]; then
[[ "${#}" -gt 1 ]] && { help; exit 1; }
fi
# Forward to setup script if user directly call main script with --setup
# Set env.ready if env. created
if [[ "${1}" == "-s" || "${1}" == "--setup" ]]; then
if [[ "${setup_key}" ]]; then
if [[ "$SUDO_USER" == "${new_user}" ]]; then
echo "${distribution} ${setup_key} ${new_user}" > "${working_path}/.env.ready"
chown "${new_user}":"${new_user}" "${working_path}/.env.ready"
chmod 600 "${working_path}/.env.ready"
check_log && echo "$(timestamp): Pre-Setup completed." >> "${wooaras_log}"
fi
else
echo -e "\n${red}*${reset} ${red}Forbidden action!${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${red}${m_tab}You cannot directly call main script with --setup${reset}"
echo -e "${red}${m_tab}Instead use setup script ${magenta}sudo ./woo-aras-setup.sh${reset}\n"
check_log && echo "$(timestamp): You cannot directly call main script with --setup" >> "${wooaras_log}"
exit 1
fi
fi
# For @SETUP && @UNINSTALL:
# Display usage for necessary privileges
if [[ "${1}" == "-s" || "${1}" == "--setup" || "${1}" == "-d" || "${1}" == "--uninstall" ]]; then
if [[ ! $SUDO_USER && $EUID -ne 0 ]]; then
usage
check_log && echo "$(timestamp): You need root privileges for setup/uninstall" >> "${wooaras_log}"
exit 1
fi
fi
# =====================================================================
# Guides placed at the beginning of the script
# User able to reach them without any break as much as possible
while :; do
case "${1}" in
-o|--options ) options
exit
;;
-U|--usage ) usage
exit
;;
-p|--dependencies ) dependencies
exit
;;
-v|--version ) version
exit
;;
-h|--help ) help
exit
;;
* ) break;;
esac
shift
done
# Early add local PATHS to deal with cron errors.
# We will also set explicit paths for binaries later.
export PATH="${PATH}:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
uniquepath () {
local path
path=""
while read -r
do
if [[ ! "${path}" =~ (^|:)"${REPLY}"(:|$) ]]; then
[[ "${path}" ]] && path="${path}:"
path="${path}${REPLY}"
fi
done < <(echo "${PATH}" | tr ":" "\n")
[[ "${path}" ]] && [[ "${PATH}" =~ /bin ]] && [[ "${PATH}" =~ /sbin ]] && export PATH="${path}"
}
uniquepath
# Path pretty error
script_path_pretty_error () {
echo -e "\n${red}*${reset} ${red}Could not determine script name and fullpath${reset}"
echo -e "${cyan}${m_tab}#####################################################${reset}\n"
broken_installation "Could not determine script name and fullpath"
check_log && echo "$(timestamp): Could not determine script name and fullpath" >> "${wooaras_log}"
exit 1
}
# Early discover script path
this_script_full_path="${BASH_SOURCE[0]}"
if command -v dirname >/dev/null 2>&1 && command -v readlink >/dev/null 2>&1 && command -v basename >/dev/null 2>&1; then
# Symlinks
while [[ -h "${this_script_full_path}" ]]; do
this_script_path="$( cd -P "$( dirname "${this_script_full_path}" )" >/dev/null 2>&1 && pwd )"
this_script_full_path="$(readlink "${this_script_full_path}")"
# Resolve
if [[ "${this_script_full_path}" != /* ]] ; then
this_script_full_path="${this_script_path}/${this_script_full_path}"
fi
done
this_script_path="$( cd -P "$( dirname "${this_script_full_path}" )" >/dev/null 2>&1 && pwd )"
this_script_name="$(basename "${this_script_full_path}")"
else
script_path_pretty_error
fi
if [[ ! "${this_script_full_path}" || ! "${this_script_path}" || ! "${this_script_name}" ]]; then
script_path_pretty_error
fi
# Enable extglob
# Remove trailing / (removes / and //) from script path
shopt -s extglob
this_script_path="${this_script_path%%+(/)}"
# PID File
PIDFILE="/var/run/woo-aras/woocommerce-aras-cargo.pid"
# Path pretty error
path_pretty_error () {
echo -e "\n${red}*${reset} ${red}Path not writable: ${1}${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo -e "${red}${m_tab}Run once with sudo to create path${reset}\n"
broken_installation "Broken installation, path not writable: ${1}, run once with sudo privileges to create path"
check_log && echo "$(timestamp): Path not writable: ${1}, run once with sudo to create path" >> "${wooaras_log}"
exit 1
}
missing_runtime_file_err () {
echo -e "\n${red}*${reset} ${red}Missing runtime file:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo -e "${m_tab}${red}Please re-start setup..${reset}\n"
broken_installation "Missing runtime file, please re-start setup"
check_log && echo "$(timestamp): Missing runtime file, please re-start setup" >> "${wooaras_log}"
exit 1
}
# Verify installation & make immutable calling script without argument till setup completed
if [[ $# -eq 0 ]]; then
if [[ ! -e "${this_script_path}/.woo.aras.set" ]]; then
echo -e "\n${red}*${reset} ${red}Broken/Uncompleted installation:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${m_tab}${red}Missing runtime file or you run the script first time${reset}"
echo -e "${m_tab}${red}Please re-start setup..${reset}\n"
usage
broken_installation "Broken/Uncompleted installation, please re-start setup"
check_log && echo "$(timestamp): Broken/Uncompleted installation, missing runtime file or you run the script first time" >> "${wooaras_log}"
exit 1
fi
fi
# Runtime protection
if [[ -e "${this_script_path}/.woo.aras.set" ]]; then
if [[ -e "${this_script_path}/.env.ready" ]]; then
uniq_id="$(cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address | tr -d ':')"
if [[ "${uniq_id}" != "$(< ${this_script_path}/.env.ready awk '{print $2}')" ]]; then
echo -e "\n${red}*${reset} ${red}Runtime protection!${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${red}${m_tab}The installation environment has changed.${reset}"
echo -e "${red}${m_tab}Please re-start setup..${reset}\n"
# Remove sensetive data immediately
rm -rf "${this_script_path:?}"/.lck/.*lck >/dev/null 2>&1
rm -rf "${this_script_path:?}"/{.woo.aras.set,.woo.aras.enb,.env.ready} >/dev/null 2>&1
broken_installation "Runtime protection, the installation environment has changed, please re-start setup"
check_log && echo "$(timestamp): Runtime protection, the installation environment has changed, please re-start setup" >> "${wooaras_log}"
exit 1
fi
else
missing_runtime_file_err
fi
fi
# Verify twoway installation content
if [[ "${1}" == "-s" || "${1}" == "--setup" ]]; then
if [[ "$(ls -A "${this_script_path}/custom-order-status-package" 2>/dev/null | wc -l)" -eq 0 ]]; then
echo -e "\n${red}*${reset} ${red}Missing installation content:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo -e "${m_tab}${red}Please re-start setup..${reset}\n"
usage
check_log && echo "$(timestamp): Missing installation content, please re-start setup" >> "${wooaras_log}"
exit 1
fi
fi
# Verify shop manager html email templates
if [[ "$(ls -A "${this_script_path}/emails" 2>/dev/null | wc -l)" -eq 0 ]]; then
usage
missing_runtime_file_err
fi
# Determine user & restrict calling critical functions before setup is complete
if [[ -s "${this_script_path}/.env.ready" ]]; then
user="$(< ${this_script_path}/.env.ready awk '{print $3}')"
if [[ ! "${user}" ]]; then
missing_runtime_file_err
fi
else
ugly_fatal "You cannot call this function [ ${1} ] before the installation is complete."
fi
# @FILE OPS
# Create file, drop file privileges back to non-root user if we got here with sudo
depriv () {
touched=0
local file my_path
for file in "${@}"
do
[[ "${file}" =~ "lck" ]] && { chown "${user}":"${user}" "${file}"; chmod 600 "${file}"; }
if [[ ! -f "${file}" ]]; then
touch "${file}"
chown "${user}":"${user}" "${file}"
[[ "${file}" == "${wooaras_log}" ]] && touched=1
elif [[ "$(stat --format "%U" "${file}" 2>/dev/null)" != "${user}" ]]; then
chown "${user}":"${user}" "${file}"
fi
done
}
# @FOLDER OPS
# Create folder, drop folder privileges back to non-root user if we got here with sudo
depriv_f () {
local my_path m_path
for m_path in "${@}"
do
# Determine the path type
[[ "${m_path}" =~ ^/run.* || "${m_path}" =~ ^/var.* ]] && my_path="system_path" || my_path="local_path"
# Start the operations
if [[ "${my_path}" == "system_path" ]]; then # Operations always need root privileges
if [[ ! -d "${m_path}" ]]; then
if [[ $SUDO_USER || $EUID -eq 0 ]]; then
mkdir "${m_path}" && chown "${user}":"${user}" "${m_path}" || path_pretty_error "${m_path}"
else
path_pretty_error "${m_path}"
fi
elif [[ $SUDO_USER || $EUID -eq 0 ]]; then
if [[ "$(stat --format "%U" "${m_path}" 2>/dev/null)" != "${user}" ]]; then
chown -R "${user}":"${user}" "${m_path}" || ugly_fatal "Cannot change ownership of path ${m_path}"
fi
elif [[ "$(stat --format "%U" "${m_path}" 2>/dev/null)" != "${user}" ]]; then
ugly_fatal "Cannot change ownership of path ${m_path}"
fi
elif [[ ! -d "${m_path}" ]]; then # Operations not always need root privileges
mkdir "${m_path}" >/dev/null 2>&1 && chown -R "${user}":"${user}" "${m_path}" || path_pretty_error "${m_path}"
fi
done
}
# Check, create runtime path (before logrotation prerotate rules called)
# Later, For @REBOOTS:
# -- We will create runtime folder via --> /etc/tmpfiles.d/ || rc.local for cron installation
# -- For systemd installation we will use 'RuntimeDirectory' so no need tmpfiles installation
runtime_path="${PIDFILE#/var}"
depriv_f "${runtime_path%/*}"
depriv "${PIDFILE}"
# Logrotation prerotate rules
my_rotate () {
# Not logrotate while script running
if [[ -f "${PIDFILE}" ]]; then
local PID
PID="$(< "${PIDFILE}")"
if ps -p "${PID}" > /dev/null 2>&1; then
exit 1
fi
fi
# Not logrotate until all shipped orders flagged as delivered otherwise data loss
if [[ -f "${this_script_path}/.two.way.set" ]]; then
if [[ "$(grep -c "SHIPPED" "${wooaras_log}")" -ne "$(grep -c "DELIVERED" "${wooaras_log}")" ]]; then
exit 1
fi
fi
# Create dummy process only while logrotation file size condition is met
if [[ -s "${wooaras_log}" ]]; then
filesize="$(du -k "${wooaras_log}" | cut -f1)"
if (( filesize > maxsize )); then
# Create dummy background process silently (PID will kill by logrotate)
# This will prevent executing script while logrotation triggering
{ perl -MPOSIX -e '$0="wooaras"; pause' & } 2>/dev/null
echo $! > "${PIDFILE}"
else
exit 1
fi
else
exit 1
fi
# Send signal to start logrotation
exit 0
}
# Reply the logrotate call
if [[ "${1}" == "--rotate" ]]; then my_rotate; fi
# Check & create .lck,tmp (local) & log (system) path
depriv_f "${wooaras_log%/*}" "${this_script_path}/tmp" "${this_script_path}/.lck"
depriv "${wooaras_log}"; [[ "${touched}" -eq 1 ]] && echo "$(timestamp): Log path created: Logging started.." >> "${wooaras_log}"
# Pid pretty error
pid_pretty_error () {
echo -e "\n${red}*${reset} ${red}FATAL ERROR: Couldn't create PID${reset}"
echo -e "${cyan}${m_tab}#####################################################${reset}\n"
echo "$(timestamp): FATAL ERROR: Couldn't create PID" >> "${wooaras_log}"
send_mail "FATAL ERROR: Couldn't create PID"
}
# Create PID before long running process
# Allow only one instance running at the same time
# Check how long actual process has been running to catch hang processes
if [[ -f "${PIDFILE}" ]]; then
PID="$(< "${PIDFILE}")"
if ps -p "${PID}" > /dev/null 2>&1; then
is_running=$(printf "%d" "$(($(ps -p "${PID}" -o etimes=) / 60))")
echo -e "\n${red}*${reset} ${red}The operation cannot be performed at the moment:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo -e "${m_tab}${red}As script already running --> ${magenta}pid=$PID${reset}${red}, please try again later${reset}\n"
send_mail "The operation cannot be performed at the moment: as script already running --> pid=$PID, please try again later"
# Warn about possible hang process
if [[ "${is_running}" -gt 30 ]]; then
echo -e "\n${red}*${reset} ${red}Possible hang process found:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo -e "${m_tab}${red}The process pid=$PID has been running for more than 30 minutes.${reset}\n"
send_mail "Possible hang process found: The process pid=$PID has been running for more than 30 minutes."
fi
exit 1
elif ! echo $$ > "${PIDFILE}"; then
pid_pretty_error
exit 1
fi
elif ! echo $$ > "${PIDFILE}"; then
pid_pretty_error
exit 1
fi
# Check dependencies & Set explicit paths
# =====================================================================
dynamic_vars () {
suffix="m"
eval "${suffix}"_"${1}"="$(command -v "${1}" 2>/dev/null)"
}
# Check dependencies
declare -a dependencies=("iconv" "awk" "sed" "stat" "${send_mail_command}"
"paste" "column" "mapfile" "readarray" "locale"
"find" "sort" "true" "false" "join" "systemctl"
"curl" "php" "jq" "perl" "openssl" "logrotate"
"whiptail")
missing_deps=()
for dep in "${dependencies[@]}"
do
if ! command -v "${dep}" >/dev/null 2>&1; then
missing_deps+=( "${dep}" )
if [[ "${dep}" == "php" ]]; then
missing_deps+=( "php-soap" )
elif [[ "${dep}" == "perl" ]]; then
missing_deps+=( "Text::Fuzzy" "App::cpanminus" )
fi
elif [[ "${dep}" == "php" ]]; then
dynamic_vars "$dep"
if ! $m_php -m | grep -q "soap"; then
missing_deps+=( "php-soap" )
fi
elif [[ "${dep}" == "perl" ]]; then
dynamic_vars "$dep"
if ! $m_perl -e 'use Text::Fuzzy;' >/dev/null 2>&1; then
if ! $m_perl -e 'use App::cpanminus;' >/dev/null 2>&1; then
missing_deps+=( "Text::Fuzzy" "App::cpanminus" )
else
missing_deps+=( "Text::Fuzzy" )
fi
fi
else
# Explicit paths for specific binaries used by script.
# Best practise to avoid cron errors is declare full path of binaries.
# I expect bash-builtin commands will not cause any cron errors.
# If you use specific linux distro and face cron errors please file a bug.
dynamic_vars "$dep"
fi
done
# Not break script but warn user about mail operations
if [[ "${missing_deps[@]}" =~ "${send_mail_command}" ]]; then
# Remove element from array to not break script
delete=( "${send_mail_command}" )
for target in "${delete[@]}"; do
for i in "${!missing_deps[@]}"; do
if [[ "${missing_deps[i]}" = "${target}" ]]; then
unset 'missing_deps[i]'
fi
done
done
echo -e "\n${yellow}*${reset} ${yellow}${send_mail_command} command not found.${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${yellow}${m_tab}You need running mail server with ${send_mail_command} command.${reset}"
if [[ "${check_mail_server}" -eq 1 ]]; then
echo "${yellow}${m_tab}Mail server not configured as SMTP port 587 is closed or not listening.${reset}"
fi
echo "${yellow}${m_tab}You can continue the setup but you cannot get important mail alerts${reset}"
fi
# Quit if missing dependencies found
if (( ${#missing_deps[@]} )); then
echo -e "\n${red}*${reset} ${red}Missing dependencies found: ${missing_deps[*]}${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${red}${m_tab}Please re-start setup.${reset}"
broken_installation "Missing dependencies found: ${missing_deps[*]}, please re-start setup."
echo "$(timestamp): Missing dependencies found: ${missing_deps[*]}, please re-start setup." >> "${wooaras_log}"
exit 1
fi
# We need column command from util-linux package, not from bsdmainutils
# Debian based distributions affected by this bug
# https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/1705437
if ! column -V 2>/dev/null | grep -q "util-linux"; then
[[ -f "/usr/local/bin/column2" ]] && my_column="/usr/local/bin/column2" || my_column="${this_script_path}/miscellaneous/column2"
else
my_column="$m_column"
fi
# Prevent iconv text translation errors
# Set locale category for character handling functions (otherwise this script not work correctly)
m_ctype=$(locale | grep LC_CTYPE | cut -d= -f2 | cut -d_ -f1 | tr -d '"')
if [[ "${m_ctype}" != "en" ]]; then
if locale -a | grep -iq "en_US.utf8"; then
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
lang_setted=1
else
echo -e "\n${red}*${reset} ${red}English locale not found${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${red}${m_tab}Please add support on English locale for your shell environment.${reset}"
echo -e "${yellow}${m_tab}e.g. for ubuntu -> apt-get -y install language-pack-en${reset}\n"
echo "$(timestamp): English locale not found, please add support on English locale for your shell environment." >> "${wooaras_log}"
broken_installation "English locale not found, please add support on English locale for your shell environment."
exit 1
fi
fi
# =====================================================================
# Late call connection error
if [[ "${connection_error}" ]]; then
echo -e "\n${red}*${reset} ${red}There is no internet connection.${reset}"
echo -e "${cyan}${m_tab}#####################################################${reset}\n"
echo "$(timestamp): There is no internet connection" >> "${wooaras_log}"
exit 1
fi
# Create temporary files
my_tmp=$(mktemp)
my_tmp_del=$(mktemp)
# Listen exit signals to destroy temporary runtime files
trap clean_up 0 1 2 3 6 15
clean_up () {
# Remove temporary runtime files
rm -f "${this_script_path:?}"/aras_request.php >/dev/null 2>&1
rm -rf "${this_script_path:?}"/*.en >/dev/null 2>&1
rm -rf "${this_script_path:?}"/{*proc*,.*proc} >/dev/null 2>&1
rm -rf "${this_script_path:?}"/{*json*,.*json} >/dev/null 2>&1
rm -rf "${this_script_path:?}"/.lvn* >/dev/null 2>&1
rm -rf ${my_tmp:?} ${my_tmp_del:?} ${PIDFILE:?} >/dev/null 2>&1
# Removes debug data that keeped in /tmp, older than user defined setting --> keep_debug
if ! [[ -e "${this_script_path}/.woo.aras.set" ]]; then
# Setup not completed so we need to check find version here
find_ver="$(find --version | grep GNU | $m_perl -pe '($_)=/([0-9]+([.][0-9]+)+)/')"
find_ver="${find_ver%.*}"
if [[ "${find_ver//.}" -ge 45 ]]; then
find "${this_script_path:?}/tmp" -maxdepth 1 -type f -mtime +"${keep_debug}" -print0 2>/dev/null | xargs -r0 rm --
fi
else
# Already passed pre check controls as setup completed
find "${this_script_path:?}/tmp" -maxdepth 1 -type f -mtime +"${keep_debug}" -print0 2>/dev/null | xargs -r0 rm --
fi
# Unset locale if setted before
if [[ "${lang_setted}" ]]; then
unset LANG
unset LC_ALL
fi
# Set default umask if changed
[[ "$(umask)" != "${d_umask}" ]] && umask "${d_umask}"
}
# Global Variables
# =====================================================================
cron_dir="/etc/cron.d"
cron_filename="woocommerce_aras"
cron_filename_update="woocommerce_aras_update"
cron_user="${user}"
systemd_user="${user}"
cron_script_full_path="${this_script_path}/${this_script_name}"
systemd_dir="/etc/systemd/system"
service_filename="woocommerce_aras.service"
timer_filename="woocommerce_aras.timer"
my_bash="$(command -v bash 2> /dev/null)"
systemd_script_full_path="${this_script_path}/${this_script_name}"
logrotate_dir="/etc/logrotate.d"
logrotate_conf="/etc/logrotate.conf"
logrotate_filename="woocommerce_aras"
sh_github="https://raw.githubusercontent.com/hsntgm/woocommerce-aras-kargo/main/woocommerce-aras-cargo.sh"
changelog_github="https://raw.githubusercontent.com/hsntgm/woocommerce-aras-kargo/main/CHANGELOG"
sh_output="${this_script_path}/woocommerce-aras-cargo.sh.tmp"
update_script="woocommerce-aras-update-script.sh"
my_tmp_folder="${this_script_path}/tmp"
my_string="woocommerce-aras-cargo-integration"
tmpfiles_d="/etc/tmpfiles.d"
tmpfiles_f="woo-aras.conf"
this_script_lck_path="${this_script_path}/.lck"
# Force remove any number of trailing slash from paths
# Assigning folder paths to variable with or without trailing slash is old topic.
cron_dir="${cron_dir%%+(/)}"
systemd_dir="${systemd_dir%%+(/)}"
logrotate_dir="${logrotate_dir%%+(/)}"
logrotate_conf="${logrotate_conf%%+(/)}"
my_tmp_folder="${my_tmp_folder%%+(/)}"
tmpfiles_d="${tmpfiles_d%%+(/)}"
this_script_lck_path="${this_script_lck_path%%+(/)}"
# =====================================================================
# Not allow -x that expose sensetive informations
# Disable to write history
hide_me () {
if [[ "${1}" == "--enable" ]]; then
if [[ $- =~ x ]]; then my_debug=1; set +x; fi
set +o history
elif [[ "${1}" == "--disable" ]]; then
[[ "${my_debug}" -eq 1 ]] && set -x
set -o history
fi
}
# Twoway pretty error
twoway_pretty_error () {
echo -e "\n${red}*${reset} ${red}Two way fulfillment workflow installation aborted:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${m_tab}${red}Couldn't copy ${i##*/} to${reset}"
echo -e "${m_tab}${red}${i%/*}${reset}\n"
echo "$(timestamp): Couldn't copy ${i##*/} to ${i%/*}" >> "${wooaras_log}"
exit 1
}
validate_twoway () {
# Collect missing files if exist
missing_files=()
for i in "${my_files[@]}"
do
if ! grep -qw "${my_string}" "${i}"; then
missing_files+=("${i}")
fi
done
if ! grep -qw "${my_string}" "${absolute_child_path}/functions.php"; then
missing_files+=("${absolute_child_path}/functions.php")
fi
! (( ${#missing_files[@]} ))
}
check_delivered () {
local w_delivered
w_delivered=$($m_curl -s -X GET -K- <<< "-u ${api_key}:${api_secret}" -H "Content-Type: application/json" "https://$api_endpoint/wp-json/wc/v3/orders?status=delivered")
if ! grep -q "rest_invalid_param" <<< "${w_delivered}"; then
echo -e "\n${yellow}*${reset} ${yellow}WARNING: Two way fulfillment workflow installation:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
echo "${m_tab}${yellow}You already have 'delivered' custom order status${reset}"
echo "${m_tab}${yellow}If you are doing the initial setup for the first time, ${red}!!!DON'T CONTINUE!!!${reset}"
echo -e "${m_tab}${yellow}If you have integrated 'delivered' custom order status via this script before, ${green}IT'S OK TO CONTINUE${reset}\n"
while true; do
echo "${cyan}${m_tab}#####################################################${reset}"
read -r -n 1 -p "${m_tab}${BC}Do you want to continue setup? --> (Y)es | (N)o${EC} " yn < /dev/tty
echo ""
case "${yn}" in
[Yy]* ) break;;
[Nn]* ) exit 1;;
* ) echo -e "\n${m_tab}${magenta}Please answer yes or no.${reset}";;
esac
done
fi
}
# Use in static functions which needs REST ops
curl_helper () {
hide_me --enable
if [[ ! "${api_key}" || ! "${api_secret}" || ! "${api_endpoint}" ]]; then
api_key=$(< "${this_script_lck_path}/.key.wc.lck" openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:"$(< ${this_script_path}/.env.ready $m_awk '{print $2}')" 2>/dev/null)
api_secret=$(< "${this_script_lck_path}/.secret.wc.lck" openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:"$(< ${this_script_path}/.env.ready $m_awk '{print $2}')" 2>/dev/null)
api_endpoint=$(< "${this_script_lck_path}/.end.wc.lck" openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:"$(< ${this_script_path}/.env.ready $m_awk '{print $2}')" 2>/dev/null)
fi
hide_me --disable
}
#
zgrep_helper () {
if [[ "$(find "${wooaras_log%/*}/" -name \*.log* -type f | wc -l)" -gt 1 ]]; then
find "${wooaras_log%/*}/" -name \*.log* -print0 2>/dev/null |
xargs -0 zgrep -ci "$1" |
$m_awk 'BEGIN {cnt=0;FS=":"}; {cnt+=$2;}; END {print cnt;}'
else
find "${wooaras_log%/*}/" -name \*.log* -print0 2>/dev/null | xargs -0 zgrep -ci "$1"
fi
}
# Get total processed orders via automation (include rotated logs)
# Variables in this function not visible by rest of script (caused by pipe) So no! need to set variables locally
statistics () {
echo -e "\n${green}*${reset} ${magenta}Automation Statistics:${reset}"
echo "${cyan}${m_tab}#####################################################${reset}"
if command -v zgrep >/dev/null 2>&1; then
if [[ -s "${wooaras_log}" ]]; then
currentime_d=$(date +"%T,%d-%b-%Y" | awk -F, '{print $2}')
today_new_order=$(grep -c $(date +%Y-%m-%d) < <(echo "${date_created[@]}"))
today_processed=$(grep -c "SHIPPED" < <(grep "${currentime_d}" "${wooaras_log}"))
total_processed=$(zgrep_helper "SHIPPED")
if [[ "${ts_status}" == "Completed" ]]; then
today_processed_del=$(grep -c "DELIVERED" < <(grep "${currentime_d}" "${wooaras_log}"))
total_processed_del=$(zgrep_helper "DELIVERED")
fi
{ # Start redirection
echo "${green}Today_New_Order: ${magenta}${today_new_order}${reset}"
echo "${green}Today_Shipped: ${magenta}${today_processed}${reset}"
echo "${green}Total_Shipped: ${magenta}${total_processed}${reset}"
echo "${green}Awaiting_Shipment: ${magenta}${w_processing}${reset}"
if [[ "${ts_status}" == "Completed" ]]; then
echo "${green}Today_Delivered: ${magenta}${today_processed_del}${reset}"
echo "${green}Total_Delivered: ${magenta}${total_processed_del}${reset}"
echo "${green}Awaiting_Delivery: ${magenta}$((total_processed-total_processed_del))${reset}"
fi
} > "${this_script_path}/.stat.proc" # NOTE: End redirection to file
$my_column -o ' ' -t -s ' ' <<< "$(< "${this_script_path}/.stat.proc")" | $m_sed 's/^/ /'
# Awaiting delivery(long pending) statistics
if [[ $(( total_processed-total_processed_del )) -gt 0 ]]; then
declare -a waiting_del_ids
waiting_del_ids=( "$(comm -23 <(find "${wooaras_log%/*}/" -name \*.log* -print0 2>/dev/null | xargs -0 zgrep -i "SHIPPED" | cut -d : -f 2- | awk '{print $6}' | awk -F= '{print $2}' | sort -n) <(find "${wooaras_log%/*}/" -name \*.log* -print0 2>/dev/null | xargs -0 zgrep -i "DELIVERED" | cut -d : -f 2- | awk '{print $6}' | awk -F= '{print $2}' | sort -n))" )
if ! [[ -e "${this_script_path}/long.proc.del" ]]; then # These are always appended file and trap(cleanup) can fail
for id in "${waiting_del_ids[@]}"; do