iredmail / iRedMail / conf / core

  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
#!/usr/bin/env bash

# Author:   Zhang Huangbin (zhb _at_ iredmail.org)

#---------------------------------------------------------------------
# This file is part of iRedMail, which is an open source mail server
# solution for Red Hat(R) Enterprise Linux, CentOS, Debian and Ubuntu.
#
# iRedMail 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.
#
# iRedMail 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 iRedMail.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------

ECHO_INFO()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[42m${_INFO_FLAG}\033[0m $@"
        else
            echo -ne "${_INFO_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[42m${_INFO_FLAG}\033[0m $@"
        else
            echo -e "${_INFO_FLAG} $@"
        fi
    fi
}

ECHO_SKIP()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[42m${_SKIP_FLAG}\033[0m $@"
        else
            echo -ne "${_SKIP_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[42m${_SKIP_FLAG}\033[0m $@"
        else
            echo -e "${_SKIP_FLAG} $@"
        fi
    fi
}
ECHO_QUESTION()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[45m${_QUESTION_FLAG}\033[0m $@"
        else
            echo -ne "${_QUESTION_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[45m${_QUESTION_FLAG}\033[0m $@"
        else
            echo -e "${_QUESTION_FLAG} $@"
        fi
    fi
}

ECHO_ERROR()
{
    if [ X"$1" == X"-n" ]; then
        shift 1
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -ne "\033[41m${_ERROR_FLAG}\033[0m $@"
        else
            echo -ne "${_ERROR_FLAG} $@"
        fi
    else
        if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
            echo -e "\033[41m${_ERROR_FLAG}\033[0m $@"
        else
            echo -e "${_ERROR_FLAG} $@"
        fi
    fi
}

ECHO_DEBUG()
{
    if [ X"${IREDMAIL_DEBUG}" == X"YES" ]; then
        if [ X"$1" == X"-n" ]; then
            shift 1
            if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
                echo -ne "\033[42m${_DEBUG_FLAG}\033[0m $@"
            else
                echo -ne "${_DEBUG_FLAG} $@"
            fi
        else
            if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
                echo -e "\033[42m${_DEBUG_FLAG}\033[0m $@"
            else
                echo -e "${_DEBUG_FLAG} $@"
            fi
        fi
    else
        :
    fi
}

read_setting()
{
    answer="${1}"
    if [ ! -z "${answer}" ]; then
        ANSWER="${answer}"
        echo ${ANSWER}
    else
        read ANSWER
    fi
}

backup_file()
{
    # Usage: backup_file file1 [file2 file3 ... fileN]
    if [ X"$#" != X"0" ]; then
        for conf_file in $@; do
            if [ -f ${conf_file} ]; then
                if [ X"${TERM}" == X"xterm" -o X"${TERM}" == X"linux" -o X"${TERM}" == X"pcconsole" ]; then
                    if [ X"${IREDMAIL_DEBUG}" == X"YES" ]; then
                        echo -e "\033[43m${_BACKUP_FLAG}\033[0m ${conf_file} -> $(basename ${conf_file}).${DATE}."
                    fi
                else
                    if [ X"${IREDMAIL_DEBUG}" == X"YES" ]; then
                        echo -e "${_BACKUP_FLAG} ${conf_file} -> $(basename ${conf_file}).${DATE}."
                    fi
                fi

                cp -f ${conf_file} ${conf_file}.${DATE}
            else
                :
            fi
        done
    else
        :
    fi
}

check_user()
{
    # Check special user privilege to execute this script.
    if [ X"$(id -u)" != X"$(id -u ${1})" ]; then
        ECHO_ERROR "Please run this script as user: ${1}."
        exit 255
    else
        if [ X"$(id -u)" == X"0" ]; then
            export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
        else
            :
        fi
    fi
}

check_hostname()
{
    echo ${HOSTNAME} | grep '\.' >/dev/null 2>&1
    [ X"$?" != X"0" ] && \
        ECHO_ERROR "Please configure a fully qualified domain name (FQDN) in /etc/hosts before we go further.\n\nExample:\n\n127.0.0.1   mail.iredmail.org mail localhost\n" && \
        exit 255
}

check_pkg()
{
    # Usage: check_pkg <command> <package>
    # It means: <package> owns <command>
    cmd="$1"
    pkg="$2"

    for i in $(echo $PATH|sed 's/:/ /g'); do
        [ -x $i/${cmd} ] && export HAS_CMD='YES' && break
    done

    if [ X"${HAS_CMD}" != X'YES' ]; then
        ECHO_INFO "Install package: ${pkg}"
        eval ${install_pkg} ${pkg}
        if [ X"$?" != X"0" ]; then
            ECHO_ERROR "Please install package ${pkg} first." && exit 255
        fi
    fi

    unset HAS_CMD
}

# Check necessery privileges/files/dirs.
check_env()
{
    # Check user privilege.
    check_user root

    # Check FQDN hostname.
    check_hostname

    # Check config tool: dialog.
    check_pkg ${BIN_DIALOG} ${PKG_DIALOG}

    ECHO_INFO -n "Checking configuration file: ${CONFIG_FILE} ..."
    if [ -f ${CONFIG_FILE} ]; then
        grep '^#EOF$' ${CONFIG_FILE} >/dev/null

        if [ X"$?" == X"0" ]; then
            echo -e " [FOUND]"
            ECHO_QUESTION -n "Use it for mail server setting? [y|N]"
            read_setting ${AUTO_USE_EXISTING_CONFIG_FILE}
            case $ANSWER in
                Y|y )
                    ECHO_INFO "Use config file: ${CONFIG_FILE} for mail server setting."
                    . ${CONFIG_FILE}

                    # Check installation status.
                    # After each component installation was completed, there
                    # should be a variable in ${STATUS_FILE}, e.g.
                    #
                    #   export STATUS_PHP_INSTALLATION='DONE'
                    #   export STATUS_PHP_CONFIGURATION='DONE'
                    #
                    if [ -e ${STATUS_FILE} ]; then
                        ECHO_INFO "Import installation process status from file: ${STATUS_FILE}."
                        . ${STATUS_FILE}
                    else
                        echo '' > ${STATUS_FILE}
                    fi

                    # Initialize tip file.
                    if [ ! -e ${TIP_FILE} ]; then
                        cat > ${TIP_FILE} <<EOF
${CONF_MSG}
EOF
                    else
                        :
                    fi
                    ;;
                N|n|* )
                    echo "Skip configuration file: ${CONFIG_FILE}."
                    . ${CONFIG_VIA_DIALOG}
                    ;;
            esac
        else
            echo -e "\tFound, but not finished."
            . ${CONFIG_VIA_DIALOG}
        fi
    else
        echo -e "\t[UNCOMPLETED]"
        . ${CONFIG_VIA_DIALOG}
    fi
}

extract_pkg()
{
    if [ X"$2" = X"" ]; then
        DST='.'
    else
        DST="$2"
    fi

    if echo $1 | grep '.tar.gz$' >/dev/null 2>&1 ; then
        ECHO_DEBUG "Extracting: $1 -> ${DST}"
        tar zxf $1 -C $DST
    elif echo $1 | grep '.tgz$' >/dev/null 2>&1 ; then
        ECHO_DEBUG "Extracting: $1 -> ${DST}"
        tar zxf $1 -C $DST
    elif echo $1 | grep '.tar.bz2$' >/dev/null 2>&1 ; then
        # Install bzip2 first.
        check_pkg ${BIN_BZIP2} ${PKG_BZIP2}

        ECHO_DEBUG "Extracting: $1 -> ${DST}"
        tar xjf $1 -C $DST
    else
        ECHO_ERROR "Unknown archive format."
    fi
}

check_status_before_run()
{
    #
    # Every function will append status info to ${STATUS_FILE}.
    # Format is:
    #   function_name() {:}
    # Status info:
    #   export status_function_name='DONE'
    #
    _status="status_$1"
    if [ X"$(eval echo \$${_status})" != X"DONE" ]; then
        $1
    else
        ECHO_SKIP "Function: $1."
    fi
}

hash_domain()
{
    # Usage: hash_domain domain
    domain="$( echo $1 | tr [A-Z] [a-z] )"

    # Different domain style: hashed, normal.
    #if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
    #    length="$(echo ${domain} | wc -L)"
    #    str1="$(echo ${domain} | cut -c1)"
    #    str2="$(echo ${domain} | cut -c2)"

    #    if [ X"${length}" == X"1" ]; then
    #        str2="${str1}"
    #    elif [ X"${length}" == X"2" ]; then
    #        str2="${str2}"
    #    else
    #        :
    #    fi

        # Use mbox, will be changed later.
    #    domain="${str1}/${str1}${str2}/${domain}"
    #else
        # Use mbox, will be changed later.
    #    domain="${domain}"
    #fi

    echo ${domain}
}

# Hash maildir string.
hash_maildir()
{
    # Usage: hash_maildir username
    username="$( echo $1 | tr [A-Z] [a-z] )"

    # Different maildir style: hashed, normal.
    if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
        str1="$(echo ${username} | cut -c1)"
        str2="$(echo ${username} | cut -c2)"
        str3="$(echo ${username} | cut -c3)"

        if [ X"${username}" == X"${str1}" ]; then
            # Username has only one character
            str2="${str1}"
            str3="${str1}"
        elif [ X"${username}" == X"${str1}${str2}" ]; then
            str3="${str2}"
        else
            :
        fi

        # Use mbox, will be changed later.
        maildir="${str1}/${str2}/${str3}/${username}-${DATE}"
    else
        # Use mbox, will be changed later.
        maildir="${username}-${DATE}"
    fi

    # For maildir format.
    [ X"${MAILBOX_FORMAT}" == X"Maildir" ] && maildir="${maildir}/"

    echo ${maildir}
}

# -----------------------------------------------------------------
# - OS/OS_Version/Arch dependent.
# -----------------------------------------------------------------
enable_service_rh()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Enable service: $i."
            /sbin/chkconfig --level 345 $i on
        fi
    done
}

