home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / oss / cvs-2004 / bahasa / utilities / config_db.pl,v < prev    next >
Text File  |  2003-10-04  |  16KB  |  743 lines

  1. head    1.6;
  2. access;
  3. symbols
  4.     Initial:1.1.1.1 bule:1.1.1;
  5. locks; strict;
  6. comment    @# @;
  7.  
  8.  
  9. 1.6
  10. date    2003.10.04.18.22.57;    author bule;    state Exp;
  11. branches;
  12. next    1.5;
  13.  
  14. 1.5
  15. date    2003.10.01.06.51.28;    author bule;    state Exp;
  16. branches;
  17. next    1.4;
  18.  
  19. 1.4
  20. date    2003.08.21.20.20.57;    author bule;    state Exp;
  21. branches;
  22. next    1.3;
  23.  
  24. 1.3
  25. date    2003.08.19.06.20.01;    author bule;    state Exp;
  26. branches;
  27. next    1.2;
  28.  
  29. 1.2
  30. date    2003.08.18.19.07.10;    author bule;    state Exp;
  31. branches;
  32. next    1.1;
  33.  
  34. 1.1
  35. date    2003.08.12.19.18.23;    author bule;    state Exp;
  36. branches
  37.     1.1.1.1;
  38. next    ;
  39.  
  40. 1.1.1.1
  41. date    2003.08.12.19.18.23;    author bule;    state Exp;
  42. branches;
  43. next    ;
  44.  
  45.  
  46. desc
  47. @@
  48.  
  49.  
  50. 1.6
  51. log
  52. @*** empty log message ***
  53. @
  54. text
  55. @#!/usr/bin/perl -w
  56. #/////////////////////////////////////////////////////////////////////////////
  57. #/////////////////////////////////////////////////////////////////////////////
  58. #/ Project Name: Bahasa Indonesia Dictionary
  59. #/ Directory:    bahasa/utilities
  60. #/ File Name:    config_db.pl 
  61. #/ Author(s):    John L. Whiteman
  62. #/ Created:      June 23, 2003  
  63. #/ Modified:     October 04, 2003
  64. #/ Description:  The purpose of this PERL script is to assist you with 
  65. #/               configuring the database required for this project.
  66. #/               It doesn't setup the actual application.  Use 
  67. #/               config_app.pl to setup the latter.  This script 
  68. #/               should work for both MS Windows and UNIX
  69. #/               environments. 
  70. #/ 
  71. #/ Copyright (c) 2003 John L. Whiteman
  72. #/
  73. #/ Permission is herby granted, free of charge, to any person obtaining a 
  74. #/ copy of this software, data, and associated documentation files 
  75. #/ (the "Software"), to deal in the Software without restriction, 
  76. #/ including without limitation the rights to use, copy, modify, merge, 
  77. #/ publish, distribute, sublicense, and/or sell copies of Software, and to
  78. #/ permit persons to whom the Software is furnished to do so, subject to 
  79. #/ the following conditions:
  80. #/
  81. #/ The above copyright notice and this permission notice shall be 
  82. #/ included in all copies or substantial portions of the Software.
  83. #/
  84. #/ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  85. #/ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  86. #/ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  87. #/ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
  88. #/ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHERE IN AN ACTION OF CONTRACT,
  89. #/ TORT OR OTHERWISE, ARISING IN THE SOFTWARE.  
  90. #/////////////////////////////////////////////////////////////////////////////
  91. #/////////////////////////////////////////////////////////////////////////////
  92. use strict;
  93. use Cwd;
  94.  
  95. show_introduction();
  96.  
  97. while(1) {
  98.  
  99.     my ($response) = 
  100.     promptyn("Would you like to continue?");
  101.  
  102.     if (!$response) {
  103.  
  104.         print "\nInstallation script terminated!\n\n";
  105.  
  106.         exit;
  107.     }
  108.  
  109.     last;
  110. }
  111.  
  112. my (%DATABASE_CFG) = ();
  113.  
  114. check_mysql();
  115.  
  116. $DATABASE_CFG{"INSTALLATION_DIRECTORY"} =
  117. prompt_installation_directory();
  118.  
  119. my ($SLASH) = "/";
  120.  
  121. $SLASH = "\\"
  122. if ($DATABASE_CFG{"INSTALLATION_DIRECTORY"} =~ /\\/);
  123.  
  124. #These are the directories and files required to setup the database
  125. my (@@manifest) = (
  126. "mysql${SLASH}create.sql",
  127. "mysql${SLASH}administration.sql",
  128. "mysql${SLASH}administrators.sql",
  129. "mysql${SLASH}alphabet.sql",
  130. "mysql${SLASH}dictionary.sql",
  131. "mysql${SLASH}hits.sql",
  132. "mysql${SLASH}misses.sql",
  133. "mysql${SLASH}parts_of_speech.sql",
  134. "mysql${SLASH}pronunciations.sql",
  135. "mysql${SLASH}requests.sql",
  136. "mysql${SLASH}searches.sql",
  137. "mysql${SLASH}tasks.sql"
  138. );
  139.  
  140. my (@@modified_manifest) = ();
  141.  
  142. print "Verifying setup directories and files...\n\n";
  143.  
  144. #Now verify files and directories
  145. foreach(@@manifest) {
  146.  
  147.     my ($path) = $_;
  148.  
  149.     $path =
  150.     $DATABASE_CFG{INSTALLATION_DIRECTORY} . $SLASH. $path;
  151.  
  152.     push(@@modified_manifest, $path);
  153.  
  154.     print "${path} ";
  155.  
  156.     show_error("\nCan't find ${path}", -1) if (!-e "$path"); 
  157.  
  158.     print "[OK]\n";
  159. }
  160.  
  161. $DATABASE_CFG{"DB_HOST"} =
  162. prompt("What is the name or IP address of the database server?");
  163.  
  164. $DATABASE_CFG{"DB_ADMIN_PASSWORD"} =
  165. prompt("What is database administrator's password?");
  166.  
  167. $DATABASE_CFG{"DB_NAME"} =
  168. prompt("What name do you want to give the dictionary database?"); 
  169.  
  170. $DATABASE_CFG{"DB_USER"} =
  171. prompt("What name do you want to give the dictionary database user?");
  172.  
  173. $DATABASE_CFG{"DB_PASSWORD"} =
  174. prompt("What will be $DATABASE_CFG{DB_USER}'s password?"); 
  175.  
  176. #Verify that configuration values given are correct before
  177. #proceeding
  178. while(1) {
  179.  
  180.     print "\n[VERIFY THESE VALUES]\n\n";
  181.  
  182.     foreach(sort(keys(%DATABASE_CFG))) {
  183.  
  184.         my ($name) = $_;
  185.  
  186.         my ($value) = $DATABASE_CFG{$name};
  187.  
  188.         print "${name} = ${value}\n";
  189.     }
  190.  
  191.     my ($response) =
  192.     promptyn("Are these values correct?");
  193.  
  194.     if (!$response) {
  195.  
  196.         print "\nInstallation script terminated!\n\n";
  197.  
  198.         exit;
  199.     }
  200.  
  201.     last;
  202. }
  203.  
  204. create_database(
  205. $DATABASE_CFG{"DB_HOST"},
  206. $DATABASE_CFG{"DB_ADMIN_PASSWORD"},
  207. $DATABASE_CFG{"DB_NAME"},
  208. $DATABASE_CFG{"DB_USER"},
  209. $DATABASE_CFG{"DB_PASSWORD"}, \@@modified_manifest);
  210.  
  211. #----------------------------------------------------------------------------
  212. #Trys to run mysql to see if can be found.  If can't then it will terminate
  213. #script.
  214. #----------------------------------------------------------------------------
  215. sub check_mysql {
  216.  
  217.     my (@@output) = `mysql --help`; 
  218.     my ($found_mysql_flag) = 0;
  219.  
  220.     print "Verifying mySQL path...\n";
  221.  
  222.     foreach(@@output) {
  223.  
  224.         my ($o) = $_;
  225.  
  226.         trim(\$o);
  227.  
  228.         if (($o =~ /Usage/) || ($o =~ /mysql/)) {
  229.  
  230.             $found_mysql_flag++;
  231.         } 
  232.     }
  233.  
  234.     if ($found_mysql_flag >= 2) {
  235.  
  236.         print "Found it!\n";
  237.  
  238.     } else {
  239.  
  240.         show_error(
  241.         "Can't execute 'mysql' at the command line.", -1);
  242.     } 
  243.  
  244.     return;
  245. }
  246. #----------------------------------------------------------------------------
  247. #Attempts to create the database and permissions at the command line;
  248. #----------------------------------------------------------------------------
  249. sub create_database {
  250.  
  251.     my ($dbhost) = shift;
  252.     my ($dbadminpwd) = shift;
  253.     my ($dbname) = shift;
  254.     my ($dbuser) = shift;
  255.     my ($dbpwd) = shift;
  256.     my ($sql_files) = shift;
  257.     my ($rc);
  258.     my ($tmp_sql) = "tmp.sql";
  259.  
  260.     print 
  261.     "Creating database ${dbname} on ${dbhost} " .
  262.     "for user ${dbuser}...\n";
  263.  
  264.     my ($sql) = 
  265.     "DROP DATABASE IF EXISTS ${dbname};" .
  266.     "CREATE DATABASE ${dbname};" .
  267.     "USE ${dbname};" .
  268.     "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP " .
  269.     "ON ${dbname}.* TO ${dbuser}\@@${dbhost} IDENTIFIED BY '${dbpwd}'; ";
  270.  
  271.     if ($dbhost ne "localhost") {
  272.  
  273.         $sql .= 
  274.         " GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP " .
  275.         "ON ${dbname}.* TO ${dbuser}\@@localhost " . 
  276.         "IDENTIFIED BY '${dbpwd}';";
  277.     }
  278.  
  279.     unlink("$tmp_sql") if (-f "$tmp_sql");
  280.  
  281.     open(TMP_SQL_FILE, ">$tmp_sql") ||  
  282.     show_error("Can't create temporary sql file to create database " .
  283.     "${tmp_sql}:\n$!"); 
  284.  
  285.     print TMP_SQL_FILE "$sql";
  286.  
  287.     close(TMP_SQL_FILE);
  288.  
  289.     $rc = system("mysql --password=${dbadminpwd} < $tmp_sql");
  290.  
  291.     unlink("$tmp_sql");
  292.  
  293.     show_error("\nA problem occurred when creating database.", -1) 
  294.         if ($rc);
  295.  
  296.     print "Loading tables and data...\n\n"; 
  297.  
  298.     foreach(@@$sql_files) {
  299.  
  300.         my ($sql_file) = $_;
  301.  
  302.         print "Loading ${sql_file}...\n";
  303.         
  304.         #my ($mysql) =
  305.         #"mysql -u ${dbuser} -h ${dbhost} " .
  306.         #"--database=${dbname} --password=${dbpwd} " .
  307.         #"< \"${sql_file}\"";
  308.  
  309.         my ($mysql) =
  310.         "mysql --database=${dbname} --password=${dbadminpwd} " .
  311.         "< \"${sql_file}\"";
  312.  
  313.         $rc = system(${mysql});
  314.  
  315.         show_error(
  316.         "\nA problem occurred while loading tables " .
  317.         "and/or data.", -1) if ($rc);
  318.     }
  319.  
  320.     print "Database successfully created!\n\n"; 
  321.  
  322.     return;
  323. }
  324. #----------------------------------------------------------------------------
  325. #Returns the basename of a path
  326. #----------------------------------------------------------------------------
  327. sub get_basename {
  328.  
  329.     my ($path) = shift;
  330.     my (@@path_tree) = shift;
  331.  
  332.     if ($path =~ /\\/) {
  333.  
  334.         @@path_tree = split(/\\/, $path);
  335.  
  336.         return($path_tree[$#path_tree]);
  337.  
  338.     } elsif ($path =~ /\//) {
  339.  
  340.         @@path_tree = split(/\//, $path);
  341.  
  342.         return($path_tree[$#path_tree]);
  343.     }
  344.  
  345.     return($path);
  346. }
  347. #----------------------------------------------------------------------------
  348. #Generic prompt 
  349. #----------------------------------------------------------------------------
  350. sub prompt {
  351.  
  352.     my ($prompt) = shift;
  353.     my ($required_flag) = shift;
  354.     my ($response) = "";
  355.  
  356.     #Iterate until correct response is found
  357.     while(1) {
  358.  
  359.         #Show prompt message
  360.         print "\n${prompt}\n\n";
  361.  
  362.         #Grab response
  363.         $response = <STDIN>;
  364.  
  365.         #Remove leading and trailing white-spaces
  366.         trim(\$response);
  367.         
  368.         if (($response) eq "" && ($required_flag)) {
  369.  
  370.             show_error("This parameter requires a value!", 0);
  371.     
  372.             next;
  373.         } 
  374.  
  375.         last;
  376.     }
  377.  
  378.     return($response);
  379. }
  380. #----------------------------------------------------------------------------
  381. #Prompt for installation directory
  382. #----------------------------------------------------------------------------
  383. sub prompt_installation_directory {
  384.  
  385.     my ($pwd) = cwd();
  386.     my ($default_dir) = "";
  387.     my ($installation_directory) = "";
  388.  
  389.     if ($pwd) {
  390.  
  391.         if (-d "$pwd") {
  392.  
  393.             show_error(
  394.             "You must run this installation script " .
  395.             "under the same directory that it resides.", -1)
  396.                 if ($pwd !~ /bahasa\/utilities$/);
  397.  
  398.             $default_dir = $pwd;
  399.  
  400.             $default_dir =~ s/\/utilities$//;
  401.         } 
  402.     }
  403.  
  404.     if (-d "$default_dir") {
  405.  
  406.         while(1) {
  407.  
  408.             $installation_directory =
  409.             promptx(
  410.             "Enter the installation directory:\n", 
  411.             "$default_dir", 1);
  412.     
  413.             if (!-d "$installation_directory") {
  414.  
  415.                 show_error(
  416.                 "'${installation_directory}'" .
  417.                 " does not exists.  Please try again.", 0);
  418.  
  419.                 next;
  420.             }
  421.  
  422.             last;
  423.         }
  424.  
  425.     } else {
  426.  
  427.  
  428.         while(1) {
  429.     
  430.             $installation_directory =
  431.             prompt("Enter the installation directory: ", 1);
  432.  
  433.             if (!-d "$installation_directory") {
  434.  
  435.                 show_error(
  436.                 "'${installation_directory}'" .
  437.                 " does not exists.  Please try again.", 0);
  438.  
  439.                 next;
  440.             }
  441.  
  442.             last;
  443.         }
  444.     }
  445.  
  446.     #Remove trailing slash for UNIX systems if exists
  447.     $installation_directory =~ s/\/$//;
  448.  
  449.     #Remove trailing slash for Windows systems if exists
  450.     $installation_directory =~ s/\\$//;
  451.  
  452.     return("$installation_directory");
  453. }
  454. #----------------------------------------------------------------------------
  455. #Generic yes or no prompt 
  456. #----------------------------------------------------------------------------
  457. sub promptyn {
  458.  
  459.     my ($prompt) = shift;
  460.     my ($response);
  461.     my ($ucresponse);
  462.     my (%responses) = (YES => 1, NO => 1, Y => 1, N => 1);
  463.  
  464.     #Iterate until correct response is found
  465.     while(1) {
  466.  
  467.         #Show prompt message
  468.         print "\n${prompt} [y|n]\n\n";
  469.  
  470.         #Grab response
  471.         $response = <STDIN>;
  472.         
  473.         #Clean leading and trailing white spaces
  474.         trim(\$response);
  475.  
  476.         if ($response eq "") {
  477.  
  478.             show_error(
  479.             "Please respond with a 'y' or 'n' answer!", 0);
  480.  
  481.             next;
  482.  
  483.         }  else {
  484.  
  485.             $ucresponse = uc($response);
  486.  
  487.             if (exists($responses{$ucresponse})) {
  488.  
  489.                 last;
  490.  
  491.             } else {
  492.  
  493.                 show_error(
  494.                 "Please respond with a 'y' or 'n' answer!", 0);
  495.             }
  496.         }
  497.     }
  498.  
  499.     return(1) if ($ucresponse =~ /^Y/);
  500.  
  501.     return(0);
  502. }
  503. #----------------------------------------------------------------------------
  504. #Generic prompt with default
  505. #----------------------------------------------------------------------------
  506. sub promptx {
  507.  
  508.     my ($prompt) = shift;
  509.     my ($default) = shift;
  510.     my ($required_flag) = shift;
  511.     my ($response) = "";
  512.  
  513.     #Iterate until correct response is found
  514.     while(1) {
  515.  
  516.         print "\n${prompt} [${default}]\n\n";
  517.  
  518.         $response = <STDIN>;
  519.  
  520.         chomp($response);
  521.  
  522.         if ($response eq "") {
  523.  
  524.             $response = $default;
  525.  
  526.             last;
  527.  
  528.         } else {
  529.  
  530.             trim(\$response);
  531.  
  532.             if (($response) eq "" && ($required_flag)) {
  533.  
  534.                 show_error(
  535.                 "This parameter requires a value!", 0);
  536.  
  537.                 next;
  538.  
  539.             } else {
  540.  
  541.  
  542.                 last;
  543.             }
  544.         }
  545.     }
  546.  
  547.     return("$response");
  548. }
  549. #----------------------------------------------------------------------------
  550. #Display error message then terminate script
  551. #----------------------------------------------------------------------------
  552. sub show_error {
  553.  
  554.     my ($msg) = shift;
  555.     my ($exit_flag) = shift || 0;
  556.  
  557.     print "\nInstallation Error:\n${msg}\n\n";
  558.  
  559.     exit(-1) if ($exit_flag);
  560.  
  561.     return;
  562. }
  563. #----------------------------------------------------------------------------
  564. #Introduction message
  565. #----------------------------------------------------------------------------
  566. sub show_introduction {
  567.  
  568.  
  569.     print <<EOF;
  570.  
  571. /////////////////////////////////////////////////////////////////////////////
  572.            BAHASA INDONESIA DICTIONARY DATABASE CREATION SCRIPT
  573. /////////////////////////////////////////////////////////////////////////////
  574.  
  575. Copyright (c) 2003 John L. Whiteman
  576.  
  577. Permission is herby granted, free of charge, to any person obtaining a 
  578. copy of this software, data, and associated documentation files 
  579. (the "Software"), to deal in the Software without restriction, 
  580. including without limitation the rights to use, copy, modify, merge, 
  581. publish, distribute, sublicense, and/or sell copies of Software, and to
  582. permit persons to whom the Software is furnished to do so, subject to 
  583. the following conditions:
  584.  
  585. The above copyright notice and this permission notice shall be 
  586. included in all copies or substantial portions of the Software.
  587.  
  588. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  589. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  590. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  591. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
  592. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHERE IN AN ACTION OF CONTRACT,
  593. TORT OR OTHERWISE, ARISING IN THE SOFTWARE.  
  594.  
  595. /////////////////////////////////////////////////////////////////////////////
  596.  
  597. The purpose of this script is to assist you 
  598. with creating the mySQL database for the 
  599. Bahasa Indonesia Dictionary. 
  600.  
  601. This script assumes that you have MYSQL 
  602. installed and running on this system.
  603.  
  604. I've tried to make this script as portable 
  605. as possible.  If for some reason this script 
  606. fails then you will have to create the 
  607. database manually.  See documentation 
  608. for more details. 
  609.  
  610. Please feel free to modify this script to what 
  611. best fits your needs.
  612.  
  613. You can abort this script at any time by 
  614. pressing CTRL C.
  615.  
  616. /////////////////////////////////////////////////////////////////////////////
  617.  
  618. EOF
  619.  
  620.     return;
  621. }
  622. #----------------------------------------------------------------------------
  623. #Trim leading and trailing white-spaces from string token.  Pass token by
  624. #reference only.
  625. #----------------------------------------------------------------------------
  626. sub trim {
  627.  
  628.     my ($token) = shift;
  629.  
  630.     return if (!$token);
  631.  
  632.     $$token =~ s/^\s+//;
  633.     $$token =~ s/\s+$//;
  634.  
  635.     return;
  636. }
  637. #----------------------------------------------------------------------------
  638. #----------------------------------------------------------------------------
  639. @
  640.  
  641.  
  642. 1.5
  643. log
  644. @*** empty log message ***
  645. @
  646. text
  647. @d9 1
  648. a9 1
  649. #/ Modified:     September 30, 2003
  650. d110 3
  651. d152 1
  652. d198 1
  653. d215 9
  654. a223 1
  655.     "ON ${dbname}.* TO ${dbuser}\@@${dbhost} IDENTIFIED BY '${dbpwd}';";
  656. d235 1
  657. a235 1
  658.     $rc = system("mysql < $tmp_sql");
  659. d250 5
  660. d256 1
  661. a256 2
  662.         "mysql -u ${dbuser} -h ${dbhost} " .
  663.         "--database=${dbname} --password=${dbpwd} " .
  664. @
  665.  
  666.  
  667. 1.4
  668. log
  669. @*** empty log message ***
  670. @
  671. text
  672. @d9 1
  673. a9 1
  674. #/ Modified:     August 21, 2003
  675. d80 1
  676. a80 1
  677. "mysql${SLASH}pronunciation.sql",
  678. @
  679.  
  680.  
  681. 1.3
  682. log
  683. @*** empty log message ***
  684. @
  685. text
  686. @d9 1
  687. a9 1
  688. #/ Modified:     August 18, 2003
  689. a496 1
  690.     print "\n\n";
  691. d498 1
  692. a498 1
  693.     print "-" x 77;
  694. d500 23
  695. a522 1
  696.     print "\n";
  697. d524 1
  698. a524 2
  699.     print <<EOF;
  700. [BAHASA INDONESIA DICTIONARY DATABASE CREATION]
  701. d539 2
  702. a540 2
  703. Please feel free to modify this script to best 
  704. fit your needs.
  705. d545 2
  706. a547 4
  707.  
  708.     print "-" x 77;
  709.  
  710.     print "\n";
  711. @
  712.  
  713.  
  714. 1.2
  715. log
  716. @*** empty log message ***
  717. @
  718. text
  719. @d2 36
  720. a549 1
  721.  
  722. @
  723.  
  724.  
  725. 1.1
  726. log
  727. @Initial revision
  728. @
  729. text
  730. @d163 1
  731. d176 13
  732. a188 1
  733.     $rc = system("echo \"${sql}\" | mysql");
  734. @
  735.  
  736.  
  737. 1.1.1.1
  738. log
  739. @Bahasa Indonesia Dictionary
  740. @
  741. text
  742. @@
  743.