install.sh 48.1 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 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
#!/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"
LBG - LBGenerator Installer
EOF

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

read -d '' ABOUT_F <<"EOF"
This script will install LBGenerator the LightBase data specification and model!

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
            f_pack_is_inst "openssl-devel" "yum" "\"openssl-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install openssl-devel
            fi
            f_pack_is_inst "postgresql-devel" "yum" "\"postgresql-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install postgresql-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
            f_pack_is_inst "libssl-dev" "apt-get" "\"libssl-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libssl-dev
            fi
            f_pack_is_inst "dh-exec" "apt-get" "\"dh-exec\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install dh-exec
            fi
            f_pack_is_inst "libpq-dev" "apt-get" "\"libpq-dev\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install libpq-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 "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
                f_pack_is_inst "autoconf" "zypper" "\"autoconf\" already installed!"
                if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                    zypper --non-interactive install autoconf
                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
            f_pack_is_inst "libopenssl-devel" "zypper" "\"libopenssl-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install libopenssl-devel
            fi
            f_pack_is_inst "postgresql-devel" "zypper" "\"postgresql-devel\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install postgresql-devel
            fi
        fi
        F_COM_PKS_INSTALLED=1
    fi
}

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

# > -----------------------------------------
# Instalar o python3.2!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install python3.2?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_chk_by_path_hlp "$BASE_INST_DIR_V/py32" "d" "python3.2 already installed in \"$BASE_INST_DIR_V/py32\"!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
        f_common_packs
        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps
        tar -zxvf Python-3.2.2.tar.gz
        cd ./Python-3.2.2
        eval "./configure --prefix=$BASE_INST_DIR_V/py32/ --with-threads --enable-shared LDFLAGS=-Wl,-rpath=$BASE_INST_DIR_V/py32/lib/"
        make && make install
        cd ..
        rm -rf ./Python-3.2.2
        f_enter_to_cont "python3.2 installed in \"$BASE_INST_DIR_V/py32\"!"
    fi
fi
f_close_section

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

# > -----------------------------------------
# Instalar o virtualenv-1.11.6 no python3.2!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install virtualenv-1.11.6 on python3.2?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_chk_by_path_hlp "$BASE_INST_DIR_V/py32/bin/virtualenv-3.2" "f" "virtualenv-1.11.6 already installed in python3.2 (\"$BASE_INST_DIR_V/py32\")!"
    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
        $BASE_INST_DIR_V/py32/bin/python3.2 setup.py install
        cd ..
        rm -rf virtualenv-1.11.6
    fi
fi
f_close_section

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

# > -----------------------------------------
# Criar o ambiente virtual (python3.2)!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_enter_to_cont "Create the virtual environment (python3.2)!"
f_chk_by_path_hlp "$BASE_INST_DIR_V/ve32" "d" "Virtual environment (python3.2) already created in \"$BASE_INST_DIR_V/ve32\"!"
if [ ${F_CHK_BY_PATH_HLP_R} -eq 0 ] ; then
    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
    f_common_packs
    cd "$BASE_INST_DIR_V"
    $BASE_INST_DIR_V/py32/bin/virtualenv-3.2 ve32
    mkdir "$BASE_INST_DIR_V/ve32/src"
    f_enter_to_cont "Virtual environment created in \"$BASE_INST_DIR_V/ve32\"!"
fi
f_close_section

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

# > -----------------------------------------
# Instalar o Apache (httpd)!

HTTPD_SERV_NAME=""
HTTPD_USR=""
HTTPD_GRP=""
HTTPD_DEF_DOC_ROOT=""
if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    HTTPD_SERV_NAME="httpd"
    HTTPD_USR="apache"
    HTTPD_GRP="apache"
    HTTPD_DEF_DOC_ROOT="/var/www"
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    HTTPD_SERV_NAME="apache2"
    HTTPD_USR="www-data"
    HTTPD_GRP="www-data"
    HTTPD_DEF_DOC_ROOT="/var/www"
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    HTTPD_SERV_NAME="apache2"
    HTTPD_USR="wwwrun"
    HTTPD_GRP="www"
    HTTPD_DEF_DOC_ROOT="/srv/www"
