[ PHPXref.com ] [ Generated: Sun Jul 20 18:53:21 2008 ] [ myPhile 1.0.2 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> install.sh (source)

   1  #!/bin/bash
   2  ##################################################################
   3  # Script Name: install.sh  
   4  # Created by:    geoff mcnamara geoffm@rni.net
   5  # Created on:    12/6/01
   6  # Purpose:      for installation of myPhile
   7  # How initiated: ./install.sh
   8  # Valid User:    as root
   9  # Parameters Expected:
  10  #           
  11  # Output:  creates myphile (or user selected) database
  12  #         with needed entries in tables for use
  13  # Notes:      
  14  #            
  15  # $Id: install.sh,v 1.32 2005-08-31 22:24:13 geoffm Exp $
  16  #
  17  ###################################################################
  18  
  19  ##################
  20  # Global Variables
  21  ##################
  22  SQL_DUMP_FILE=myphile.sql
  23  DEBUG=1  # Level 1 or 2 accepted
  24  GDATE=`date +%Y%m%d-%H:%M`
  25  LINE="============================================================"
  26  # Make sure the PATH covers the basics 
  27  PATH=$PATH:/usr/bin:/usr/local/bin:/usr/ucb
  28  ERRFLAG=1000   # MAKE SURE that you reset this to 0 if all goes well!!
  29  HOST=`hostname`
  30  WHOAMI_PRG="whoami "
  31  TMP_FILE=/tmp/`basename $0`.$$
  32  BASE_DIR=`dirname $0`
  33  LOG_DIR="$BASE_DIR"
  34  LOG_FILE=$LOG_DIR/`basename $0`.log
  35  BLACK=''
  36  RED=''
  37  GREEN=''
  38  YELLOW=''
  39  BLUE=''
  40  MAGENTA=''
  41  CYAN=''
  42  WHITE=''
  43  BRIGHT=''
  44  BLINK=''
  45  REVERSE=''
  46  NORMAL=''
  47  
  48  #####################
  49  ## Functions
  50  #####################
  51  
  52  #############
  53  handleOPTS ()
  54  #############
  55  {
  56  # call this function with "handleOPTS $*"
  57  # --- Command line Options/Arguments ---
  58    while getopts "mhu*" opt; do
  59    case $opt in
  60      m )
  61        doMENU
  62        ;;
  63      h )
  64        doUSAGE 
  65        ERRFLAG=1
  66        doEXIT
  67        ;;
  68      u )
  69        doUPGRADE
  70        ;;
  71      * )
  72        doUSAGE
  73        ERRFLAG=1
  74        doEXIT
  75        ;;
  76      esac
  77    done
  78  
  79    shift `expr $OPTIND - 1`
  80  
  81    if [ $# -gt 0 ]; then
  82      doUSAGE
  83      ERRFLAG=1
  84      doEXIT
  85    fi
  86  }
  87  
  88  ##########
  89  doUSAGE ()
  90  ##########
  91  {
  92    echo Usage: `basename $0` [-hu] 
  93    echo "        Purpose: install script for myphile application"
  94    echo "        -h            Will show this usage info..."
  95    echo "        -u            Upgrade your current myphile application"
  96    ERRFLAG=1
  97    doEXIT "Have a nice day!"
  98  }
  99  
 100  ##########
 101  doTITLE ()
 102  ##########
 103  {
 104    #echo
 105    echo $LINE
 106    echo "   $1"
 107    echo $LINE
 108  }
 109  
 110  ###########
 111  doSUBTITLE ()
 112  ###########
 113  {
 114    #echo 
 115    echo "+---------------------------------------"
 116    echo "| *** $1"
 117    echo "+---------------------------------------"
 118  }
 119  
 120  ##############
 121  addLOG_ENTRY ()
 122  ##############
 123  {
 124    if [ "x$LOGFLAG" = "xYES" ]; then
 125      # This will allow you to add a time stamped log entry
 126      # Expects a string messages as the parameter
 127      echo "`date +%Y%m%d-%H:%M` - $@"  >>$LOG_FILE
 128    fi
 129  }
 130  
 131  ###########
 132  doPURIFY ()
 133  ###########
 134  {
 135  # This function will "purify" any input - stripping out all
 136  # comments regardless of position in a line
 137  # Can be called with a filename or just as a pipe
 138  # To call as a pipe - do this cat /etc/hosts | doPURIFY
 139  # and this will strip out all comments in /ect/hosts
 140  if [ "x$1" = "x" ]; then
 141    # we did not receive a filename
 142    sed -e "s/#.*$//" | sed "/^$/d"
 143   else
 144    # we received a filename
 145    cat $1 |sed -e "s/#.*$//" | sed "/^$/d"
 146  fi
 147  }
 148  
 149  #################
 150  doEXIT ()
 151  #################
 152  {
 153    #echo "  $LINE"
 154    printf "\n"
 155    if [ "x$*" != "x" ]; then
 156      echo $*
 157    fi
 158    if [ -f $TMP_FILE ] ; then
 159      rm -f $TMP_FILE
 160    fi
 161    if [ -f $EMAIL_FILE ]; then
 162      rm -f $EMAIL_FILE
 163    fi
 164    if [ $ERRFLAG = 0 ]; then
 165      echo Done... Enjoy!
 166      addLOG_ENTRY "Success: $0 script exiting..."
 167      exit $ERRFLAG
 168    else
 169      if [ $ERRFLAG = 1000 ]; then
 170        echo Make sure you change the ERRFLAG in the script!
 171      fi
 172      echo Error code: $ERRFLAG
 173      echo Now exiting...
 174      exit $ERRFLAG
 175    fi
 176  }
 177  
 178  #########
 179  doMENU ()
 180  #########
 181  {
 182    clear
 183    cat << EOF
 184  
 185  
 186  
 187  
 188  
 189    $RED}EXPERIMENTAL$NORMAL} - Install menu for myphile
 190    $LINE
 191    1 ) Do a first time install of myphile
 192    2 ) Dump the current myphile database to myphile.sql
 193    3 ) Update myphile
 194    q ) Quit
 195  
 196    $LINE
 197  EOF
 198    printf "  Please make your selection: "
 199    read SELECTION
 200    case $SELECTION in
 201      1 ) 
 202        doINSTALL
 203        ;;
 204      2 )
 205        echo "This can be used for backup"
 206        echo "It was used to build the myphile.sql for the install process."
 207        if askYN "Do you want to continue" y ; then
 208          mysqldump --opt -u root -p myphile > myphile.sql
 209          cat myphile.sql
 210          echo $LINE
 211          echo Done!
 212          stty sane
 213        fi
 214        ;;
 215      3 )
 216        doUPGRADE
 217        ;;
 218      q )
 219        ERRFLAG=0
 220        doEXIT User selected QUIT...
 221        ;;
 222      * )
 223        echo Nothing selected please try again...
 224        read ANS
 225        ;;
 226    esac 
 227    doMENU
 228  }
 229  
 230  ############
 231  askYN ()
 232  #############
 233  {
 234  # expects papar1 to be prompt strin
 235  # param2 is optional default ans (case does not matter)
 236  # default to N without any other direction...
 237  # alwasy returns a capital Y or N in ANS variable
 238  # this function will return 0 if the ANSwer is Y else it returns 1
 239  # makes it easy to use with if promtYESNO "Are you happy" y ; then ...
 240  #
 241    ANS="N"
 242  
 243    if [ $# -lt 1 ]; then
 244      ERRFLAG=1
 245      doEXIT "This function requires more paramters [askYN]"
 246    fi
 247  
 248    # Set the default
 249    case "$2" in
 250      [Yy]|[Yy][Ee][Ss] )
 251        ANS="Y"
 252        ;;
 253      [Nn]|[Nn][Oo] )
 254        ANS="N"
 255        ;;
 256    esac
 257  
 258    while : ; do
 259      printf "$1 (y/n [$ANS]): "
 260      read myANS
 261      case "$myANS" in
 262        [Yy]|[Yy][Ee][Ss] )
 263          ANS="Y"
 264          break
 265          ;;
 266        [Nn]|[Nn][Oo] )
 267          ANS="N"
 268          break
 269          ;;
 270        * )
 271          break
 272          ;;
 273      esac  
 274    done
 275    export ANS
 276    if [ "$ANS}x" = "Yx" ]; then
 277      return 0
 278    else
 279      return 1
 280    fi
 281  }
 282  
 283  ############
 284  chkID ()
 285  ############
 286  {
 287  # param #1 = username that can run this script
 288    if id | grep $1 >/dev/null; then
 289      #echo Continuing...
 290      :
 291    else
 292      ERRFLAG=1
 293      doEXIT "You must be [$1] to use this script"
 294    fi
 295  }
 296  
 297  ##############
 298  doREPLACEIT () 
 299  ##############
 300  { 
 301    AWK_CMD="awk "
 302    TMP_DIR=/tmp
 303    FILE_SIZE=$(ls -l $3 | $AWK_CMD '{print $5}')
 304    AVAIL_SIZE=$(df -k $TMP_DIR | $AWK_CMD '{getline;print $4*1024}')
 305    #SIZE_DIFF=$(expr $AVAIL_SIZE - $FILE_SIZE )
 306    # This may fail - not tested yet... gwm 6/2003
 307    SIZE_DIFF=echo "$AVIAL_SIZE - $FILE_SIZE" | bc -l4  
 308  
 309    if [ $# != 3 ]; then
 310      echo 'Usage: replaceit "string to find" "string to place" filename'
 311    else
 312      if [ "$SIZE_DIFF" -le "0" ]; then
 313        ERRFLAG=2
 314        doEXIT There is insufficient room to make a temporary file
 315      else
 316        if [ ! -w $3 ]; then
 317          ERRFLAG=3
 318          doEXIT It appears that the file [$3] is not writable...
 319        else
 320          #echo "Replacing $(egrep -c "$1" $3) occurrences of $1 in the file $3 with [$2]..."
 321          sed "s|$1|$2|g" $3 >$TMP_DIR/replaceit$$
 322          #cat $TMP_DIR/replaceit$$ >$3
 323          cat $TMP_DIR/replaceit$$|less
 324          #rm $TMP_DIR/replaceit$$
 325          #echo " --- --- --- "
 326        fi;
 327      fi;
 328    fi
 329  }
 330  
 331  ########################
 332  getMYSQL_ACCESS_INFO ()
 333  ########################
 334  {
 335  doTITLE "MySQL access information"
 336  printf "Please enter the hostname where mysql is running [$my_dbhost]: "
 337  read dbhost
 338  if [ -z $dbhost ]; then
 339    dbhost=$my_dbhost
 340  fi
 341  export dbhost
 342  printf "Please enter the root access user for mysql at $dbhost [$my_dbrootuser]: "
 343  read dbrootuser
 344  if [ -z $dbrootuser ]; then
 345    dbrootuser=$my_dbrootuser
 346  fi
 347  export dbrootuser
 348  printf "Please enter the root access password for $dbrootuser@$dbhost [$my_dbrootpasswd]: "
 349  stty -echo
 350  read dbrootpasswd
 351  stty echo
 352  if [ -z $dbrootpasswd ]; then
 353    echo
 354    dbrootpasswd=$my_dbrootpasswd
 355  fi
 356  export dbrootpasswd
 357  
 358  if [ "x$dbrootpasswd" = "x" ]; then
 359    echo $LINE
 360    echo WARNING: Having a blank password for root access to mysql posses
 361    echo a security risk.
 362    
 363    echo "    --- --- --- --- --- ---"
 364    echo It may require that you to hit ENTER at the prompt requesting
 365    echo a password for each mysql action taken throughout this script.
 366    echo "    --- --- --- --- --- ---"
 367    echo "To establish a password for your mysql root account you can"
 368    echo "run these commands while in mysql:"
 369    echo "  mysql> USE mysql;"
 370    echo "  mysql> UPDATE user SET Password=PASSWORD\(\'newone\'\) WHERE user=\'root\'"
 371    echo "          or"
 372    echo "  mysqladmin -u root password 'newone' "
 373    echo $LINE
 374    if ! askYN "Do you want to continue" n ; then
 375      export ERRFLAG=10
 376      doEXIT "Please set your mysql root account password..."
 377    else
 378      clear
 379      echo "Continuing with myPhile install..."
 380      echo $LINE
 381    fi
 382  fi
 383  
 384  if [ "x$dbrootpasswd" = "x" ]; then
 385    export CMD="mysql -u$dbrootuser "
 386  else
 387    export CMD="mysql -u$dbrootuser -p'$dbrootpasswd'"
 388  fi
 389  }
 390  
 391  ######################
 392  getDB_ACCESS_INFO ()
 393  ######################
 394  {
 395  printf "\n"
 396  doSUBTITLE "Access information for database $dbname"
 397  echo Please understand that this is the user and password that mysql
 398  echo will accept to connect to the database called $dbname. It is not
 399  echo a user signon for the web interface. Initially you will use 'guest'
 400  echo or 'admin' to log into the web interface. 
 401  echo $LINE
 402  printf "Please enter the user to access $dbname [$my_user]: "
 403  read user
 404  if [ -z $user ]; then
 405    user=$my_user
 406  fi
 407  export user
 408  
 409  printf "Please enter the password for $user [$my_passwd]: "
 410  stty -echo
 411  read passwd
 412  stty echo
 413  if [ -z $passwd ]; then
 414    echo 
 415    passwd=$my_passwd
 416  fi
 417  export passwd
 418  }
 419  
 420  #########################
 421  doVERIFY ()
 422  #########################
 423  {
 424  doSUBTITLE " Verify user entries"
 425  printf "MySQL host:                   [$dbhost]\n" 
 426  printf "MySQL root user:              [$dbrootuser]\n"
 427  #printf "MySQL root user passwd:       [$dbrootpasswd]\n"
 428  printf "MySQL root user passwd:       [XXXXXXXXXXXXX]\n"
 429  #printf "MySQL myPhile database name:  [$dbname]\n" 
 430  printf "myPhile user:                 [$user]\n"
 431  printf "myPhile user passwd:          [XXXXXXXXXXXXX]\n"
 432  echo $LINE
 433  if ! askYN "Are these selection correct?" y ; then
 434    clear
 435    exec $0 $*
 436  fi
 437  }
 438  
 439  ###################
 440  doCREATE_DB ()
 441  ###################
 442  {
 443  doSUBTITLE "Create database [$dbname@$dbhost] "
 444  cat << EOF | eval $CMD
 445  CREATE DATABASE IF NOT EXISTS $dbname;
 446  EOF
 447  }
 448  
 449  ########################
 450  doGRANTS ()
 451  ########################
 452  {
 453  doSUBTITLE "grant $user access to $dbname@$dbhost"
 454  cat << EOF | eval $CMD
 455  GRANT select, insert, update, delete
 456  ON $dbname}.*
 457  TO $user}@$dbhost}
 458  IDENTIFIED BY '$passwd}';
 459  EOF
 460  }
 461  
 462  
 463  ########################
 464  doTABLE_VIEWS ()
 465  ########################
 466  {
 467  table_name=myphile_views
 468  doSUBTITLE "Create $table_name table and insert needed data"
 469  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 470  #if ! askYN "Is this acceptible" Y; then
 471  #return 
 472  #fi
 473  cat << EOF | eval $CMD
 474  USE $dbname;
 475  DROP TABLE IF EXISTS $table_name;
 476  CREATE TABLE IF NOT EXISTS $table_name (
 477  id int(11) unsigned NOT NULL auto_increment,
 478  viewname varchar(20) default NULL,
 479  dbname varchar(25) default NULL,
 480  dbhost varchar(25) default NULL,
 481  dbuser varchar(25) default NULL,
 482  dbpassword varchar(15) default NULL,
 483  dbtable varchar(20) default NULL,
 484  bookmarktitle varchar(35) default NULL,
 485  toptitle varchar(35) default NULL,
 486  topimage varchar(55) default NULL,
 487  topimage_scale char(3) default NULL,
 488  row_fields varchar(255) default NULL,
 489  detail_fields blob default NULL,
 490  dflt_user varchar(15) default NULL,
 491  orderby varchar(255) default NULL,
 492  filterby varchar(255) default NULL,
 493  sql_review char(3) default NULL,
 494  page_rows int(3) unsigned default NULL,
 495  header_bgcolor varchar(15) default NULL,
 496  alt_row_bgcolor varchar(15) default NULL,
 497  navbar_bgcolor varchar(15) default NULL,
 498  created datetime default NULL,
 499  modified datetime default NULL,
 500  PRIMARY KEY  (id),
 501  UNIQUE KEY view_name (view_name)
 502  ) TYPE=MyISAM;
 503  #
 504  # Dumping data for table '$table_name'
 505  #
 506  INSERT INTO $table_name VALUES (1,'contacts-basic','$dbname','$dbhost','$user','$passwd','contacts','my.domain.com myphile contacts','My Contacts','yourlogohere.png','last first company position','last first company position street city state zip hmphone wkphone mobile othrphone fax pgr email keywords modified notes','','last','2001-12-14 00:00:00','0000-00-00 00:00:00');
 507  EOF
 508  }
 509  
 510  ########################
 511  doTABLE_USERS ()
 512  ########################
 513  {
 514  table_name=myphile_users
 515  doSUBTITLE "Create $table_name table and insert needed data"
 516  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 517  #if ! askYN "Is this acceptible" Y; then
 518  #  return 
 519  #fi
 520  #echo WARNING: You may get warnings regarding insertion of data records
 521  #echo          that already exists. 
 522  cat << EOF | eval $CMD
 523  USE $dbname;
 524  DROP TABLE IF EXISTS $table_name;
 525  CREATE TABLE IF NOT EXISTS $table_name (
 526    id int(11) unsigned NOT NULL auto_increment,
 527    user varchar(10) default NULL,
 528    first varchar(15) default NULL,
 529    last varchar(15) default NULL,
 530    password varchar(10) default NULL,
 531    email varchar(70) default NULL,
 532    viewname varchar(20) default NULL,
 533    authlist varchar(35) default NULL,
 534    created datetime default NULL,
 535    lastmod datetime default NULL,
 536    PRIMARY KEY  (id)
 537  ) TYPE=MyISAM;
 538  #
 539  # Dumping data for table '$table_name'
 540  #
 541  INSERT INTO $table_name VALUES (1,'admin','Admin','User','changeme',NULL,'contacts-basic','admin row view edit delete add',NULL,NULL);
 542  INSERT INTO $table_name VALUES (2,'guest','Guest','User','',NULL,'contacts-basic','row',NULL,NULL);
 543  INSERT INTO $table_name VALUES (3,'newuser','New','User','blah',NULL,'contacts-basic','view row',NULL,NULL);
 544  EOF
 545  }
 546  
 547  ########################
 548  doTABLE_FCONTROL ()
 549  ########################
 550  {
 551  table_name=myphile_fcontrol
 552  doSUBTITLE "Create $table_name table and insert needed data"
 553  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 554  #if ! askYN "Is this acceptible" Y; then
 555  #  return 
 556  #fi
 557  cat << EOF | eval $CMD $dbname
 558  DROP TABLE IF EXISTS $table_name;
 559  CREATE TABLE IF NOT EXISTS $table_name (
 560  id int(11) unsigned NOT NULL auto_increment,
 561  viewname varchar(20) default NULL,
 562  fieldname varchar(20) default NULL,
 563  description varchar(35) default NULL,
 564  add_edit varchar(10) default NULL,
 565  calc tinytext default NULL,
 566  mask tinytext default NULL,
 567  note varchar(45) default NULL,
 568  created datetime default NULL,
 569  modified datetime default NULL,
 570  PRIMARY KEY  (id)
 571  ) TYPE=MyISAM;
 572  EOF
 573  }
 574  
 575  ########################
 576  doTABLE_CONTACTS ()
 577  ########################
 578  {
 579  table_name=contacts
 580  doSUBTITLE "Create sample $table_name table and insert needed data"
 581  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 582  #if ! askYN "Is this acceptible" Y; then
 583  #return 
 584  #fi
 585  cat << EOF | eval $CMD
 586  USE $dbname;
 587  DROP TABLE IF EXISTS $table_name;
 588  CREATE TABLE IF NOT EXISTS $table_name (
 589  id smallint(5) unsigned NOT NULL auto_increment,
 590  last varchar(15) default NULL,
 591  first varchar(15) default NULL,
 592  company varchar(35) default NULL,
 593  position varchar(25) default '',
 594  street varchar(35) default NULL,
 595  city varchar(35) default NULL,
 596  state char(2) default NULL,
 597  zip varchar(10) default NULL,
 598  hmphone varchar(15) default NULL,
 599  wkphone varchar(15) default NULL,
 600  mobile varchar(15) default NULL,
 601  othrphone varchar(15) default NULL,
 602  fax varchar(15) default NULL,
 603  pgr varchar(35) default NULL,
 604  email varchar(35) default NULL,
 605  keywords varchar(35) default NULL,
 606  modified date default NULL,
 607  notes tinytext,
 608  PRIMARY KEY  (id)
 609  ) TYPE=MyISAM;
 610  #
 611  # Dumping data for table '$table_name'
 612  #
 613  INSERT INTO $table_name VALUES (30,'McNamara','Geoff','','','8909 Walker Street','','','','','','','','','','','','0000-00-00','');
 614  EOF
 615  }
 616  
 617  ########################
 618  doTABLE_INVENTORY ()
 619  ########################
 620  {
 621  table_name=inventory
 622  doSUBTITLE "Create sample $table_name table and insert needed data"
 623  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 624  #if ! askYN "Is this acceptible" Y; then
 625  #return 
 626  #fi
 627  cat << EOF | eval $CMD
 628  DROP TABLE IF EXISTS $table_name;
 629  CREATE TABLE IF NOT EXISTS $table_name (
 630  id int(11) unsigned NOT NULL auto_increment,
 631  device varchar(20) default NULL,
 632  name varchar(25) default NULL,
 633  device_type varchar(20) default NULL,
 634  make varchar(25) default NULL,
 635  model varchar(35) default NULL,
 636  ser_num varchar(30) default NULL,
 637  system_con varchar(25) default NULL,
 638  location varchar(25) default NULL,
 639  network_info varchar(35) default NULL,
 640  primary_addr varchar(20) default NULL,
 641  primary_use varchar(25) default NULL,
 642  access_info varchar(35) default NULL,
 643  os_info varchar(35) default NULL,
 644  service_info varchar(124) default NULL,
 645  purchase_price double(16,2) default '0.00',
 646  in_service datetime default NULL,
 647  status varchar(15) default NULL,
 648  history tinytext default NULL,
 649  specs tinytext default NULL,
 650  salvaged datetime default NULL,
 651  status varchar(25) default NULL,
 652  notes tinytext default NULL,
 653  created datetime default NULL,
 654  lastmod datetime default NULL,
 655  PRIMARY KEY  (id)
 656  ) TYPE=MyISAM;
 657  EOF
 658  }
 659  
 660  ########################
 661  doTABLE_TODO ()
 662  ########################
 663  {
 664  table_name=todo
 665  doSUBTITLE "Create $table_name table and insert needed data"
 666  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 667  #if ! askYN "Is this acceptible" Y; then
 668  #  return 
 669  #fi
 670  #echo $CMD $dbname
 671  #read ANS
 672  cat << EOF | eval $CMD $dbname
 673  DROP TABLE IF EXISTS $table_name;
 674  CREATE TABLE IF NOT EXISTS $table_name (
 675  id int(11) unsigned NOT NULL auto_increment,
 676  description varchar(60) default NULL,
 677  category varchar(35) default NULL,
 678  subcategory varchar(35) default NULL,
 679  project varchar(35) default NULL,
 680  status varchar(15) default NULL,
 681  assigned varchar(25) default NULL,
 682  start datetime default NULL,
 683  end datetime default NULL,
 684  total_time varchar(10) default NULL,
 685  notes tinytext default NULL,
 686  created datetime default NULL,
 687  modified datetime default NULL,
 688  PRIMARY KEY  (id)
 689  ) TYPE=MyISAM;
 690  EOF
 691  # need to add a record for
 692  # fcontrol add select now() for created
 693  # fcontrol edit select now() for modified
 694  # a view record for todo
 695  # a user called todo
 696  echo Done!
 697  }
 698  
 699  ########################
 700  doTABLE_GIFTS ()
 701  ########################
 702  {
 703  table_name=gifts
 704  doSUBTITLE "Create $table_name table and insert needed data"
 705  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 706  #if ! askYN "Is this acceptible" Y; then
 707  #  return 
 708  #fi
 709  cat << EOF | eval $CMD $dbname
 710  DROP TABLE IF EXISTS $table_name;
 711  CREATE TABLE IF NOT EXISTS $table_name (
 712  id int(11) unsigned NOT NULL auto_increment,
 713  who varchar(15) default NULL,
 714  from_or_for varchar(4) default NULL,
 715  description varchar(35) default NULL,
 716  status varchar(15) default NULL,
 717  aquired datetime default NULL,
 718  source varchar(35) default NULL,
 719  assigned varchar(25) default NULL,
 720  cost varchar(15) default NULL,
 721  notes tinytext default NULL,
 722  created datetime default NULL,
 723  lastmod datetime default NULL,
 724  PRIMARY KEY  (id)
 725  ) TYPE=MyISAM;
 726  EOF
 727  }
 728  
 729  ########################
 730  doTABLE_INVEST ()
 731  ########################
 732  {
 733  table_name=invest
 734  doSUBTITLE "Create $table_name table and insert needed data"
 735  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 736  #if ! askYN "Is this acceptible" Y; then
 737  #  return 
 738  #fi
 739  cat << EOF | eval $CMD $dbname
 740  DROP TABLE IF EXISTS $table_name;
 741  CREATE TABLE IF NOT EXISTS $table_name (
 742  id int(11) unsigned NOT NULL auto_increment,
 743  name varchar(20) default NULL,
 744  symbol varchar(10) default NULL,
 745  exchange varchar(25),
 746  description varchar(35) default NULL,
 747  bought_or_sold varchar(6),
 748  quantity varchar(15),
 749  price double(16,2),
 750  commision varchar(15),
 751  account varchar(35),
 752  trans_date date,
 753  notes tinytext,
 754  created datetime default NULL,
 755  modified datetime default NULL,
 756  PRIMARY KEY  (id)
 757  ) TYPE=MyISAM;
 758  EOF
 759  # need to add a record for
 760  # fcontrol add select now() for created
 761  # fcontrol edit select now() for modified
 762  # a view record for todo
 763  # a user called todo
 764  echo Done!
 765  }
 766  
 767  ########################
 768  doTABLE_QUOTES ()
 769  ########################
 770  {
 771  table_name=quotes
 772  doSUBTITLE "Create $table_name table and insert needed data"
 773  echo "WARNING:  This routine will DROP [remove] the table $dbname}.$table_name"
 774  #if ! askYN "Is this acceptible" Y; then
 775  #  return 
 776  #fi
 777  cat << EOF | eval $CMD $dbname
 778  DROP TABLE IF EXISTS $table_name;
 779  CREATE TABLE IF NOT EXISTS $table_name (
 780  id int(11) unsigned NOT NULL auto_increment,
 781  quote mediumtext default NULL,
 782  who varchar(30) default NULL,
 783  circa date  default NULL,
 784  category varchar(25) default NULL,
 785  keywords varchar(35) default NULL,
 786  orig_source tinytext default NULL,
 787  my_source varchar(40) default NULL,
 788  notes tinytext default NULL,
 789  created datetime default NULL,
 790  modified datetime default NULL,
 791  PRIMARY KEY  (id)
 792  ) TYPE=MyISAM;
 793  EOF
 794  # need to add a record for
 795  # fcontrol add select now() for created
 796  # fcontrol edit select now() for modified
 797  # a view record for todo
 798  # a user called todo
 799  echo Done!
 800  }
 801  
 802  ########################
 803  doREPLACEITS ()
 804  ########################
 805  {
 806  doSUBTITLE "Changing references to Admin database in index.php"
 807  doREPLACEIT AdminDB=\.\*\; AdminDB=\"$dbname\"\; $BASE_DIR/index.php
 808  doREPLACEIT AdminDBHOST=\.\*\; AdminDBHOST=\"$dbhost\"\; $BASE_DIR/index.php
 809  doREPLACEIT AdminDBUSER=\.\*\; AdminDBUSER=\"$user\"\; $BASE_DIR/index.php
 810  doREPLACEIT AdminDBPASSWORD=\.\*\; AdminDBPASSWORD=\"$passwd\"\; $BASE_DIR/index.php
 811  
 812  doSUBTITLE "Changing references in [myphile.sql] for [$my_user]"
 813  doREPLACEIT $ACCESS_NAME $my_user  $BASE_DIR/myphile.sql
 814  
 815  #doREPLACEIT $ACCESS_NAME_PASSWD $my_passwd  $BASE_DIR/myphile.sql
 816  }
 817  
 818  ####################
 819  loadMYPHILE_SQL ()
 820  ####################
 821  {
 822    doSUBTITLE "Loading new tables and inserting new records"
 823  # Assumes that the database has already been completed
 824  #  eval $CMD $dbname < $BASE_DIR/myphile.sql  
 825  createVIEWS
 826  createUSERS
 827  createFCONTROL
 828  createCONTACTS
 829  }
 830  
 831  ################
 832  changeUSER_PASSWORD ()
 833  ##############
 834  {
 835  # This has to be done after loadMYPHILE_SQL ()
 836  doSUBTITLE "Change the password for $user to allow access to $dbname"
 837  cat << EOF | eval $CMD $dbname
 838  UPDATE views SET dbuser='$user',dbpassword='$passwd' WHERE dbuser='$my_user' AND dbname='$dbname';
 839  UPDATE users SET password='' WHERE authlist NOT LIKE '%admin%';
 840  #UPDATE users SET password='$passwd' WHERE user='admin';
 841  EOF
 842  }
 843  
 844  ##############
 845  doCLEANUP ()
 846  ##############
 847  {
 848  if askYN "Do you want the install files removed" y; then
 849    rm install.sh
 850    rm myphile.sql
 851  else
 852    echo Leaving the install files in place.
 853  fi
 854  }
 855  
 856  ####################
 857  doINSTALL ()
 858  ####################
 859  {
 860  # I used "mysqldump -opt myphile>myphile.sql" to create myphile.dump file
 861  # This script will use the below variables to do replacements
 862  # on the pre-existing strings for all table insertions
 863  # what pre-exists in the data files - this is for me (gwm) to manipulate
 864  export ACCESS_NAME=$my_user
 865  export ACCESS_NAME_PASSWD=$my_passwd
 866  
 867  clear
 868  getMYSQL_ACCESS_INFO
 869  
 870  if `echo "SHOW DATABASES;" | eval $CMD | grep myphile >/dev/null`; then
 871    echo The database myphile already exists.
 872    echo You may want to consider using the -u parameter for upgrading.
 873    echo Continuing will delete all records in your myphile database and
 874    echo then rebuild it without your existing data..
 875    if ! askYN "Do you want to continue" N;then
 876      ERRFLAG=1
 877      doEXIT
 878    fi
 879  fi
 880  getDB_ACCESS_INFO 
 881  doVERIFY
 882  doGRANTS 
 883  doCREATE_DB
 884  doREPLACEITS
 885  # see note above about how myphile.sql was created...
 886  # it drops everything before loading new stuff...
 887  loadMYPHILE_SQL
 888  changeUSER_PASSWORD
 889  doCLEANUP
 890  echo $LINE
 891  doLOGIN_MSG
 892  echo $LINE
 893  echo -geoff mcnamara
 894  export ERRFLAG=0
 895  #doEXIT
 896  }
 897  
 898  ##############
 899  doLOGIN_MSG ()
 900  ##############
 901  {
 902  echo Use your browser to access http://your_web_site/myphile.
 903  echo You will initially be logged in with the 'guest' account.
 904  echo Click on 'Admin' and login with 'admin' and password 'changeme'.
 905  echo You should go to 'Admin->User Management' and edit the admin 
 906  echo user - change the password to something else and remember it!
 907  echo    I hope your find this application useful.
 908  }
 909  
 910  ####################
 911  createFCONTROL ()
 912  ####################
 913  {
 914    doSUBTITLE "Creating fcontrol table"
 915    if [ -f myphile.sql ]; then 
 916     sed -n '/CREATE TABLE fcontrol/,/ISAM/p' myphile.sql | eval $CMD myphile
 917    else
 918      echo Could not find myphile.sql
 919    fi
 920  }
 921  
 922  #####################
 923  descTABLE ()
 924  #####################
 925  {
 926    echo "desc $1;"\
 927    |  eval $CMD $dbname \
 928    | awk '{printf "%-20s%s\n", $1, $2}' \
 929    | sed -e '1d'
 930    ## |awk '{getline;printf "%-20s%s\n", $1, $2}'
 931  }
 932  
 933  #####################
 934  showTABLES ()
 935  #####################
 936  {
 937    echo "SHOW TABLES;" \
 938    |  eval $CMD $dbname \
 939    | awk '{printf "%-20s%s\n", $1, $2}' \
 940    | sed -e '1d'
 941    ## |awk '{getline;printf "%-20s%s\n", $1, $2}'
 942  }
 943  
 944  #####################
 945  getTABLE_FROM_DUMP()
 946  #####################
 947  {
 948    #doSUBTITLE "Doing getTABLE_FROM_DUMP"
 949    MYTABLE=$1
 950    DUMP=$2
 951    END_STRING="TYPE=MyISAM"
 952    if [ "x$2" = "x" ]; then
 953      echo We didn\'t get 2 params... 
 954      doEXIT "Function getTABLE_FROM_DUMP failed..."
 955    fi
 956    if [ ! -f $DUMP ]; then
 957      echo Ooops - can not find file [$DUMP]..
 958      doEXIT "Failed to find file..."
 959    fi
 960    #echo We are here ....
 961    #echo MYTABLE=$MYTABLE
 962    cat $DUMP \
 963      | sed -e "/CREATE TABLE $MYTABLE}/,/$END_STRING}/!d" -e "/$END_STRING}/d" -e "/UNIQUE/d"  -e "/PRIMARY/d" -e "s/,//" \
 964      | awk '{getline;printf "%-20s%s\n", $1, $2}'
 965    #echo Done...
 966  }
 967  
 968  ###################
 969  doUPGRADE ()
 970  ###################
 971  {
 972    getMYSQL_ACCESS_INFO
 973    TABLES="views fcontrol users contacts todo"
 974    for i in $TABLES; do
 975      {
 976        printf "\n"
 977        doSUBTITLE "Checking table [$i] structure for any needed updates..."
 978        getTABLE_FROM_DUMP $i $SQL_DUMP_FILE |
 979        while read RLINE; do
 980        {
 981          export NEW_FIELDNAME=`echo $RLINE | awk '{print $1}'`    
 982          export NEW_TYPE=`echo $RLINE | awk '{print $2}'`    
 983          LEFT_STRING="Checking field [$NEW_FIELDNAME] ..."
 984          COLS=$(tput cols)
 985          POS=$( expr $COLS} - $( echo "$LEFT_STRING}" | awk '{print length}') )
 986          printf "%s" "$LEFT_STRING}"
 987          descTABLE $i |
 988          while read OTHR_LINE; do
 989          {
 990            export CURR_FIELDNAME=`echo $OTHR_LINE | awk '{print $1}'`
 991            export CURR_TYPE=`echo $OTHR_LINE | awk '{print $2}'`
 992            #echo Checking $NEW_FIELDNAME with $CURR_FIELDNAME
 993            if [ "x$NEW_FIELDNAME" = "x$CURR_FIELDNAME" ]; then
 994              echo $NEW_FIELDNAME was found> FOUND.tmp
 995              #
 996              #
 997              
 998              if [ "$NEW_TYPE" = "$CURR_TYPE" ]; then
 999                #echo NEW type [$NEW_TYPE] matches the CURRENT type [$CURR_TYPE] for field $NEW_FIELDNAME
1000                RIGHT_STRING="$NORMAL}[$GREEN}OK$NORMAL}]  "
1001                printf "%$POS}s\n" "$RIGHT_STRING}"
1002                :
1003              else
1004                RIGHT_STRING="$NORMAL}[$RED}ERROR$NORMAL}]  "
1005                printf "%$POS}s\n" "$RIGHT_STRING}"
1006                #echo $LINE
1007                #echo An adjustment to the current table [$i] is needed.
1008                #echo For field [$NEW_FIELDNAME]
1009                #echo CURRENT type [$CURR_TYPE] 
1010                ##echo FIALS to match
1011                #echo NEW type [$NEW_TYPE]
1012                #echo $LINE
1013                alterFIELD $NEW_FIELDNAME $NEW_TYPE $i
1014              fi
1015            else
1016              #echo $NEW_FIELDNAME $CURR_FIELDNAME
1017              :
1018            fi
1019          }
1020          done
1021    
1022          if [ ! -f FOUND.tmp  ] ; then
1023            RIGHT_STRING="$NORMAL}[$RED}ERROR$NORMAL}]  "
1024            printf "%$POS}s\n" "$RIGHT_STRING}"
1025            #echo ${RED}$LINE${NORMAL}
1026            #echo Fieldname [${NEW_FIELDNAME}] was not found in current table...
1027            #echo An adjustment to the current table [$i] is needed.
1028            #echo NEW field [$NEW_FIELDNAME]
1029            #echo NEW type  [$NEW_TYPE]
1030            #echo ${RED}$LINE${NORMAL}
1031            addFIELD $NEW_FIELDNAME $NEW_TYPE $i
1032          else
1033            rm -f FOUND.tmp
1034          fi
1035        }
1036        done
1037      }
1038      done
1039  
1040      export ERRFLAG=$?
1041      echo $LINE
1042      doCLEANUP
1043      echo $LINE
1044      doLOGIN_MSG
1045      echo With the upgrade routine some sample records may not have been
1046      echo added.  This was intentionally done to avoid overwriting 
1047      echo existing records in your current tables.
1048      doEXIT
1049  } # End doUPGRADE ()    
1050  
1051  #########################
1052  alterFIELD ()
1053  #######################
1054  {
1055  # $1 = field to change
1056  # $2 = new field type
1057  # $3 = table name
1058    FIELDNAME=$1
1059    FIELDTYPE=$2
1060    TABLENAME=$3
1061    printf "\n"
1062    doSUBTITLE  "Altering field [$FIELDNAME] to new type [$FIELDTYPE] for table $TABLENAME]"
1063    printf "\n"
1064  #  echo "ALTER TABLE $TABLENAME CHANGE $FIELDNAME $FIELDNAME $FIELDTYPE;" 
1065    echo "ALTER TABLE $TABLENAME CHANGE $FIELDNAME $FIELDNAME $FIELDTYPE;" \
1066     |  eval $CMD $dbname 
1067    return $?
1068  }
1069  
1070  ####################
1071  addFIELD ()
1072  ####################
1073  {
1074  # $1 = field to add
1075  # $2 = field type
1076  # $3 = table name
1077  # $4 = field options (optional)
1078    FIELDNAME=$1
1079    FIELDTYPE=$2
1080    TABLENAME=$3
1081    FIELDOPTIONS=$4
1082    printf "\n"
1083    doSUBTITLE  "Adding field [$FIELDNAME] with type [$FIELDTYPE] to table $TABLENAME]"
1084    printf "\n"
1085    #echo "ALTER TABLE $TABLENAME ADD $FIELDNAME $FIELDTYPE $FIELDOPTIONS;" 
1086    echo "ALTER TABLE $TABLENAME ADD $FIELDNAME $FIELDTYPE $FIELDOPTIONS;" \
1087      |  eval $CMD $dbname 
1088    return $?
1089  }
1090  
1091  ####################
1092  # Main Code
1093  #####################
1094  # these are to gain root access for setup
1095  export my_dbhost=localhost
1096  export my_dbrootuser=root
1097  export my_dbrootpasswd=""
1098  # these are for the myphile database
1099  export dbname=myphile
1100  export my_user=wwwuser
1101  export my_passwd="changeme"
1102  
1103  chkID root
1104  if [ $# != 0 ]; then
1105    handleOPTS $*
1106  else
1107    doINSTALL
1108  fi
1109  
1110  #ERRFLAG=0
1111  doEXIT Have a nice day!
1112  ### End of Script ###


[ Powered by PHPXref - Served by Debian GNU/Linux ]