disable_service_rh()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Disable service: $i."
            /sbin/chkconfig --level 345 $i off
        fi
    done
}

enable_service_suse()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Enable service: $i."
            systemctl enable ${i}.service
        fi
    done
}

disable_service_suse()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Disable service: $i."
            systemctl disable ${i}.service >/dev/null
        fi
    done
}

enable_service_debian()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Enable service: $i."
            update-rc.d $i defaults
        fi
    done
}

disable_service_debian()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Disable service: $i."
            update-rc.d -f $i remove
        fi
    done
}

enable_service_gentoo()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Enable service: $i."
            rc-update add $i default
        fi
    done
}

disable_service_gentoo()
{
    services="$@"
    for i in $services; do
        if [ -x /etc/init.d/$i ]; then
            ECHO_DEBUG "Disable service: $i."
            rc-update del $i default
        fi
    done
}

service_control()
{
    service="$1"
    action="$2"
    tmp_rc_script="${DIR_RC_SCRIPTS}/${service}"
    if [ -x ${tmp_rc_script} ]; then
        ${tmp_rc_script} ${action}
    else
        ECHO_ERROR "File not exist or not executable: ${tmp_rc_script}."
    fi
}

# Generate MD5 password
gen_md5_passwd()
{
    # Usage: gen_sql_passwd 'plain_password'
    openssl passwd -1 ${1}
}

