install.sh 33.7 KB
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
#!/bin/bash

# > -----------------------------------------
# Run that script with bash even if the user use sh/dash or any sh like 
# interpreter. This way it correctly works with either: 
# "sh ./my_script.sh" or "bash ./my_script.sh" or "./my_script.sh"

if [ -z "$BASH_VERSION" ]
then
    exec bash "$0" "$@"
fi

# < -----------------------------------------

# NOTE: Evita problemas com caminhos relativos! By Questor
SCRIPTDIR_V="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $SCRIPTDIR_V/ez_i.sh

# > --------------------------------------------------------------------------
# INÍCIO!
# --------------------------------------

read -d '' TITLE_F <<"EOF"
LBC - LBConverter Installer
EOF

read -d '' VERSION_F <<"EOF"
1.0.0.0
EOF

read -d '' ABOUT_F <<"EOF"
This script will install LBC - LBConverter the LightBase OCR (Optical Character 
Recognition)/data extractor component that obtain data from submitted files!

Have fun! =D
EOF

read -d '' WARNINGS_F <<"EOF"
- This installer is compatible with RHEL, SUSE, Debian or distributions 
based on these.

- We RECOMMEND you...
    Install all the components (answer yes to everything). Except 
        contrary guidance!
    Check for previous installations! If there is previous 
        installations consider this variant in the process!
    Although this is not mandatory, do the installation process in an 
        exclusive machine for this.

- We NOTICE you...
    This installer assumes that the target distribution has a "standard 
        setup". This may include components such as firewall, SELinux 
        and others.

- We WARNING you...
    THIS INSTALLER AND RESULTING PRODUCTS COMES WITH ABSOLUTELY NO WARRANTY! 
    USE AT YOUR OWN RISK! WE ARE NOT RESPONSIBLE FOR ANY DAMAGE TO YOURSELF, 
    HARDWARE, OR CO-WORKERS. EXCEPT IN CASES WHERE THERE ARE SIGNED CONTRACT 
    THAT REGULATES THIS!
EOF

read -d '' COMPANY_F <<"EOF"
BR Light LTDA - LightBase Consulting in Public Software/LightBase Consultoria em Software Público
Free Software + Our Ideas = Best Solution!/Software Livre + Nossas Idéias = Melhor Solução!
+55-61-3347-1949 - http://www.LightBase.com.br - Brasil-DF
EOF

f_begin "$TITLE_F" "$VERSION_F" "$ABOUT_F" "$WARNINGS_F" "$COMPANY_F"
ABOUT_F=""
WARNINGS_F=""

# < --------------------------------------------------------------------------

# > --------------------------------------------------------------------------
# TERMOS E LICENÇA!
# --------------------------------------

read -d '' TERMS_LICEN_F <<"EOF"
BY USING THIS INSTALLER YOU ARE AGREEING TO THE TERMS OF USE OF ALL 
INVOLVED SOFTWARE!
EOF

f_terms_licen "$TERMS_LICEN_F"
TERMS_LICEN_F=""

# < --------------------------------------------------------------------------

# > -----------------------------------------
# Checar se o usuário é root!

f_is_root

# < -----------------------------------------

# > --------------------------------------------------------------------------
# INTRUÇÕES!
# --------------------------------------

read -d '' INSTRUCT_F <<"EOF"
- TO CANCEL installation at any time use Ctrl+c!
EOF

f_instruct "$INSTRUCT_F"
INSTRUCT_F=""

# < --------------------------------------------------------------------------

DISTRO_TYPE=""
DISTRO_NAME=""
# > -----------------------------------------
# Checar se a distro é compatível!

f_open_section
f_about_distro
f_div_section
echo "DISTRO INFORMATION:"
f_div_section
echo "NAME: .... ${F_ABOUT_DISTRO_R[0]}"
echo "VERSION: . ${F_ABOUT_DISTRO_R[1]}"
echo "BASED: ... ${F_ABOUT_DISTRO_R[2]}"
echo "ARCH: .... ${F_ABOUT_DISTRO_R[3]}"
f_div_section