fi

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install Apache ($HTTPD_SERV_NAME)?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_common_packs

    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        f_pack_is_inst "httpd-devel" "yum" "Apache ($HTTPD_SERV_NAME) already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            yum -y install httpd-devel httpd
            chkconfig httpd on
        fi
    elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
        f_pack_is_inst "apache2-dev" "apt-get" "Apache ($HTTPD_SERV_NAME) already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            apt-get -y install apache2-dev apache2
            update-rc.d -f $HTTPD_SERV_NAME remove
            update-rc.d $HTTPD_SERV_NAME defaults
        fi
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_pack_is_inst "apache2-devel" "zypper" "Apache ($HTTPD_SERV_NAME) already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            zypper --non-interactive install apache2-devel apache2
            chkconfig apache2 on
        fi
    fi
    eval "chown -R $HTTPD_USR $HTTPD_DEF_DOC_ROOT"
    eval "chown -R :$HTTPD_GRP $HTTPD_DEF_DOC_ROOT"
    eval "chmod -R 755 $HTTPD_DEF_DOC_ROOT"
    eval "service $HTTPD_SERV_NAME restart" &>/dev/null
fi
f_close_section

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

# > -----------------------------------------
# Abrir o firewall para o Apache (httpd)!

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 Apache ($HTTPD_SERV_NAME) (TCP 80)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 80 "Port 80 is already open!" 0 "ACCEPT" "tcp" "NEW"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                iptables -I INPUT 5 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
                service iptables save
                service iptables restart
            fi
        fi
        f_close_section
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_yes_no "Open firewall for Apache ($HTTPD_SERV_NAME) (TCP 80)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 80 "Port 80 is already open!" 0 "ACCEPT" "tcp"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                SuSEfirewall2 open EXT TCP 80
                SuSEfirewall2 stop
                SuSEfirewall2 start
            fi
        fi
    fi
    f_close_section
fi

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

# > -----------------------------------------
# Instalar o mod_wsgi-4.3.2!