gen_ldap_passwd()
{
    if [ X"${DISTRO}" == X'FREEBSD' -o X"${DISTRO}" == X'OPENBSD' ]; then
        SLAPPASSWD='/usr/local/sbin/slappasswd'
    else
        SLAPPASSWD='/usr/sbin/slappasswd'
    fi

    password="${1}"
    if [ -x ${SLAPPASSWD} ]; then
        ${SLAPPASSWD} -h {SSHA} -s "${password}"
    else
        python ${TOOLS_DIR}/generate_ssha_password.py ${password}
    fi
}

# Create SSL certs/private files.
generate_ssl_keys()
{
    ECHO_INFO "Create self-signed SSL certification files."

    # Create necessary directories.
    mkdir -p ${SSL_KEY_DIR} ${SSL_CERT_DIR} 2>/dev/null
    [ X"${DISTRO}" == X'GENTOO' ] && chmod o+rx ${SSL_KEY_DIR}

    openssl req \
        -x509 -nodes -days 3650 -newkey rsa:${SSL_KEY_SIZE} \
        -subj "/C=${TLS_COUNTRY}/ST=${TLS_STATE}/L=${TLS_CITY}/O=${TLS_COMPANY}/OU=${TLS_DEPARTMENT}/CN=${TLS_HOSTNAME}/emailAddress=${TLS_ADMIN}/" \
        -out ${SSL_CERT_FILE} -keyout ${SSL_KEY_FILE} >/dev/null 2>&1

    # Set correct file permission.
    chmod +r ${SSL_CERT_FILE}
    chmod +r ${SSL_KEY_FILE}

    cat >> ${TIP_FILE} <<EOF

SSL cert keys (size: ${SSL_KEY_SIZE}):
    - ${SSL_CERT_FILE}
    - ${SSL_KEY_FILE}

EOF

    echo 'export status_generate_ssl_keys="DONE"' >> ${STATUS_FILE}
}