if [[ "${F_ABOUT_DISTRO_R[2]}" == "Debian" ]] || [[ "${F_ABOUT_DISTRO_R[2]}" == "RedHat" ]] || 
    [[ "${F_ABOUT_DISTRO_R[2]}" == "Suse" ]] ; then
    if [[ "${F_ABOUT_DISTRO_R[2]}" == "Debian" ]] ; then
        DISTRO_TYPE="DEB"
        if [[ "${F_ABOUT_DISTRO_R[0]}" == "Ubuntu" ]] ; then
            DISTRO_NAME="Ubuntu"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "14.04" ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 14.04/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        elif [[ "${F_ABOUT_DISTRO_R[0]}" == "Debian GNU/Linux" ]] ; then
            DISTRO_NAME="Debian"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "8" ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 8/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        else
            f_div_section
            f_yes_no "Linux distro may be incompatible with this installer (expected: Ubuntu or Debian/obtained: ${F_ABOUT_DISTRO_R[0]})! Continue?"
            f_div_section
            if [ ${YES_NO_R} -eq 0 ] ; then
                exit 0
            fi
        fi
    elif [[ "${F_ABOUT_DISTRO_R[2]}" == "RedHat" ]] ; then
        DISTRO_TYPE="RH"
        if [[ "${F_ABOUT_DISTRO_R[0]}" == "Red Hat Enterprise Linux Server" ]] ; then
            DISTRO_NAME="RedHat"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "6."* ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 6.X/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        elif [[ "${F_ABOUT_DISTRO_R[0]}" == "CentOS" ]] ; then
            DISTRO_NAME="CentOS"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "6."* ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 6.X/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        else
            f_div_section
            f_yes_no "Linux distro may be incompatible with this installer (expected: CentOS or Red Hat Enterprise Linux Server/obtained: ${F_ABOUT_DISTRO_R[0]})! Continue?"
            f_div_section
            if [ ${YES_NO_R} -eq 0 ] ; then
                exit 0
            fi
        fi
    elif [[ "${F_ABOUT_DISTRO_R[2]}" == "Suse" ]] ; then
        DISTRO_TYPE="SUSE"
        if [[ "${F_ABOUT_DISTRO_R[0]}" == "openSUSE" ]] ; then
            DISTRO_NAME="openSUSE"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "13."* ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 13.X/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        elif [[ "${F_ABOUT_DISTRO_R[0]}" == "SLES" ]] ; then
            DISTRO_NAME="SLES"
            if [[ "${F_ABOUT_DISTRO_R[1]}" != "12."* ]] ; then
                f_div_section
                f_yes_no "Linux version may be incompatible with this installer (expected: 12.X/obtained: ${F_ABOUT_DISTRO_R[1]})! Continue?"
                f_div_section
                if [ ${YES_NO_R} -eq 0 ] ; then
                    exit 0
                fi
            fi
        else
            f_div_section
            f_yes_no "Linux distro may be incompatible with this installer (expected: openSUSE or SUSE Linux Enterprise Server/obtained: ${F_ABOUT_DISTRO_R[0]})! Continue?"
            f_div_section
            if [ ${YES_NO_R} -eq 0 ] ; then
                exit 0
            fi
        fi
    fi

    if [[ "${F_ABOUT_DISTRO_R[3]}" != "x86_64" ]] ; then
        f_enter_to_cont "Linux architecture completely incompatible with this installer (expected: x86_64/obtained: ${F_ABOUT_DISTRO_R[3]})!"
        exit 0
    fi
else
    f_enter_to_cont "Linux distro completely incompatible with this installer (expected: Debian(or based) or RedHat(or based) or SUSE(or based)/obtained: ${F_ABOUT_DISTRO_R[2]})!"
    exit 0
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Dá ao usuário mais avançado a possibilideade de usar o instalador 
# simplificado!

# NOTE: É possível forçar o processo de instalção simplificado setando 
# "SIMPLE_INST" com 1! By Questor
SIMPLE_INST=0
if [ ${SIMPLE_INST} -eq 0 ] ; then
    f_open_section
    f_yes_no "Use simple install (use default values for most of the options)?"
    if [ ${YES_NO_R} -eq 1 ] ; then

        # NOTE: Essa variável serve apenas para "preservar" o valor 
        # setado pelo usuário sendo somente "leitura". A variável a 
        # ser usada nas regras deve ser "EZ_I_SKIP_ON_V" (ez_i.sh)! Essa 
        # estratégia serve para mudarmos o comportamento do "ez_i.sh" 
        # de acordo com as circunstâncias! By Questor
        SIMPLE_INST=1

        # NOTE: Essa variável é para consumo do "ez_i.sh", para que ele 
        # não execute algumas funções e simplifique o processo de 
        # instalação! By Questor
        EZ_I_SKIP_ON_V=1
    fi
    f_close_section
    sleep 1