if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    MOD_WSGI_PATH="/usr/lib64/httpd/modules/mod_wsgi.so"
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    MOD_WSGI_PATH="/usr/lib/apache2/modules/mod_wsgi.so"
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    MOD_WSGI_PATH="/usr/lib64/apache2/mod_wsgi.so"
fi

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install mod_wsgi-4.3.2?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_common_packs

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

    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        REPLACE_REF="include ld.so.conf.d/*.conf"
    elif [[ "$DISTRO_TYPE" == "DEB" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        REPLACE_REF="include /etc/ld.so.conf.d/*.conf"
    fi

    f_fl_cont_str "$BASE_INST_DIR_V/py32/lib" "/etc/ld.so.conf" "The file \"/etc/ld.so.conf\" probably has already been changed! Check it!"
    if [ ${FL_CONT_STR_R} -eq 0 ] ; then
        f_ez_sed "$REPLACE_REF" "$REPLACE_REF\n$BASE_INST_DIR_V/py32/lib" "/etc/ld.so.conf"
    fi

    UP_MOD_WSGI=1
    f_chk_by_path_hlp "$MOD_WSGI_PATH" "f" "mod_wsgi already installed in \"$MOD_WSGI_PATH\"!"
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        if [ ${EZ_I_SKIP_ON_V} -eq 0 ] ; then
            TITLE_F="Update/reinstall mod_wsgi? (\"y\" recommended)"
            f_div_section
            f_yes_no "$TITLE_F"
            f_div_section
            TITLE_F=""
            UP_MOD_WSGI=$YES_NO_R
            if [ ${UP_MOD_WSGI} -eq 1 ] ; then
                rm -rf "$MOD_WSGI_PATH"
            fi
        fi
    fi
    if [ ${UP_MOD_WSGI} -eq 1 ] ; then
        ldconfig
        tar -zxvf ./mod_wsgi-4.3.2.tar.gz
        cd ./mod_wsgi-4.3.2
        eval "./configure --with-python=$BASE_INST_DIR_V/py32/bin/python3.2"
        make
        make install
        cd ..
        rm -rf ./mod_wsgi-4.3.2
    fi
fi
f_close_section

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

# > -----------------------------------------
# Instalar o ElasticSearch!

ES_NAME="Elasticsearch 1.7.5"
ES_SERV_CMD="/etc/init.d/elasticsearch"
EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install $ES_NAME?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_common_packs
    f_chk_by_path_hlp "/usr/local/elasticsearch" "d" "\"elasticsearch-1.7.5.tar.gz\" already installed in \"/usr/local/elasticsearch\"!"
    F_BAK_MD_R=1
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        f_ez_mv_bak "/usr/local/elasticsearch" "Backup old version and update/reinstall? (\"y\" recommended)"
        if [ ${F_BAK_MD_R} -eq 1 ] ; then
            $ES_SERV_CMD stop &>/dev/null
            cd "$F_BAK_PATH_R"
            cd ..
            tar -cvvzf "$F_BAK_NAME_R.tar.gz" "./$F_BAK_NAME_R"
            rm -rf "$F_BAK_PATH_R"
        fi
    fi
    if [ ${F_BAK_MD_R} -eq 1 ] ; then
        if [[ "$DISTRO_TYPE" == "RH" ]] ; then
            f_pack_is_inst "java-1.7.0-openjdk" "yum" "java-1.7.0-openjdk already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                yum -y install java-1.7.0-openjdk
            fi
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            f_pack_is_inst "openjdk-7-jdk" "apt-get" "\"openjdk-7-jdk\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                apt-get -y install openjdk-7-jdk
            fi
        elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            f_pack_is_inst "java-1_7_0-openjdk" "zypper" "\"java-1_7_0-openjdk\" already installed!"
            if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
                zypper --non-interactive install java-1_7_0-openjdk
            fi
        fi
        cd "$SCRIPTDIR_V"
        cd ./other-srcs-n-apps
        tar -zxvf elasticsearch-1.7.5.tar.gz
        mv elasticsearch-1.7.5 /usr/local/elasticsearch
        tar -zxvf elasticsearch-servicewrapper-master.tar.gz
        mv elasticsearch-servicewrapper-master/service /usr/local/elasticsearch/bin/
        rm -rf elasticsearch-servicewrapper-master
        /usr/local/elasticsearch/bin/service/elasticsearch install
        $ES_SERV_CMD start
    fi
fi
f_close_section

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

# > -----------------------------------------
# Abrir o firewall para o ElasticSearch!

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 $ES_NAME? (TCP 9200)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 9200 "Port 9200 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 9200 -j ACCEPT
                service iptables save
                service iptables restart
            fi
        fi
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_yes_no "Open firewall for $ES_NAME? (TCP 9200)?"
        if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 9200 "Port 9200 is already open!" 0 "ACCEPT" "tcp"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                SuSEfirewall2 open EXT TCP 9200
                SuSEfirewall2 stop
                SuSEfirewall2 start
            fi
        fi
    fi
    f_close_section
fi

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

# > -----------------------------------------
# Instalar o postgresql!

PG_NAME=""
PG_SERV_NAME=""
PG_PACK_NAME=""
if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    PG_NAME="PostgreSQL 9.3"
    PG_SERV_NAME="postgresql-9.3"
    if [[ "$DISTRO_NAME" == "RedHat" ]] ; then
        PG_PACK_NAME="pgdg-redhat93-9.3-2.noarch.rpm"
    else
        PG_PACK_NAME="pgdg-centos93-9.3-2.noarch.rpm"
    fi
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    if [[ "$DISTRO_NAME" == "Debian" ]] ; then
        PG_NAME="PostgreSQL 9.4"
        PG_SERV_NAME="postgresql"
        PG_PACK_NAME="postgresql-9.4"
    else
        PG_NAME="PostgreSQL 9.3"
        PG_SERV_NAME="postgresql"
        PG_PACK_NAME="postgresql-9.3"
    fi
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    if [[ "$DISTRO_NAME" == "SLES" ]] ; then
        PG_NAME="PostgreSQL 9.4"
        PG_SERV_NAME="postgresql"
        PG_PACK_NAME="postgresql94-server"
    else
        PG_NAME="PostgreSQL 9.3"
        PG_SERV_NAME="postgresql"
        PG_PACK_NAME="postgresql93-server"
    fi
fi
EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
f_yes_no "Install $PG_NAME?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_common_packs

    cd "$SCRIPTDIR_V"
    cd ./other-srcs-n-apps
    if [[ "$DISTRO_TYPE" == "RH" ]] ; then
        f_pack_is_inst "postgresql93-server" "yum" "\"postgresql93-server\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            yum -y localinstall $PG_PACK_NAME
            yum -y install postgresql93-server
            service $PG_SERV_NAME initdb
            chkconfig $PG_SERV_NAME on
            service $PG_SERV_NAME start
        fi
    elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
        f_pack_is_inst "$PG_PACK_NAME" "apt-get" "\"$PG_PACK_NAME\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            apt-get -y install $PG_PACK_NAME
            update-rc.d -f $PG_SERV_NAME remove
            update-rc.d $PG_SERV_NAME defaults
            service $PG_SERV_NAME start
        fi
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_pack_is_inst "$PG_PACK_NAME" "zypper" "\"$PG_PACK_NAME\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            zypper --non-interactive install $PG_PACK_NAME
            chkconfig $PG_SERV_NAME on
            service $PG_SERV_NAME start
        fi
    fi
fi
f_close_section

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

# > -----------------------------------------
# Abrir o firewall para o postgresql!

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 $PG_NAME (TCP 5432)?"
        if [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 5432 "Port 5432 is already open!" 0 "ACCEPT" "tcp"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                iptables -I INPUT 1 -m tcp -p tcp --dport 5432 -j ACCEPT
                service iptables save
                service iptables restart
            fi
        fi
    elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
        f_yes_no "Open firewall for $PG_NAME (TCP 5432)?"
        if [ ${YES_NO_R} -eq 1 ] ; then
            f_chk_iptables 5432 "Port 5432 is already open!" 0 "ACCEPT" "tcp"
            if [ ${F_CHK_IPTABLES_R} -eq 1 ] ; then
                SuSEfirewall2 open EXT TCP 5432
                SuSEfirewall2 stop
                SuSEfirewall2 start
            fi
        fi
    fi
    f_close_section
fi

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

# > -----------------------------------------
# Configurar o postgresql!

CONF_PG=0
EZ_I_SKIP_ON_V=0
f_open_section
f_yes_no "Configure $PG_NAME (use \"y\" if you never did it)?"
PG_USER_F="lbu"
PG_PWD_F="lbu"
PG_EXT_ACS_F=""

if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    PG_HBA_PATH="/var/lib/pgsql/9.3/data/pg_hba.conf"
    PG_CONF_PATH="/var/lib/pgsql/9.3/data/postgresql.conf"
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    if [[ "$DISTRO_NAME" == "Debian" ]] ; then
        PG_HBA_PATH="/etc/postgresql/9.4/main/pg_hba.conf"
        PG_CONF_PATH="/etc/postgresql/9.4/main/postgresql.conf"
    else
        PG_HBA_PATH="/etc/postgresql/9.3/main/pg_hba.conf"
        PG_CONF_PATH="/etc/postgresql/9.3/main/postgresql.conf"
    fi
elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    PG_HBA_PATH="/var/lib/pgsql/data/pg_hba.conf"
    PG_CONF_PATH="/var/lib/pgsql/data/postgresql.conf"
fi

if [ ${YES_NO_R} -eq 1 ] ; then

    EZ_I_SKIP_ON_V=$SIMPLE_INST

    if [[ "$DISTRO_TYPE" == "DEB" ]] && [[ "$DISTRO_NAME" == "Debian" ]] ; then
        f_pack_is_inst "sudo" "apt-get" "\"sudo\" already installed!"
        if [ ${F_PACK_IS_INST_R} -eq 0 ] ; then
            apt-get -y install sudo
        fi
    fi

    f_div_section

    QUESTION_F="Enter the $PG_NAME user for lightbase (LBG). 
Use empty for \"$PG_USER_F\"!"

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

    f_div_section

    QUESTION_F="Enter the $PG_NAME password for \"$PG_USER_F\". 
Use empty for \"$PG_PWD_F\"!"

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

    f_div_section

    QUESTION_F="Enter one ip or ip range for external access.
e.g. \"172.20.143.89/32\" for a single host or \"172.20.143.0/24\" for a small network or \"10.6.0.0/16\" for a larger one!
Use empty for local access only!"

    f_get_usr_input "$QUESTION_F" 1
    QUESTION_F=""
    if [ -n "$GET_USR_INPUT_R" ] ; then
        PG_EXT_ACS_F=$GET_USR_INPUT_R
    fi
    EZ_I_SKIP_ON_V=0

    service $PG_SERV_NAME start &>/dev/null

    f_is_not_running "postgres" "ERROR! \"postgres\" service not running!"
    if [ ${F_IS_NOT_RUNNING_R} -eq 0 ] ; then
        cd /tmp
        f_get_stderr_stdout "sudo -u postgres psql -tAc \"SELECT * FROM pg_roles WHERE rolname='$PG_USER_F';\""
        if [[ $F_GET_STDOUT_R == *"$PG_USER_F|"* ]]; then
            # NOTE: 
            # rolname|
            # rolsuper|
            # rolinherit|
            # rolcreaterole|
            # rolcreatedb|
            # rolcatupdate|
            # rolcanlogin|
            # rolconnlimit|
            # rolpassword|
            # rolvaliduntil|
            # rolconfig|
            # oid
            # ! By Questor
            if [[ $F_GET_STDOUT_R != *"$PG_USER_F|t|t|t|t|t|t|f|-1|"* ]]; then
                f_div_section
                echo "The user \"$PG_USER_F\" already created, but something is apparently wrong with his roles. The expected roles are \"lbu|f|t|t|t|t|t|f|-1|...\"! DETAILS: \"$F_GET_STDOUT_R\"."
                f_div_section
                f_continue
            else
                EZ_I_SKIP_ON_V=$SIMPLE_INST
                f_enter_to_cont "The user \"$PG_USER_F\" already created and his roles are correct!"
                EZ_I_SKIP_ON_V=0
            fi
        else
            if [[ $F_GET_STDERR_R == "" ]]; then
                sudo -u postgres psql -c "CREATE USER $PG_USER_F INHERIT SUPERUSER CREATEROLE CREATEDB;"
                sudo -u postgres psql -c "ALTER USER $PG_USER_F PASSWORD '$PG_PWD_F';"
            else
                f_enter_to_cont "An error occurred when creating \"$PG_USER_F\" user permissions! ERROR: \"$F_GET_STDERR_R\"."
                f_error_exit
            fi
        fi
        cd - &>/dev/null
    else
        f_error_exit
    fi

    EZ_I_SKIP_ON_V=$SIMPLE_INST
    if [ -n "$PG_EXT_ACS_F" ] ; then
        f_fl_cont_str "host    all             all             $PG_EXT_ACS_F            trust" "$PG_HBA_PATH" "The file \"$PG_HBA_PATH\" probably has already been changed to enable external access! Check it!"
        if [ ${FL_CONT_STR_R} -eq 0 ] ; then
            f_ez_sed "# IPv4 local connections:" "# IPv4 local connections:\nhost    all             all             $PG_EXT_ACS_F            trust" "$PG_HBA_PATH"
        fi
    fi

    f_fl_cont_str "host    all             all             127.0.0.1/32            trust" "$PG_HBA_PATH" "The file \"$PG_HBA_PATH\" probably has already been changed to enable local access! Check it!"
    if [ ${FL_CONT_STR_R} -eq 0 ] ; then
        f_ez_sed "# IPv4 local connections:" "# IPv4 local connections:\nhost    all             all             127.0.0.1/32            trust" "$PG_HBA_PATH"
    fi

    f_fl_cont_str "listen_addresses = '*'" "$PG_CONF_PATH" "The file \"$PG_CONF_PATH\" probably has already been changed to set \"listen addresses\"! Check it!"
    if [ ${FL_CONT_STR_R} -eq 0 ] ; then
        f_ez_sed "# - Connection Settings -" "# - Connection Settings -\n\nlisten_addresses = '*'" "$PG_CONF_PATH"
    fi

    service $PG_SERV_NAME restart

    f_enter_to_cont "The file \"$PG_HBA_PATH\" has settings for $PG_NAME! Check it for more details!"
    EZ_I_SKIP_ON_V=0
    CONF_PG=1
else
    f_enter_to_cont "The file \"$PG_HBA_PATH\" has settings for $PG_NAME! Check it!"
fi
f_close_section

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

# > -----------------------------------------
# Criar as estruturas de dados básicas do LB no postgresql!

CREATE_LB_DT=0
EZ_I_SKIP_ON_V=0
f_open_section
f_yes_no "Create the basic LightBase (LBG) data structures in $PG_NAME (use \"y\" if you never did it)?"
PG_DB_F="lb"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then

    EZ_I_SKIP_ON_V=$SIMPLE_INST
    QUESTION_F="Enter the $PG_NAME database name for LightBase (LBG). 
Use empty for \"$PG_DB_F\" (recommended)!"

    f_get_usr_input "$QUESTION_F" 1
    QUESTION_F=""
    if [ -n "$GET_USR_INPUT_R" ] ; then
        PG_DB_F=$GET_USR_INPUT_R
    fi
    EZ_I_SKIP_ON_V=0
    f_is_not_running "postgres" "ERROR! \"postgres\" service not running!"

    if [ ${F_IS_NOT_RUNNING_R} -eq 0 ] ; then
        cd /tmp
        f_get_stderr_stdout "sudo -u postgres psql -c \"CREATE DATABASE $PG_DB_F;\""
        if [[ $F_GET_STDERR_R != "" ]]; then
            if [[ $F_GET_STDERR_R == *" already exists"* ]]; then
                EZ_I_SKIP_ON_V=$SIMPLE_INST
                f_enter_to_cont "The database \"$PG_DB_F\" already created!"
                EZ_I_SKIP_ON_V=0
            else
                f_div_section
                echo "An error occurred when creating \"$PG_DB_F\" database! ERROR: \"$F_GET_STDERR_R\"."
                f_div_section
                f_error_exit
            fi
        else
            cd "$SCRIPTDIR_V"
            cp ./lbn-basic-dt-strt/lbn_basic_dt_strt.sql /tmp
            cd /tmp
            chmod 700 lbn_basic_dt_strt.sql
            chown postgres lbn_basic_dt_strt.sql
            chown :postgres lbn_basic_dt_strt.sql
            sudo -u postgres psql $PG_DB_F -f lbn_basic_dt_strt.sql
            rm -f lbn_basic_dt_strt.sql
        fi
    else
        f_error_exit
    fi
    CREATE_LB_DT=1
else
    f_enter_to_cont "The file \"./lbn-basic-dt-strt/lbn_basic_dt_strt.sql\" has the basic LightBase (LBG) data structures! Check it!"
fi
f_close_section

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

# > -----------------------------------------
# Instalar as dependências python da LIB - liblightbase!

EZ_I_SKIP_ON_V=$SIMPLE_INST
cd "$SCRIPTDIR_V"
bash py-packs-liblightbase.sh "$EZ_I_SKIP_ON_V" "$BASE_INST_DIR_V"

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

# > -----------------------------------------
# Instalar a LIB - liblightbase!

EZ_I_SKIP_ON_V=$SIMPLE_INST
f_open_section
read -d '' TITLE_F <<"EOF"
Install the LIB - liblightbase?
EOF

f_yes_no "$TITLE_F"
TITLE_F=""
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then
    f_chk_by_path_hlp "$BASE_INST_DIR_V/ve32/src/liblightbase" "d" "\"liblightbase\" already installed in \"$BASE_INST_DIR_V/ve32/src\"!"
    F_BAK_MD_R=1
    if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
        f_ez_mv_bak "$BASE_INST_DIR_V/ve32/src/liblightbase" "Backup old version and update? (\"y\" recommended)"
    fi
    if [ ${F_BAK_MD_R} -eq 1 ] ; then
        cd "$SCRIPTDIR_V"
        tar -zxvf liblightbase.tar.gz
        mv "$SCRIPTDIR_V/liblightbase" "$BASE_INST_DIR_V/ve32/src/"
        cd "$BASE_INST_DIR_V/ve32/src/liblightbase"
        eval "$BASE_INST_DIR_V/ve32/bin/python3.2 setup.py install"
    fi
fi
f_close_section

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

# > -----------------------------------------
# Instalar e configurar o LBG - LBGenerator!

EZ_I_SKIP_ON_V=$SIMPLE_INST
cd "$SCRIPTDIR_V"
bash py-packs-LBGenerator.sh "$EZ_I_SKIP_ON_V" "$BASE_INST_DIR_V"

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

# > -----------------------------------------
# Instalar o LBG - LBGenerator!

if [[ "$DISTRO_TYPE" == "RH" ]] ; then
    VAR_LOG_HTTPD="/var/log/httpd"
elif [[ "$DISTRO_TYPE" == "DEB" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
    VAR_LOG_HTTPD="/var/log/apache2"
fi

EZ_I_SKIP_ON_V=$SIMPLE_INST
APP_URL_F="lbg"
f_open_section
f_yes_no "Install the LBG - LBGenerator?"
if [ ${EZ_I_SKIP_ON_V} -eq 1 ] || [ ${YES_NO_R} -eq 1 ] ; then

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

        f_enter_to_cont "Configure Apache ($HTTPD_SERV_NAME) for LBG - LBGenerator!"

        PG_CFG_F="postgresql://$PG_USER_F:$PG_PWD_F@127.0.0.1/$PG_DB_F"
        QUESTION_F="Enter the postgresql LBG configuration. 
Use empty for \"$PG_CFG_F\"!"

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

        QUESTION_F="Enter lightbase URL. 
Use empty for \"$APP_URL_F\" (used like e.g. http://<ip_or_name>/$APP_URL_F)!"
        f_div_section
        f_get_usr_input "$QUESTION_F" 1
        f_div_section
        QUESTION_F=""
        if [ -n "$GET_USR_INPUT_R" ] ; then
            APP_URL_F=$GET_USR_INPUT_R
        fi

        eval "cp -f \"$BASE_INST_DIR_V/ve32/src/LBGenerator/lbgenerator.wsgi-dist\" \"$BASE_INST_DIR_V/ve32/src/LBGenerator/lbgenerator.wsgi\""
        f_ez_sed "<VE32_PATH>" "$BASE_INST_DIR_V/ve32" "$BASE_INST_DIR_V/ve32/src/LBGenerator/lbgenerator.wsgi"

        eval "cp -f \"$BASE_INST_DIR_V/ve32/src/LBGenerator/production.ini-dist\" \"$BASE_INST_DIR_V/ve32/src/LBGenerator/production.ini\""
        f_ez_sed "<SQLALCHEMY_URL>" "$PG_CFG_F" "$BASE_INST_DIR_V/ve32/src/LBGenerator/production.ini" 1

        HTTPD_CONF_PATH=""
        if [[ "$DISTRO_TYPE" == "RH" ]] ; then
            HTTPD_CONF_PATH="/etc/httpd/conf.d"
        elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            HTTPD_CONF_PATH="/etc/apache2/sites-available"
        elif [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
            HTTPD_CONF_PATH="/etc/apache2/conf.d"
        fi

        f_chk_by_path_hlp "$HTTPD_CONF_PATH/lbg.conf" "f" "\"lbg.conf\" already created in \"$HTTPD_CONF_PATH/lbg.conf\"!"
        F_BAK_MD_R=1
        if [ ${F_CHK_BY_PATH_HLP_R} -eq 1 ] ; then
            f_ez_mv_bak "$HTTPD_CONF_PATH/lbg.conf" "Backup old version and update? (\"y\" recommended)"
        fi

        if [ ${F_BAK_MD_R} -eq 1 ] ; then
            eval "cp -f \"$SCRIPTDIR_V/other-srcs-n-apps/lbg.conf.dist\" \"$HTTPD_CONF_PATH/lbg.conf\""
            f_ez_sed "<MOD_WSGI_PATH>" "$MOD_WSGI_PATH" "$HTTPD_CONF_PATH/lbg.conf"
            f_ez_sed "<VE32_PATH>" "$BASE_INST_DIR_V/ve32" "$HTTPD_CONF_PATH/lbg.conf" 1
            f_ez_sed "<LBG_URL>" "$APP_URL_F" "$HTTPD_CONF_PATH/lbg.conf" 1
            f_ez_sed "<VAR_LOG_HTTPD>" "$VAR_LOG_HTTPD" "$HTTPD_CONF_PATH/lbg.conf" 1
            f_ez_sed "<HTTPD_USR>" "$HTTPD_USR" "$HTTPD_CONF_PATH/lbg.conf" 1
            f_ez_sed "<HTTPD_GRP>" "$HTTPD_GRP" "$HTTPD_CONF_PATH/lbg.conf" 1
            if [[ "$DISTRO_TYPE" == "RH" ]] ; then
                f_ez_sed "<DIR_RULES>" "Order allow,deny\n   Allow from all" "$HTTPD_CONF_PATH/lbg.conf"
            elif [[ "$DISTRO_TYPE" == "DEB" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]] ; then
                f_ez_sed "<DIR_RULES>" "AllowOverride None\n   Require all granted  " "$HTTPD_CONF_PATH/lbg.conf"
            fi
        fi

        if [[ "$DISTRO_TYPE" == "DEB" ]] ; then
            ln -s "$HTTPD_CONF_PATH/lbg.conf" "/etc/apache2/sites-enabled/lbg.conf" &>/dev/null
        fi

        eval "chown -R $HTTPD_USR $BASE_INST_DIR_V"
        eval "chown -R :$HTTPD_GRP $BASE_INST_DIR_V"
        eval "chmod -R 755 $BASE_INST_DIR_V"
        eval "service $HTTPD_SERV_NAME restart" &>/dev/null

    fi
fi
f_close_section

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

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

ERROR_LOG_FL=""
if [[ "$DISTRO_TYPE" == "RH" ]] || [[ "$DISTRO_TYPE" == "SUSE" ]]; then
    ERROR_LOG_FL="error_log"
elif [[ "$DISTRO_TYPE" == "DEB" ]] ; then
    ERROR_LOG_FL="error.log"
fi

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

# NOTE: A lógica imediatamente abaixo serve para os casos onde o 
# usuário optou por instalação simplificada para que ele seja informado 
# sobre quais foram as configurações de persistência setadas! By Questor
PERSIST_CONFIG=""
if [ ${SIMPLE_INST} -eq 1 ] && ([ ${CONF_PG} -eq 1 ] || [ ${CREATE_LB_DT} -eq 1 ]); then

    if [ ${CONF_PG} -eq 1 ] ; then
        CONF_PG_VL="
        user ...... $PG_USER_F
        password .. $PG_PWD_F"
    fi
    if [ ${CREATE_LB_DT} -eq 1 ] ; then
        CREATE_LB_DT_VL="
        database .... $PG_DB_F"
    fi

    PERSIST_CONFIG="
    LightBase (LBG) $PG_NAME persistence config...$CREATE_LB_DT_VL$CONF_PG_VL
"
fi

USEFUL_INFO_F="    To start/stop...

        Apache ($HTTPD_SERV_NAME)
            service $HTTPD_SERV_NAME start
            service $HTTPD_SERV_NAME stop

        $PG_NAME
            service $PG_SERV_NAME start
            service $PG_SERV_NAME stop

        $ES_NAME
            $ES_SERV_CMD start
            $ES_SERV_CMD stop
$PERSIST_CONFIG
    To access...
        http://<ip>/$APP_URL_F

    To configure...
        vi $BASE_INST_DIR_V/ve32/src/LBGenerator/production.ini

    To configure httpd (Apache)...
        vi $HTTPD_CONF_PATH/lbg.conf

    Log...
        less $VAR_LOG_HTTPD/$ERROR_LOG_FL"

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

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