# Add alias entry in Postfix /etc/postfix/aliases.
add_postfix_alias()
{
    # Usage: add_postfix_alias nobody root
    # File /path/to/aliases will be created if not exist.
    export alias_orig="${1}"
    export alias_dest="${2}"

    if [ ! -f ${POSTFIX_FILE_ALIASES} ]; then
        if [ -f /etc/aliases ]; then
            cp -f /etc/aliases ${POSTFIX_FILE_ALIASES}
        else
            # Create an empty file
            touch ${POSTFIX_FILE_ALIASES}
        fi
    fi

    # If alias_orig exists, comment it out
    if grep "^${alias_orig}:" ${POSTFIX_FILE_ALIASES} >/dev/null 2>&1; then
        perl -pi -e 's/^($ENV{alias_orig}:.*)/#${1}/' ${POSTFIX_FILE_ALIASES}
    fi

    # Add new alias
    echo "${alias_orig}: ${alias_dest}" >> ${POSTFIX_FILE_ALIASES}

    postalias hash:${POSTFIX_FILE_ALIASES} >/dev/null 2>&1
    unset alias_orig
    unset alias_dest
}

# Install/Remove binary packages on RHEL/CentOS.
install_pkg_rhel()
{
    ECHO_INFO "Installing package(s): $@"
    ${YUM} -y --disablerepo=epel,rpmforge,ius install $@
    if [ X"$?" != X"0" ]; then
        ECHO_ERROR "Installation failed, please check the terminal output."
        ECHO_ERROR "If you're not sure what the problem is, try to get help in iRedMail"
        ECHO_ERROR "forum: http://www.iredmail.org/forum/"
        exit 255
    else
        :
    fi
}

remove_pkg_rhel()
{
    ECHO_INFO "Removing package(s): $@"
    ${YUM} remove -y $@
    [ X"$?" != X"0" ] && ECHO_ERROR "Package removed failed, please check the terminal output."
}

# Install/Remove binary packages on openSUSE 
install_pkg_suse()
{
    ECHO_INFO "Installing package(s): $@"
    zypper --non-interactive install -y $@
    if [ X"$?" != X"0" ]; then
        ECHO_ERROR "Installation failed, please check the terminal output."
        exit 255
    else
        :
    fi
}

remove_pkg_suse()
{
    ECHO_INFO "Removing package(s): $@"
    zypper --non-interactive remove -y $@
    [ X"$?" != X"0" ] && ECHO_ERROR "Package removed failed, please check the terminal output."
}

# Install/Remove binary packages on Debian/Ubuntu.
install_pkg_debian()
{
    ECHO_INFO "Installing package(s): $@"
    ${APTGET} install -y --force-yes $@
    if [ X"$?" != X"0" ]; then
        ECHO_ERROR "Installation failed, please check the terminal output."
        exit 255
    else
        :
    fi
}

remove_pkg_debian()
{
    ECHO_INFO "Removing package(s): $@"
    ${APTGET} purge -y $@
    [ X"$?" != X"0" ] && ECHO_ERROR "Package removed failed, please check the terminal output."
}