fi

# < -----------------------------------------

# > -----------------------------------------
# Garantir o encodamento correto para evitar problemas de 
# compatibilidade!

if [ ${SIMPLE_INST} -eq 0 ] ; then
    EZ_I_SKIP_ON_V=$SIMPLE_INST
    f_open_section
    f_yes_no "Set terminal encode? (in some cases recommended for Windows ssh clients)"
    if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
        export LANG=pt_BR.utf8
    fi
    f_close_section
fi

# < -----------------------------------------

# > -----------------------------------------
# Desabilita o SElinux!

if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    EZ_I_SKIP_ON_V=0
    f_open_section
    f_yes_no "Disable SElinux (use "y" if you never did it)?"
    if [ ${YES_NO_R} -eq 1 ] ; then
        setenforce 0

        # NOTE: As condições abaixo visam evitar que o arquivo seja 
        # desnecessariamente e erroneamente modificado! By Questor
        EZ_I_SKIP_ON_V=$SIMPLE_INST
        f_fl_cont_str "# SELINUX=enforcing" "/etc/sysconfig/selinux" "The file \"/etc/sysconfig/selinux\" probably has already been changed! Check it!"
        EZ_I_SKIP_ON_V=0
        if [ ${FL_CONT_STR_R} -eq 0 ] ; then
            f_fl_cont_str "SELINUX=disabled" "/etc/sysconfig/selinux"
            if [ ${FL_CONT_STR_R} -eq 0 ] ; then
                f_ez_sed "SELINUX=enforcing" "# SELINUX=enforcing\nSELINUX=disabled" "/etc/sysconfig/selinux"
            fi
        fi
    fi
    f_close_section
fi

# < -----------------------------------------

# > -----------------------------------------
# Atualizar a distro (repositório)!

EZ_I_SKIP_ON_V=$SIMPLE_INST
if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    if [[ "$DISTRO_NAME" == "RedHat" ]] ; then
        RESOURCES_ARR_P=("Red Hat Enterprise Linux Server")
        f_chk_distro_status "$DISTRO_NAME" RESOURCES_ARR_P[@]
    fi
    f_div_section
    f_yes_no "Update your distro? (\"y\" highly recommended)"
    f_div_section
    if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
        yum -y update
    fi
    f_pack_is_inst "git" "yum" "\"git\" already installed!"
    if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
        yum -y install git
    fi
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    f_div_section
    f_yes_no "Update your distro? (\"y\" highly recommended)"
    f_div_section
    if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
        apt-get -y update
    fi
    f_pack_is_inst "git" "apt-get" "\"git\" already installed!"
    if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
        apt-get -y install git
    fi
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    if [[ "$DISTRO_NAME" == "SLES" ]] ; then
        RESOURCES_ARR_P=("SUSE_Linux_Enterprise_Server_12_SP1_x86_64"
"SUSE_Linux_Enterprise_Software_Development_Kit_12_SP1_x86_64"
"Web_and_Scripting_Module_12_x86_64")
        f_chk_distro_status "SLES" RESOURCES_ARR_P[@]
    fi
    f_div_section
    f_yes_no "Update your distro? (\"y\" highly recommended)"
    f_div_section
    if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
        zypper --non-interactive update
    fi
    f_pack_is_inst "git-core" "zypper" "\"git-core\" already installed!"
    if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
        zypper --non-interactive install git-core
    fi
fi

# < -----------------------------------------

BASE_INST_DIR_V="/usr/local/lb"
# > -----------------------------------------
# Criar o diretório base da instalação!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
QUESTION_F="Insert where the base installation directory (\"lb\") will be created (don't use \"/\" at the end). 
Use empty for \"/usr/local\"!"

f_get_usr_input "$QUESTION_F" 1
QUESTION_F=""
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ -z "$GET_USR_INPUT_R" ] ; then
    f_chk_by_path_hlp "$BASE_INST_DIR_V" "d" "\"$BASE_INST_DIR_V\" directory already created!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
        mkdir -p "$BASE_INST_DIR_V"
    fi
