Bug 7888 - Permission for "change password" button in circ toolbar should be "borrowe...
[koha.git] / debian / scripts / koha-create
index 018cae5..bb2b642 100755 (executable)
 
 set -e
 
-usage="Usage: $0 [--create-db|--request-db|--populate-db] \
+usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
     [--marcflavor marc21|normarc|unimarc] \
     [--zebralang en|nb|fr] \
     [--defaultsql /path/to/some.sql] \
-    [--configfile /path/to/config] instancename"
+    [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
+    [--database database] [--adminuser n] instancename"
 
 die() {
     echo "$@" 1>&2
     exit 1
 }
 
+# UPPER CASE VARIABLES - from configfile or default value
+# lower case variables - generated within this script
 generate_config_file() {
     touch "$2"
     chown "root:$username" "$2"
     chmod 0640 "$2"
     sed -e "s/__KOHASITE__/$name/g" \
-        -e "s/__OPACPORT__/80/g" \
+        -e "s/__OPACPORT__/$OPACPORT/g" \
         -e "s/__INTRAPORT__/$INTRAPORT/g" \
-        -e "s/__OPACSERVER__/$domain/g" \
+        -e "s/__OPACSERVER__/$opacdomain/g" \
         -e "s/__INTRASERVER__/$intradomain/g" \
         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
@@ -73,20 +76,25 @@ getinstancemysqldatabase() {
 
 # Set defaults and read config file, if it exists.
 DOMAIN=""
+OPACPORT="80"
+OPACPREFIX=""
+OPACSUFFIX=""
 INTRAPORT="8080"
 INTRAPREFIX=""
 INTRASUFFIX=""
 DEFAULTSQL=""
 ZEBRA_MARC_FORMAT="marc21"
 ZEBRA_LANGUAGE="en"
+ADMINUSER="1"
+PASSWDFILE="/etc/koha/passwd"
 if [ -e /etc/koha/koha-sites.conf ]
 then
     . /etc/koha/koha-sites.conf
 fi
 
-[ $# -ge 2 ] && [ $# -le 10 ] || die $usage
+[ $# -ge 2 ] && [ $# -le 16 ] || die $usage
 
-TEMP=`getopt -o crpm:l:d:f: -l create-db,request-db,populate-db,marcflavor:,zebralang:,defaultsql:,configfile: \
+TEMP=`getopt -o crpm:l:d:f:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,adminuser: \
      -n "$0" -- "$@"`
 
 # Note the quotes around `$TEMP': they are essential!
@@ -96,16 +104,21 @@ eval set -- "$TEMP"
 CLO_ZEBRA_MARC_FORMAT=""
 CLO_ZEBRA_LANGUAGE=""
 CLO_DEFAULTSQL=""
+CLO_ADMINUSER=""
 
 while true ; do
        case "$1" in
                -c|--create-db) op=create ; shift ;;
                -r|--request-db) op=request ; shift ;;
                -p|--populate-db) op=populate ; shift ;;
+        -u|--use-db) op=use ; shift ;;
                -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
                -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
                -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
                -f|--configfile) configfile="$2" ; shift 2 ;;
+        -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
+        -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
+               -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
                --) shift ; break ;;
                *) die "Internal error processing command line arguments" ;;
        esac
@@ -135,31 +148,58 @@ if [ "$CLO_DEFAULTSQL" != "" ]
 then
     DEFAULTSQL="$CLO_DEFAULTSQL"
 fi
+if [ "$CLO_ADMINUSER" != "" ]
+then
+    ADMINUSER="$CLO_ADMINUSER"
+fi
+if [ "$CLO_PASSWDFILE" != "" ]
+then
+    PASSWDFILE="$CLO_PASSWDFILE"
+fi
 
 name="$1"
 
-domain="$name$DOMAIN"
-if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
+opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
+intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
+
+
+if [ `cat $PASSWDFILE | grep "^$name:"` ]
 then
-    intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
-else
-    intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
+    passwdline=`cat $PASSWDFILE | grep "^$name:"`
+    mysqluser=`echo $passwdline | cut -d ":" -f 2`
+    mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
+    mysqldb=`echo $passwdline | cut -d ":" -f 4`
 fi
 
+# The order of precedence for MySQL database name is:
+# default < passwd file < command line
+if [ "$mysqldb" = "" ]
+then
+    mysqldb="koha_$name"
+fi
+if [ "$CLO_DATABASE" != "" ]
+then
+    mysqldb="$CLO_DATABASE"
+fi
 
-mysqldb="koha_$name"
+if [ "$mysqluser" = "" ]
+then
+    mysqluser="koha_$name"
+fi
 mysqlhost="$(getmysqlhost)"
-mysqluser="koha_$name"
 
-if [ "$op" = create ] || [ "$op" = request ]
+if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
 then
-    mysqlpwd="$(pwgen -1)"
+    if [ "$mysqlpwd" = "" ]
+    then
+        mysqlpwd="$(pwgen -1)"
+    fi
 else
     mysqlpwd="$(getinstancemysqlpassword $name)"
 fi
 
 
-if [ "$op" = create ] || [ "$op" = request ]
+if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
 then
     # Create new user and group.
     username="$name-koha"
@@ -192,6 +232,15 @@ FLUSH PRIVILEGES;
 eof
     fi #`
 
+    if [ "$op" = use ]
+    then
+        mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
+CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
+GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
+FLUSH PRIVILEGES;
+eof
+    fi #`
+
     # Generate and install Apache site-available file and log dir.
     generate_config_file apache-site.conf.in \
         "/etc/apache2/sites-available/$name"
@@ -250,7 +299,7 @@ then
         # Populate the database with default content.
         zcat "$DEFAULTSQL" |
         sed "s/__KOHASITE__/$name/g" |
-        mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
+        mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
 
 
         # Change the default user's password.
@@ -264,7 +313,7 @@ then
 USE \`$mysqldb\`;
 UPDATE borrowers 
 SET password = '$staffdigest' 
-WHERE borrowernumber = 3;
+WHERE borrowernumber = $ADMINUSER;
 eof
         #`
         echo "staff user password is '$staffpass' but keep that secret"
@@ -278,7 +327,7 @@ eof
 fi
 
 
-if [ "$op" = create ] || [ "$op" = populate ]
+if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
 then
     # Reconfigure Apache.
     a2ensite "$name"