# Install/Remove binary packages on Gentoo
install_pkg_gentoo()
{
    ECHO_INFO "Installing package(s) with option(s) '${GENTOO_EMERGE_INSTALL_OPTIONS}': $@"
    pkgs_total="$(echo $@ | wc -w)"
    pkgs_counter=1
    for pkg in $@; do
        if [ X"${pkg}" != X'' ]; then
            portname="$( echo ${pkg} | tr -d '-' | tr -d '/' | tr -d '\.' )"
            status="\$status_install_port_$portname"
            if [ X"$(eval echo ${status})" != X"DONE" ]; then
                ECHO_INFO "Installing package (${pkgs_counter} of ${pkgs_total}): ${pkg}"
                echo "export status_install_port_${portname}='processing'" >> ${STATUS_FILE}

                # Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
                port_start_time="$(date +%s)"

                emerge ${GENTOO_EMERGE_INSTALL_OPTIONS} ${pkg}

                if [ X"$?" == X"0" ]; then
                    # Log used time
                    used_time="$(($(date +%s)-port_start_time))"

                    echo "export status_install_port_${portname}='DONE'  # ${used_time} seconds, ~$((used_time/60)) minutes" >> ${STATUS_FILE}
                else
                    ECHO_ERROR "Installation failed, please check the terminal output."
                    ECHO_ERROR "If you're not sure what the problem is, try to get help in iRedMail"
                    ECHO_ERROR "forum: http://www.iredmail.org/forum/"
                    exit 255
                fi
            else
                ECHO_INFO "Skip package: ${pkg}."
            fi
            pkgs_counter=$((pkgs_counter+1))
        fi
    done
}

remove_pkg_gentoo()
{
    ECHO_INFO "Removing package(s): $@"
    emerge -C $@
    [ X"$?" != X"0" ] && ECHO_ERROR "Package removed failed, please check the terminal output."
}

gentoo_mask_package()
{
    [ -f ${GENTOO_CONF_PACKAGE_MASK} ] || touch ${GENTOO_CONF_PACKAGE_MASK}
    for pkg in $@; do
        comment_mark="# ${PROG_NAME}-${pkg}"
        if ! grep "^${comment_mark}$" ${GENTOO_CONF_PACKAGE_MASK} &>/dev/null; then
            ECHO_DEBUG "Mask packages in ${GENTOO_CONF_PACKAGE_MASK}: $@"
            echo "${comment_mark}" >> ${GENTOO_CONF_PACKAGE_MASK}
            echo "${pkg}" >> ${GENTOO_CONF_PACKAGE_MASK}
        else
            ECHO_DEBUG "Skip masking packages in ${GENTOO_CONF_PACKAGE_MASK}: $@"
        fi
    done
}

gentoo_unmask_package()
{
    [ -f ${GENTOO_CONF_PACKAGE_UNMASK} ] || touch ${GENTOO_CONF_PACKAGE_UNMASK}
    for pkg in $@; do
        comment_mark="# ${PROG_NAME}-${pkg}"
        if ! grep "^${comment_mark}$" ${GENTOO_CONF_PACKAGE_UNMASK} &>/dev/null; then
            ECHO_DEBUG "Unmask packages in ${GENTOO_CONF_PACKAGE_UNMASK}: $@"
            echo "${comment_mark}" >> ${GENTOO_CONF_PACKAGE_UNMASK}
            echo "${pkg}" >> ${GENTOO_CONF_PACKAGE_UNMASK}
        else
            ECHO_DEBUG "Skip unmasking packages in ${GENTOO_CONF_PACKAGE_UNMASK}: $@"
        fi
    done
}