else
    BASE_INST_DIR_V="$GET_USR_INPUT_R/lb"
    f_chk_by_path_hlp "$BASE_INST_DIR_V" "d" "\"$BASE_INST_DIR_V\" directory already created!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
        mkdir -p "$BASE_INST_DIR_V"
    fi
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Instalar pacotes que são comuns a determinados componentes!

F_COM_PKS_INSTALLED=0
f_common_packs() {
    : 'Instalar pacotes que são comuns a determinados componentes.'

    if [ ${F_COM_PKS_INSTALLED} -eq 0 ] ; then
        if [[ "$DISTRO_TYPE" == "RH" ]] ; then
            f_pack_is_inst "gcc-c++" "yum" "\"gcc-c++\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install gcc-c++
            fi
            f_pack_is_inst "autoconf" "yum" "\"autoconf\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install autoconf
            fi
            f_pack_is_inst "automake" "yum" "\"automake\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install automake
            fi
            f_pack_is_inst "libtool" "yum" "\"libtool\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install libtool
            fi
            f_pack_is_inst "zlib-devel" "yum" "\"zlib-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install zlib-devel
            fi
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            f_pack_is_inst "g++" "apt-get" "\"g++\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install g++
            fi
            f_pack_is_inst "autoconf" "apt-get" "\"autoconf\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install autoconf
            fi
            f_pack_is_inst "make" "apt-get" "\"make\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install make
            fi
            f_pack_is_inst "libtool" "apt-get" "\"libtool\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libtool
            fi
            f_pack_is_inst "zlib1g-dev" "apt-get" "\"zlib1g-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install zlib1g-dev
            fi
        elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            f_pack_is_inst "gcc-c++" "zypper" "\"gcc-c++\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install gcc-c++
            fi
            if [[ "$DISTRO_NAME" != "SLES" ]] ; then
                f_pack_is_inst "autoconf" "zypper" "\"autoconf\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install autoconf
                fi
                f_pack_is_inst "automake" "zypper" "\"automake\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install automake
                fi
                f_pack_is_inst "make" "zypper" "\"make\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install make
                fi
                f_pack_is_inst "libtool" "zypper" "\"libtool\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install libtool
                fi
            fi
            f_pack_is_inst "zlib-devel" "zypper" "\"zlib-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install zlib-devel
            fi
        fi
        F_COM_PKS_INSTALLED=1
    fi
}

# < -----------------------------------------

# > -----------------------------------------
# Instalar o virtualenv-1.11.6 no python2.X!

PYTHON_2_X=""
VE_2_X=""
if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    PYTHON_2_X="2.6"
    VE_2_X="ve26"
