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 < prev    next >
Perl Script  |  2003-08-12  |  10KB  |  502 lines

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Cwd;
  4.  
  5. show_introduction();
  6.  
  7. while(1) {
  8.  
  9.     my ($response) = 
  10.     promptyn("Would you like to continue?");
  11.  
  12.     if (!$response) {
  13.  
  14.         print "\nInstallation script terminated!\n\n";
  15.  
  16.         exit;
  17.     }
  18.  
  19.     last;
  20. }
  21.  
  22. my (%DATABASE_CFG) = ();
  23.  
  24. check_mysql();
  25.  
  26. $DATABASE_CFG{"INSTALLATION_DIRECTORY"} =
  27. prompt_installation_directory();
  28.  
  29. my ($SLASH) = "/";
  30.  
  31. $SLASH = "\\"
  32. if ($DATABASE_CFG{"INSTALLATION_DIRECTORY"} =~ /\\/);
  33.  
  34. #These are the directories and files required to setup the database
  35. my (@manifest) = (
  36. "mysql${SLASH}create.sql",
  37. "mysql${SLASH}administration.sql",
  38. "mysql${SLASH}administrators.sql",
  39. "mysql${SLASH}alphabet.sql",
  40. "mysql${SLASH}dictionary.sql",
  41. "mysql${SLASH}hits.sql",
  42. "mysql${SLASH}misses.sql",
  43. "mysql${SLASH}parts_of_speech.sql",
  44. "mysql${SLASH}pronunciation.sql",
  45. "mysql${SLASH}requests.sql",
  46. "mysql${SLASH}searches.sql",
  47. "mysql${SLASH}tasks.sql"
  48. );
  49.  
  50. my (@modified_manifest) = ();
  51.  
  52. print "Verifying setup directories and files...\n\n";
  53.  
  54. #Now verify files and directories
  55. foreach(@manifest) {
  56.  
  57.     my ($path) = $_;
  58.  
  59.     $path =
  60.     $DATABASE_CFG{INSTALLATION_DIRECTORY} . $SLASH. $path;
  61.  
  62.     push(@modified_manifest, $path);
  63.  
  64.     print "${path} ";
  65.  
  66.     show_error("\nCan't find ${path}", -1) if (!-e "$path"); 
  67.  
  68.     print "[OK]\n";
  69. }
  70.  
  71. $DATABASE_CFG{"DB_HOST"} =
  72. prompt("What is the name or IP address of the database server?");
  73.  
  74. $DATABASE_CFG{"DB_NAME"} =
  75. prompt("What name do you want to give the dictionary database?"); 
  76.  
  77. $DATABASE_CFG{"DB_USER"} =
  78. prompt("What name do you want to give the dictionary database user?");
  79.  
  80. $DATABASE_CFG{"DB_PASSWORD"} =
  81. prompt("What will be $DATABASE_CFG{DB_USER}'s password?"); 
  82.  
  83. #Verify that configuration values given are correct before
  84. #proceeding
  85. while(1) {
  86.  
  87.     print "\n[VERIFY THESE VALUES]\n\n";
  88.  
  89.     foreach(sort(keys(%DATABASE_CFG))) {
  90.  
  91.         my ($name) = $_;
  92.  
  93.         my ($value) = $DATABASE_CFG{$name};
  94.  
  95.         print "${name} = ${value}\n";
  96.     }
  97.  
  98.     my ($response) =
  99.     promptyn("Are these values correct?");
  100.  
  101.     if (!$response) {
  102.  
  103.         print "\nInstallation script terminated!\n\n";
  104.  
  105.         exit;
  106.     }
  107.  
  108.     last;
  109. }
  110.  
  111. create_database(
  112. $DATABASE_CFG{"DB_HOST"},
  113. $DATABASE_CFG{"DB_NAME"},
  114. $DATABASE_CFG{"DB_USER"},
  115. $DATABASE_CFG{"DB_PASSWORD"}, \@modified_manifest);
  116.  
  117. #----------------------------------------------------------------------------
  118. #Trys to run mysql to see if can be found.  If can't then it will terminate
  119. #script.
  120. #----------------------------------------------------------------------------
  121. sub check_mysql {
  122.  
  123.     my (@output) = `mysql --help`; 
  124.     my ($found_mysql_flag) = 0;
  125.  
  126.     print "Verifying mySQL path...\n";
  127.  
  128.     foreach(@output) {
  129.  
  130.         my ($o) = $_;
  131.  
  132.         trim(\$o);
  133.  
  134.         if (($o =~ /Usage/) || ($o =~ /mysql/)) {
  135.  
  136.             $found_mysql_flag++;
  137.         } 
  138.     }
  139.  
  140.     if ($found_mysql_flag >= 2) {
  141.  
  142.         print "Found it!\n";
  143.  
  144.     } else {
  145.  
  146.         show_error(
  147.         "Can't execute 'mysql' at the command line.", -1);
  148.     } 
  149.  
  150.     return;
  151. }
  152. #----------------------------------------------------------------------------
  153. #Attempts to create the database and permissions at the command line;
  154. #----------------------------------------------------------------------------
  155. sub create_database {
  156.  
  157.     my ($dbhost) = shift;
  158.     my ($dbname) = shift;
  159.     my ($dbuser) = shift;
  160.     my ($dbpwd) = shift;
  161.     my ($sql_files) = shift;
  162.     my ($rc);
  163.  
  164.     print 
  165.     "Creating database ${dbname} on ${dbhost} " .
  166.     "for user ${dbuser}...\n";
  167.  
  168.     my ($sql) = 
  169.     "DROP DATABASE IF EXISTS ${dbname};" .
  170.     "CREATE DATABASE ${dbname};" .
  171.     "USE ${dbname};" .
  172.     "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP " .
  173.     "ON ${dbname}.* TO ${dbuser}\@${dbhost} IDENTIFIED BY '${dbpwd}';";
  174.  
  175.     $rc = system("echo \"${sql}\" | mysql");
  176.  
  177.     show_error("\nA problem occurred when creating database.", -1) 
  178.         if ($rc);
  179.  
  180.     print "Loading tables and data...\n\n"; 
  181.  
  182.     foreach(@$sql_files) {
  183.  
  184.         my ($sql_file) = $_;
  185.  
  186.         print "Loading ${sql_file}...\n";
  187.         
  188.         my ($mysql) =
  189.         "mysql -u ${dbuser} -h ${dbhost} " .
  190.         "--database=${dbname} --password=${dbpwd} " .
  191.         "< \"${sql_file}\"";
  192.  
  193.         $rc = system(${mysql});
  194.  
  195.         show_error(
  196.         "\nA problem occurred while loading tables " .
  197.         "and/or data.", -1) if ($rc);
  198.     }
  199.  
  200.     print "Database successfully created!\n\n"; 
  201.  
  202.     return;
  203. }
  204. #----------------------------------------------------------------------------
  205. #Returns the basename of a path
  206. #----------------------------------------------------------------------------
  207. sub get_basename {
  208.  
  209.     my ($path) = shift;
  210.     my (@path_tree) = shift;
  211.  
  212.     if ($path =~ /\\/) {
  213.  
  214.         @path_tree = split(/\\/, $path);
  215.  
  216.         return($path_tree[$#path_tree]);
  217.  
  218.     } elsif ($path =~ /\//) {
  219.  
  220.         @path_tree = split(/\//, $path);
  221.  
  222.         return($path_tree[$#path_tree]);
  223.     }
  224.  
  225.     return($path);
  226. }
  227. #----------------------------------------------------------------------------
  228. #Generic prompt 
  229. #----------------------------------------------------------------------------
  230. sub prompt {
  231.  
  232.     my ($prompt) = shift;
  233.     my ($required_flag) = shift;
  234.     my ($response) = "";
  235.  
  236.     #Iterate until correct response is found
  237.     while(1) {
  238.  
  239.         #Show prompt message
  240.         print "\n${prompt}\n\n";
  241.  
  242.         #Grab response
  243.         $response = <STDIN>;
  244.  
  245.         #Remove leading and trailing white-spaces
  246.         trim(\$response);
  247.         
  248.         if (($response) eq "" && ($required_flag)) {
  249.  
  250.             show_error("This parameter requires a value!", 0);
  251.     
  252.             next;
  253.         } 
  254.  
  255.         last;
  256.     }
  257.  
  258.     return($response);
  259. }
  260. #----------------------------------------------------------------------------
  261. #Prompt for installation directory
  262. #----------------------------------------------------------------------------
  263. sub prompt_installation_directory {
  264.  
  265.     my ($pwd) = cwd();
  266.     my ($default_dir) = "";
  267.     my ($installation_directory) = "";
  268.  
  269.     if ($pwd) {
  270.  
  271.         if (-d "$pwd") {
  272.  
  273.             show_error(
  274.             "You must run this installation script " .
  275.             "under the same directory that it resides.", -1)
  276.                 if ($pwd !~ /bahasa\/utilities$/);
  277.  
  278.             $default_dir = $pwd;
  279.  
  280.             $default_dir =~ s/\/utilities$//;
  281.         } 
  282.     }
  283.  
  284.     if (-d "$default_dir") {
  285.  
  286.         while(1) {
  287.  
  288.             $installation_directory =
  289.             promptx(
  290.             "Enter the installation directory:\n", 
  291.             "$default_dir", 1);
  292.     
  293.             if (!-d "$installation_directory") {
  294.  
  295.                 show_error(
  296.                 "'${installation_directory}'" .
  297.                 " does not exists.  Please try again.", 0);
  298.  
  299.                 next;
  300.             }
  301.  
  302.             last;
  303.         }
  304.  
  305.     } else {
  306.  
  307.  
  308.         while(1) {
  309.     
  310.             $installation_directory =
  311.             prompt("Enter the installation directory: ", 1);
  312.  
  313.             if (!-d "$installation_directory") {
  314.  
  315.                 show_error(
  316.                 "'${installation_directory}'" .
  317.                 " does not exists.  Please try again.", 0);
  318.  
  319.                 next;
  320.             }
  321.  
  322.             last;
  323.         }
  324.     }
  325.  
  326.     #Remove trailing slash for UNIX systems if exists
  327.     $installation_directory =~ s/\/$//;
  328.  
  329.     #Remove trailing slash for Windows systems if exists
  330.     $installation_directory =~ s/\\$//;
  331.  
  332.     return("$installation_directory");
  333. }
  334. #----------------------------------------------------------------------------
  335. #Generic yes or no prompt 
  336. #----------------------------------------------------------------------------
  337. sub promptyn {
  338.  
  339.     my ($prompt) = shift;
  340.     my ($response);
  341.     my ($ucresponse);
  342.     my (%responses) = (YES => 1, NO => 1, Y => 1, N => 1);
  343.  
  344.     #Iterate until correct response is found
  345.     while(1) {
  346.  
  347.         #Show prompt message
  348.         print "\n${prompt} [y|n]\n\n";
  349.  
  350.         #Grab response
  351.         $response = <STDIN>;
  352.         
  353.         #Clean leading and trailing white spaces
  354.         trim(\$response);
  355.  
  356.         if ($response eq "") {
  357.  
  358.             show_error(
  359.             "Please respond with a 'y' or 'n' answer!", 0);
  360.  
  361.             next;
  362.  
  363.         }  else {
  364.  
  365.             $ucresponse = uc($response);
  366.  
  367.             if (exists($responses{$ucresponse})) {
  368.  
  369.                 last;
  370.  
  371.             } else {
  372.  
  373.                 show_error(
  374.                 "Please respond with a 'y' or 'n' answer!", 0);
  375.             }
  376.         }
  377.     }
  378.  
  379.     return(1) if ($ucresponse =~ /^Y/);
  380.  
  381.     return(0);
  382. }
  383. #----------------------------------------------------------------------------
  384. #Generic prompt with default
  385. #----------------------------------------------------------------------------
  386. sub promptx {
  387.  
  388.     my ($prompt) = shift;
  389.     my ($default) = shift;
  390.     my ($required_flag) = shift;
  391.     my ($response) = "";
  392.  
  393.     #Iterate until correct response is found
  394.     while(1) {
  395.  
  396.         print "\n${prompt} [${default}]\n\n";
  397.  
  398.         $response = <STDIN>;
  399.  
  400.         chomp($response);
  401.  
  402.         if ($response eq "") {
  403.  
  404.             $response = $default;
  405.  
  406.             last;
  407.  
  408.         } else {
  409.  
  410.             trim(\$response);
  411.  
  412.             if (($response) eq "" && ($required_flag)) {
  413.  
  414.                 show_error(
  415.                 "This parameter requires a value!", 0);
  416.  
  417.                 next;
  418.  
  419.             } else {
  420.  
  421.  
  422.                 last;
  423.             }
  424.         }
  425.     }
  426.  
  427.     return("$response");
  428. }
  429. #----------------------------------------------------------------------------
  430. #Display error message then terminate script
  431. #----------------------------------------------------------------------------
  432. sub show_error {
  433.  
  434.     my ($msg) = shift;
  435.     my ($exit_flag) = shift || 0;
  436.  
  437.     print "\nInstallation Error:\n${msg}\n\n";
  438.  
  439.     exit(-1) if ($exit_flag);
  440.  
  441.     return;
  442. }
  443. #----------------------------------------------------------------------------
  444. #Introduction message
  445. #----------------------------------------------------------------------------
  446. sub show_introduction {
  447.  
  448.     print "\n\n";
  449.  
  450.     print "-" x 77;
  451.  
  452.     print "\n";
  453.  
  454.     print <<EOF;
  455. [BAHASA INDONESIA DICTIONARY DATABASE CREATION]
  456.  
  457. The purpose of this script is to assist you 
  458. with creating the mySQL database for the 
  459. Bahasa Indonesia Dictionary. 
  460.  
  461. This script assumes that you have MYSQL 
  462. installed and running on this system.
  463.  
  464. I've tried to make this script as portable 
  465. as possible.  If for some reason this script 
  466. fails then you will have to create the 
  467. database manually.  See documentation 
  468. for more details. 
  469.  
  470. Please feel free to modify this script to best 
  471. fit your needs.
  472.  
  473. You can abort this script at any time by 
  474. pressing CTRL C.
  475.  
  476. EOF
  477.  
  478.     print "-" x 77;
  479.  
  480.     print "\n";
  481.  
  482.     return;
  483. }
  484. #----------------------------------------------------------------------------
  485. #Trim leading and trailing white-spaces from string token.  Pass token by
  486. #reference only.
  487. #----------------------------------------------------------------------------
  488. sub trim {
  489.  
  490.     my ($token) = shift;
  491.  
  492.     return if (!$token);
  493.  
  494.     $$token =~ s/^\s+//;
  495.     $$token =~ s/\s+$//;
  496.  
  497.     return;
  498. }
  499. #----------------------------------------------------------------------------
  500. #----------------------------------------------------------------------------
  501.  
  502.