gentoo_add_use_flags()
{
    # USAGE: gentoo_add_use_flags pkg_name USE_flags
    [ -f ${GENTOO_CONF_PACKAGE_USE} ] || touch ${GENTOO_CONF_PACKAGE_USE}
    if [ X"$#" == X'2' ]; then
        #echo "${1} ${2}" >> /etc/portage/package.use
        pkg="${1}"
        use_flags="${2}"
        comment_mark="# ${PROG_NAME}-${pkg}"
        if ! grep "^${comment_mark}$" ${GENTOO_CONF_PACKAGE_USE} &>/dev/null; then
            ECHO_DEBUG "Add USE flags for package ${pkg} in ${GENTOO_CONF_PACKAGE_USE}."
            echo "${comment_mark}" >> ${GENTOO_CONF_PACKAGE_USE}
            echo "${pkg} ${use_flags}" >> ${GENTOO_CONF_PACKAGE_USE}
        else
            ECHO_DEBUG "Skip adding USE flags for package ${pkg} in ${GENTOO_CONF_PACKAGE_USE}."
        fi
    fi
}

gentoo_add_make_conf()
{
    # USAGE: gentoo_add_make_conf VARIABLE VALUE
    if [ X"$#" == X'2' ]; then
        export VAR="${1}"
        export VALUE="${2}"

        # Comment out old setting.
        perl -pi -e 's/($ENV{VAR}.*)/#${1}/' ${GENTOO_CONF_MAKE_CONF}

        # Add new setting.
        echo "${VAR}='${VALUE}'" >> ${GENTOO_CONF_MAKE_CONF}

        unset VAR VALUE
    fi
}

enable_service_openbsd()
{
    # USAGE: enable_service_openbsd flag value
    # For example: enable_service_openbsd sendmail_flags ''
    [ -f ${RC_CONF_LOCAL} ] || touch ${RC_CONF_LOCAL}
    if [ X"$#" == X'2' ]; then
        flag="${1}"
        value="${2}"
        comment_mark="# ${PROG_NAME}-${flag}"
        if ! grep "^${comment_mark}$" ${RC_CONF_LOCAL} &>/dev/null; then
            ECHO_DEBUG "Add flag in ${RC_CONF_LOCAL}: ${flag}='${value}'"
            echo "${comment_mark}" >> ${RC_CONF_LOCAL}
            echo "${flag}='${value}'" >> ${RC_CONF_LOCAL}
        else
            ECHO_DEBUG "Skip adding flag in ${RC_CONF_LOCAL}: ${pkg}='${value}'"
        fi
    fi
}
disable_service_openbsd()
{
    :
}
enable_service_freebsd()
{
    :
}
disable_service_freebsd()
{
    :
}

freebsd_add_make_conf()
{
    # USAGE: freebsd_add_make_conf VAR VALUE
    [ -f ${FREEBSD_MAKE_CONF} ] || touch ${FREEBSD_MAKE_CONF}
    if [ X"$#" == X'2' ]; then
        var="${1}"
        value="${2}"
        final_option="${1}=${2}"
        comment_mark="# ${PROG_NAME}-${var}"
        if ! grep "^${comment_mark}$" ${FREEBSD_MAKE_CONF} &>/dev/null; then
            ECHO_DEBUG "Add make option in ${FREEBSD_MAKE_CONF}: ${final_option}"
            echo "${comment_mark}" >> ${FREEBSD_MAKE_CONF}
            echo "${final_option}" >> ${FREEBSD_MAKE_CONF}
        else
            ECHO_DEBUG "Skip adding option in ${FREEBSD_MAKE_CONF}: ${final_option}"
        fi
    fi
}

freebsd_enable_service_in_rc_conf()
{
    # USAGE: freebsd_enable_service_in_rc_conf VAR VALUE
    if [ X"${DISTRO}" == X'FREEBSD' ]; then
        [ -f /etc/rc.conf ] || touch /etc/rc.conf
        if [ X"$#" == X'2' ]; then
            var="${1}"
            value="${2}"
            final_option="${1}='${2}'"
            comment_mark="# ${PROG_NAME}-${var}"
            if ! grep "^${comment_mark}$" /etc/rc.conf &>/dev/null; then
                ECHO_DEBUG "Add setting in /etc/rc.conf: ${final_option}"
                echo "${comment_mark}" >> /etc/rc.conf
                echo "${final_option}" >> /etc/rc.conf
            else
                ECHO_DEBUG "Skip adding setting in /etc/rc.conf: ${final_option}"
            fi
        fi
    fi
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.