elif [[ "$DISTRO_TYPE" == "DEB" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    PYTHON_2_X="2.7"
    VE_2_X="ve27"
fi
EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install virtualenv-1.11.6 on python$PYTHON_2_X?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        f_chk_by_path_hlp "/usr/bin/virtualenv-$PYTHON_2_X" "f" "virtualenv-1.11.6 already installed!"
    elif [[ "$DISTRO_TYPE" == "DEB" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_chk_by_path_hlp "/usr/local/bin/virtualenv-$PYTHON_2_X" "f" "virtualenv-1.11.6 already installed!"
    fi
    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        f_pack_is_inst "python-setuptools" "yum" "\"python-setuptools\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            yum -y install python-setuptools
        fi
    elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
        f_pack_is_inst "python-setuptools" "apt-get" "\"python-setuptools\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            apt-get -y install python-setuptools
        fi
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_pack_is_inst "python-setuptools" "zypper" "\"python-setuptools\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            zypper --non-interactive install python-setuptools
        fi
    fi
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
        f_common_packs
        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps
        tar -zxvf virtualenv-1.11.6.tar.gz
        cd virtualenv-1.11.6
        python$PYTHON_2_X setup.py install
        cd ..
        rm -rf virtualenv-1.11.6
    fi
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Criar o ambiente virtual (python2.X)!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_enter_to_cont "Create the virtual environment (python$PYTHON_2_X)!"
f_chk_by_path_hlp "$BASE_INST_DIR_V/$VE_2_X" "d" "Virtual environment (python$PYTHON_2_X) already created in \"$BASE_INST_DIR_V/$VE_2_X\"!"
if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
    f_common_packs
    cd "$BASE_INST_DIR_V"
    virtualenv-$PYTHON_2_X $VE_2_X
    mkdir "$BASE_INST_DIR_V/$VE_2_X/src"
    f_enter_to_cont "Virtual environment created in \"$BASE_INST_DIR_V/$VE_2_X\"!"
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Instalar o catdoc, unzip e ImageMagick para o LBConverter!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
INST_CATDOC_UNZIP_IMAGMAG=""
if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    INST_CATDOC_UNZIP_IMAGMAG="Install catdoc, unzip and ImageMagick?"
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    INST_CATDOC_UNZIP_IMAGMAG="Install catdoc, unzip and ImageMagick?"
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    INST_CATDOC_UNZIP_IMAGMAG="Install catdoc and ImageMagick?"
fi
f_yes_no "$INST_CATDOC_UNZIP_IMAGMAG"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then

    UP_CATDOC=1
    f_chk_by_path_hlp "$BASE_INST_DIR_V/$VE_2_X/lib/catdoc-0.94.2" "d" "catdoc-0.94.2 already installed in \"$BASE_INST_DIR_V/$VE_2_X/lib/catdoc-0.94.2\"!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        if [ ${EZ_I_SKIP_ON_V} -eq 0 ] ; then
            f_div_section
            f_yes_no "Update/reinstall catdoc-0.94.2? (\"y\" recommended)"
            f_div_section
            UP_CATDOC=$YES_NO_R
            if [ ${UP_CATDOC} -eq 1 ] ; then
                rm -rf "$BASE_INST_DIR_V/$VE_2_X/lib/catdoc-0.94.2"
            fi
        fi
    fi
    if [ ${UP_CATDOC} -eq 1 ] ; then
        if [[ "$DISTRO_TYPE" == "RH" ]] ; then
            f_common_packs
            f_pack_is_inst "unzip" "yum" "\"unzip\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install unzip
            fi
            f_pack_is_inst "ImageMagick" "yum" "\"ImageMagick\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install ImageMagick
            fi
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            f_common_packs
            f_pack_is_inst "unzip" "apt-get" "\"unzip\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install unzip
            fi
            f_pack_is_inst "imagemagick" "apt-get" "\"imagemagick\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install imagemagick
            fi
        elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            f_common_packs
            f_pack_is_inst "ImageMagick" "zypper" "\"ImageMagick\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install ImageMagick
            fi
        fi
        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps
        tar -zxvf catdoc-0.94.2.tar.gz
        mv ./catdoc-0.94.2 $BASE_INST_DIR_V/$VE_2_X/lib
        cd $BASE_INST_DIR_V/$VE_2_X/lib/catdoc-0.94.2
        ./configure
        make && make install
    fi
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Instalar o tesseract-ocr-3.02.02!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install tesseract-ocr-3.02.02?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then

    UP_TESSERACT_OCR=1
    f_chk_by_path_hlp "/usr/local/lib/tesseract-ocr" "d" "tesseract-ocr-3.02.02 already installed in \"/usr/local/lib/tesseract-ocr\"!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        if [ ${EZ_I_SKIP_ON_V} -eq 0 ] ; then
            f_div_section
            f_yes_no "Update/reinstall tesseract-ocr-3.02.02? (\"y\" recommended)"
            f_div_section
            UP_TESSERACT_OCR=$YES_NO_R
        fi
    fi
    if [ ${UP_TESSERACT_OCR} -eq 1 ] ; then
        if [[ "$DISTRO_TYPE" == "RH" ]] ; then
            f_common_packs
            f_pack_is_inst "libpng12-devel" "yum" "\"libpng12-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install libpng-devel
            fi
            f_pack_is_inst "libjpeg-turbo-devel" "yum" "\"libjpeg-turbo-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install libjpeg-turbo-devel
            fi
            f_pack_is_inst "libtiff-devel" "yum" "\"libtiff-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install libtiff-devel
            fi
            f_pack_is_inst "openjpeg-libs" "yum" "\"openjpeg-libs\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install openjpeg-libs
            fi
            f_pack_is_inst "giflib" "yum" "\"giflib\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install giflib
            fi

            # NOTE: Esse pacote só existe no CentOS 7! By Questor
            # f_pack_is_inst "libwebp-devel" "yum" "\"libwebp-devel\" already installed!"
            # if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            #     yum -y install libwebp-devel
            # fi
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            f_common_packs
            f_pack_is_inst "libpng-dev" "apt-get" "\"libpng-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libpng-dev
            fi
            f_pack_is_inst "libjpeg-dev" "apt-get" "\"libjpeg-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libjpeg-dev
            fi
            f_pack_is_inst "libtiff-dev" "apt-get" "\"libtiff-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libtiff-dev
            fi
            f_pack_is_inst "libopenjpeg2" "apt-get" "\"libopenjpeg2\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libopenjpeg2
            fi
            f_pack_is_inst "libgif-dev" "apt-get" "\"libgif-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libgif-dev
            fi
            f_pack_is_inst "libwebp-dev" "apt-get" "\"libwebp-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libwebp-dev
            fi
        elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            f_common_packs
            f_pack_is_inst "libpng-devel" "zypper" "\"libpng-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libpng-devel
            fi
            f_pack_is_inst "libjpeg-devel" "zypper" "\"libjpeg-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libjpeg-devel
            fi
            f_pack_is_inst "libtiff-devel" "zypper" "\"libtiff-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libtiff-devel
            fi
            if [[ "$DISTRO_NAME" != "SLES" ]] ; then
                f_pack_is_inst "libopenjp2-7" "zypper" "\"libopenjp2-7\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install libopenjp2-7
                fi
            fi
            f_pack_is_inst "libgif6" "zypper" "\"libgif6\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libgif6
            fi
            f_pack_is_inst "libwebp-devel" "zypper" "\"libwebp-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libwebp-devel
            fi
        fi

        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps

        tar -zxvf leptonica-1.71.tar.gz
        mv ./leptonica-1.71 /usr/local/lib
        cd /usr/local/lib/leptonica-1.71
        ./autobuild
        ./configure
        make -j 4 && make install

        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps

        tar -zxvf tesseract-ocr-3.02.02.tar.gz
        mv ./tesseract-ocr /usr/local/lib
        cd /usr/local/lib/tesseract-ocr
        ./autogen.sh
        ./configure
        make -j 4 && make install
        ldconfig

        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps

        tar -zxvf tesseract-ocr-3.01.eng.tar.gz
        tar -zxvf tesseract-ocr-3.02.por.tar.gz
        mv ./tesseract-ocr /usr/local/lib/tesseract-ocr
        cp /usr/local/lib/tesseract-ocr/tesseract-ocr/tessdata/eng.traineddata /usr/local/share/tessdata
        export TESSDATA_PREFIX=/usr/local/lib/tesseract-ocr/tesseract-ocr

    fi
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Instalar as dependências python2.X da LBC - LBConverter!

EZ_I_SKIP_ON_V=$SIMPLE_INST
cd "$SCRIPTDIR_V"
bash py-packs-LBConverter.sh "$EZ_I_SKIP_ON_V" "$BASE_INST_DIR_V" "$PYTHON_2_X" "$VE_2_X"

# < -----------------------------------------

HTTP_PORT_F=6544
# > -----------------------------------------
# Instalar e configurar o LBC - LBConverter!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install the LBC - LBConverter?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then

    f_chk_by_path_hlp "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter" "d" "\"LBConverter\" already installed in \"$BASE_INST_DIR_V/$VE_2_X/src\"!"
    F_BAK_MD_R=1
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        f_ez_mv_bak "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter" "Backup old version and update? (\"y\" recommended)"
    fi
    if [ ${F_BAK_MD_R} -eq 1 ] ; then
        cd "$SCRIPTDIR_V"
        tar -zxvf LBConverter.tar.gz
        mv "$SCRIPTDIR_V/LBConverter" "$BASE_INST_DIR_V/$VE_2_X/src/"
        cd "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter"
        eval "$BASE_INST_DIR_V/$VE_2_X/bin/python$PYTHON_2_X setup.py install"

        f_enter_to_cont "Configure LBC - LBConverter!"
        \cp "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-dist" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-prov"

        if [[ "$DISTRO_TYPE" == "RH" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            f_ez_sed "<LBI_SCRIPT_HEAD>" "# chkconfig: 2345 99 01\n# description: Esquema de inicialização do LBConverter\n# processname: lbconverter" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-prov"
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            f_ez_sed "<LBI_SCRIPT_HEAD>" "### BEGIN INIT INFO\n# Provides:          lbconverter\n# Required-Start:    \$remote_fs \$syslog\n# Required-Stop:     \$remote_fs \$syslog\n# Default-Start:     2 3 4 5\n# Default-Stop:      0 1 6\n# Short-Description: Start daemon at boot time\n# Description:       Enable service provided by daemon.\n### END INIT INFO" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-prov"
        fi

        f_ez_sed "<LBCONVERTERMG_PATH>" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconvertermg" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-prov"
        rm -rf "/etc/init.d/lbconverter"
        mv "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconverter-prov" "/etc/init.d/lbconverter"

        chmod 755 -R /etc/init.d/
        cd /etc/init.d/

        if [[ "$DISTRO_TYPE" == "RH" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            chkconfig --level 2345 lbconverter on
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            update-rc.d -f lbconverter remove
            update-rc.d lbconverter defaults
        fi

        \cp "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconvertermg-dist" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconvertermg"
        f_ez_sed "<VIRTUALENV_PATH>" "$BASE_INST_DIR_V/$VE_2_X" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/lbconvertermg"

        LBG_URL_F="http://127.0.0.1/lbg"
        QUESTION_F="Enter the LBG - LBGenerator URL. 
Use empty for \"$LBG_URL_F\" (LOCALHOST)!"

        f_get_usr_input "$QUESTION_F" 1
        QUESTION_F=""
        if [ -n "$GET_USR_INPUT_R" ] ; then
            LBG_URL_F=$GET_USR_INPUT_R
        fi

        QUESTION_F="Enter the port number for http service. 
Use empty for \"$HTTP_PORT_F\" (recommended)!"

        f_get_usr_input "$QUESTION_F" 1
        QUESTION_F=""
        if [ -n "$GET_USR_INPUT_R" ] ; then
            HTTP_PORT_F=$GET_USR_INPUT_R
        fi

        \cp "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/production.ini-dist" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/production.ini"
        f_ez_sed "<HTTP_SERVICE_PORT_NUM>" "$HTTP_PORT_F" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/production.ini"
        f_ez_sed "<MACHINE_IP_OR_NAME>" "$LBG_URL_F" "$BASE_INST_DIR_V/$VE_2_X/src/LBConverter/production.ini"

        service lbconverter restart
    fi
fi
f_close_section

# < -----------------------------------------

# > -----------------------------------------
# Abrir o firewall para o http service!

if [[ "$DISTRO_TYPE" == "RH" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    EZ_I_SKIP_ON_V=$SIMPLE_INST
    f_open_section
    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        f_yes_no "Open firewall for http service (TCP $HTTP_PORT_F)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables ${HTTP_PORT_F} "Port $HTTP_PORT_F is already open!" 0 "ACCEPT" "tcp" "NEW"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                iptables -I INPUT 6 -p tcp -m state --state NEW -m tcp --dport ${HTTP_PORT_F} -j ACCEPT
                service iptables save
                service iptables restart
            fi
        fi
        f_close_section
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_yes_no "Open firewall for http service (TCP $HTTP_PORT_F)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables ${HTTP_PORT_F} "Port $HTTP_PORT_F is already open!" 0 "ACCEPT" "tcp"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                SuSEfirewall2 open EXT TCP ${HTTP_PORT_F}
                SuSEfirewall2 stop
                SuSEfirewall2 start
            fi
        fi
    fi
    f_close_section
fi

# < -----------------------------------------

# > --------------------------------------------------------------------------
# FINAL!
# --------------------------------------

EZ_I_SKIP_ON_V=0
read -d '' TITLE_F <<"EOF"
Installer finished! Thanks!
EOF

USEFUL_INFO_F="To configure...
    vi $BASE_INST_DIR_V/$VE_2_X/src/LBConverter/production.ini

To start/stop...
    service lbconverter start
    service lbconverter stop

Log...
    less /var/log/lbconverter.log"

f_end "$TITLE_F" "$USEFUL_INFO_F"
TITLE_F=""
USEFUL_INFO_F=""

# < --------------------------------------------------------------------------