head     1.12;
branch   ;
access   ;
symbols  Version_2_1:1.11;
locks    ; strict;
comment  @# @;


1.12
date     91.03.20.19.10.05;  author cimarron;  state Exp;
branches ;
next     1.11;

1.11
date     91.02.19.22.23.00;  author mao;  state Exp;
branches ;
next     1.10;

1.10
date     90.10.23.14.47.58;  author kemnitz;  state Exp;
branches ;
next     1.9;

1.9
date     90.10.21.23.35.15;  author hong;  state Exp;
branches ;
next     1.8;

1.8
date     90.10.05.15.19.22;  author cimarron;  state Exp;
branches ;
next     1.7;

1.7
date     90.09.25.17.23.26;  author kemnitz;  state Exp;
branches ;
next     1.6;

1.6
date     90.09.25.15.51.21;  author kemnitz;  state Exp;
branches ;
next     1.5;

1.5
date     90.09.18.22.03.11;  author hong;  state Exp;
branches ;
next     1.4;

1.4
date     90.08.27.14.16.08;  author hong;  state Exp;
branches ;
next     1.3;

1.3
date     90.08.23.16.20.38;  author kemnitz;  state Exp;
branches ;
next     1.2;

1.2
date     90.08.22.17.53.47;  author kemnitz;  state Exp;
branches ;
next     1.1;

1.1
date     90.08.22.17.31.02;  author kemnitz;  state Exp;
branches ;
next     ;


desc
@Createdb shell script.
@


1.12
log
@added a few debugging lines
@
text
@#!/bin/sh
# ----------------------------------------------------------------
#   FILE
#	createdb	create a postgres database
#
#   DESCRIPTION
#	this program feeds the proper input to the ``backend'' program
#	to create a postgres database and register it in the
#	shared ``pg_database'' database.
#
#   NOTES
#	currently the backend does not do any locking on pg_database
#	so it is possible that pg_database could be corrupted if we
#	create a new database while a postgres/postmaster session is
#	in progress.  Someday locking code will have to be added 
#	to this script.
#
#   IDENTIFICATION
# 	$Header: RCS/createdb,v 1.11 91/02/19 22:23:00 mao Exp Locker: cimarron $
# ----------------------------------------------------------------

CMDNAME=`basename $0`

# ----------------
# 	check arguments:
# 	    -d indicates debug mode.
#	    -n means don't clean up on error (so your cores don't go away)
# ----------------
debug=0
noclean=0

for ARG
do
	case "$ARG" in
	-d)	debug=1; echo "$CMDNAME: debug mode on";;
	-n)	noclean=1; echo "$CMDNAME: noclean mode on";;
	*)	FILES="$FILES $ARG";;
	esac
done

# ----------------
# 	if the debug flag is set, then 
# ----------------
if (test "$debug" -eq 1)
then
    BACKENDARGS="-COd -ami"
else
    BACKENDARGS="-COQ -ami"
fi

# ----------------
# 	if no files are specified, assume the username.
# ----------------
if (test -z "$FILES")
then
    FILES=$USER
    if (test "$debug" -eq 1)
    then
        echo "$CMDNAME: no database name specified, assuming: $FILES"
    fi
fi

# ----------------
# 	check POSTGRESHOME
# ----------------
if (test -z "$POSTGRESHOME")
then
    echo "$CMDNAME: POSTGRESHOME not set."
    exit 1
else
    PG=`echo $POSTGRESHOME | sed -e 's/\([^:]*\):.*/\1/'`
    PGS=`echo $POSTGRESHOME | sed -e 's/:/ /g'`
fi

# ----------------
# 	check POSTGRESTREE
# ----------------
if (test -z "$POSTGRESTREE")
then
    TREE=$PG
else
    TREE=$POSTGRESTREE
fi

# ----------------
# 	check POSTGRESBIN
# ----------------
if (test -z "$POSTGRESBIN")
then
    PGBIN=$PG/bin
else
    PGBIN=$POSTGRESBIN
fi

# ----------------
# 	check POSTGRESTEMP
# ----------------
if (test -z "$POSTGRESTEMP")
then
    PGTEMP=$PG/files
else
    PGTEMP=$POSTGRESTEMP
fi

# ----------------
# 	find the paths to the backend, pg_version, and pg_id programs
# ----------------
if (test "$debug" -eq 1)
then
    echo "$CMDNAME: looking for backend in:"
    echo "$CMDNAME:   $PGBIN"
    echo "$CMDNAME:   $TREE"
fi

if (test -f $PGBIN/backend)
then
    BACKEND=$PGBIN/backend
    PG_VERSION=$PGBIN/pg_version
	PG_ID=$PGBIN/pg_id
    if (test "$debug" -eq 1)
    then
	echo "$CMDNAME: found $BACKEND"
    fi
elif (test -f $TREE/*/support/backend)
then
    BACKEND=$TREE/*/support/backend
    PG_VERSION=$TREE/*/support/pg_version
	PG_ID=$TREE/*/support/pg_id
    if (test "$debug" -eq 1)
    then
	echo "$CMDNAME: found $BACKEND"
    fi
else
    echo "$CMDNAME: could not find backend program"
    echo "$CMDNAME: set POSTGRESHOME to the proper directory and rerun."
    exit 1
fi

# ----------------
# 	find the template files
# ----------------
if (test "$debug" -eq 1)
then
    echo "$CMDNAME: looking for template files..."
fi
if (test -f $PGTEMP/local1_template1.bki)
then
    TEMPLATE=$PGTEMP/local1_template1.bki
    GLOBAL=$PGTEMP/global1.bki
else
    echo "$CMDNAME: warning: Make install has not been run"
    TEMPLATE=`echo $TREE/*/support/local.bki`
    GLOBAL=`echo $TREE/*/support/dbdb.bki`

    if (test ! -f $TEMPLATE)
    then
        echo "$CMDNAME: could not find template files"
        echo "$CMDNAME: set POSTGRESTEMP to the proper directory and rerun."
        exit 1
    fi
fi
if (test "$debug" -eq 1)
then
    echo "$CMDNAME: using $TEMPLATE"
    echo "$CMDNAME: using $GLOBAL"
fi

#
# Figure out who I am...
#

UID=`$PG_ID`

# ----------------
# 	create the template database if necessary
# ----------------

if (test ! -d $PG/data)
then
    for pg in $PGS
    do
       mkdir $pg/data $pg/data/base $pg/data/base/template1
    done
    if (test "$debug" -eq 1)
    then
	echo "$CMDNAME: creating SHARED relations in $PG/data"
	echo "$CMDNAME: creating template database in $PG/data/base/template1"
    fi

    $BACKEND $BACKENDARGS template1 < $TEMPLATE 

    if (test $? -ne 0)
    then
        echo "$CMDNAME: could not create template database"
        if (test $noclean -eq 0)
        then
	    echo "$CMDNAME: cleaning up."
	    for pg in $PGS
	    do
                rm -rf $pg/data
	    done
        else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
        fi
	exit 1;
    fi

    $PG_VERSION $PG/data/base/template1

    #
    # Add the template database to pg_database
    #

    echo "open pg_database" > /tmp/create.$$
    echo "insert (template1 $UID template1)" >> /tmp/create.$$
    echo "show" >> /tmp/create.$$
    echo "close pg_database" >> /tmp/create.$$

    $BACKEND $BACKENDARGS template1 < $GLOBAL 

    if (test $? -ne 0)
    then
        echo "$CMDNAME: could create shared relations"
        if (test $noclean -eq 0)
        then
	    echo "$CMDNAME: cleaning up."
	    for pg in $PGS
	    do
                rm -rf $pg/data
	    done
        else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
        fi
	exit 1;
    fi

    $PG_VERSION $PG/data

    $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 

    if (test $? -ne 0)
    then
        echo "$CMDNAME: could not log template database"
        if (test $noclean -eq 0)
        then
	    echo "$CMDNAME: cleaning up."
	    for pg in $PGS
	    do
                rm -rf $pg/data
	    done
        else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
        fi
	exit 1;
    fi

    rm -f /tmp/create.$$
fi

# ----------------
# 	create the desired databases from the template database
# ----------------
for DBNAME in $FILES
do
    if (test -d $PG/data/base/$DBNAME)
    then
        echo "$CMDNAME: database $DBNAME already exists."
    else
        for pg in $PGS
        do
            mkdir $pg/data/base/$DBNAME
	    if (test "$debug" -eq 1)
	    then
		echo "$CMDNAME: making directory $pg/data/base/$DBNAME"
	    fi
            cp $pg/data/base/template1/* $pg/data/base/$DBNAME
        done
    
        echo "open pg_database" > /tmp/create.$$
        echo "insert ($DBNAME $UID $DBNAME)" >> /tmp/create.$$
        echo "show" >> /tmp/create.$$
        echo "close pg_database" >> /tmp/create.$$
	if (test "$debug" -eq 1)
	then
	    echo "$CMDNAME: creating database $DBNAME"
	fi
        $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 
    
        if (test $? -ne 0)
    	then
            echo "$CMDNAME: could not log new database"
            if (test $noclean -eq 0)
            then
	        echo "$CMDNAME: cleaning up."
	        for pg in $PGS
	        do
                    rm -rf $pg/data/base/$DBNAME
	        done
            else
	        echo "$CMDNAME: cleanup not done (noclean mode set)."
            fi
	    exit 1;
        fi

        rm -f /tmp/create.$$
    fi
done
@


1.11
log
@don't print debugging messages unless -d is specified
@
text
@d19 1
a19 1
# 	$Header: RCS/createdb,v 1.10 90/10/23 14:47:58 kemnitz Exp Locker: mao $
d110 3
a112 1
    echo "$CMDNAME: looking for backend..."
d114 1
@


1.10
log
@Now uses new pg_id binary
@
text
@d19 1
a19 1
# 	$Header: RCS/createdb,v 1.9 90/10/21 23:35:15 hong Exp Locker: kemnitz $
d57 4
a60 1
    echo "$CMDNAME: no database name specified, assuming: $FILES"
d108 4
a111 1
echo "$CMDNAME: looking for backend..."
d117 4
a120 1
    echo "$CMDNAME: found $BACKEND"
d126 4
a129 1
    echo "$CMDNAME: found $BACKEND"
d139 4
a142 1
echo "$CMDNAME: looking for template files..."
d159 5
a163 2
echo "$CMDNAME: using $TEMPLATE"
echo "$CMDNAME: using $GLOBAL"
d181 5
a185 2
    echo "$CMDNAME: creating SHARED relations in $PG/data"
    echo "$CMDNAME: creating template database in $PG/data/base/template1"
d269 4
a272 1
            echo "$CMDNAME: making directory $pg/data/base/$DBNAME"
d280 4
a283 1
        echo "$CMDNAME: creating database $DBNAME"
@


1.9
log
@bug fix in striping databases
@
text
@d19 1
a19 1
# 	$Header: RCS/createdb,v 1.8 90/10/05 15:19:22 cimarron Exp Locker: hong $
a21 1
UID=`id | sed 's/uid=//' | sed 's/(.*//'`
d103 1
a103 1
# 	find the paths to the backend and pg_version programs
d110 1
d116 1
d120 1
a120 1
    echo "$CMDNAME: set POSTGRESBIN to the proper directory and rerun."
d147 3
d151 2
d156 1
@


1.8
log
@I got upset when I couldn't debug the backend program because createdb
would always delete the data/ directory in case of errors, so I added 
lots of comments and fixed things so that:

	1) the -d debug flag passes the correct args to the backend
	   (i.e. it works now)
	2) added a -n flag. you can now say

		createdb -n bletch

	   and if there is an error, createdb will not preform "cleanup".
	   Thas is, your data/base/.. directories will not be removed.

	3) added some more environment variables

		POSTGRESBIN	points to alternate bin/ directory
		POSTGRESTEMP	points to alternate template directory

	4) you can now create several databases at once.  example:

		createdb foo bar baz

	   will create three databases.
@
text
@d19 1
a19 1
# 	$Header: RCS/createdb,v 1.7 90/09/25 17:23:26 kemnitz Exp $
d69 1
a69 1
    PG=`echo $POSTGRESHOME | sed -e 's/\(.*\):.*/\1/'`
@


1.7
log
@Fixed stuff.
@
text
@d2 3
a4 1
# $Header: RCS/createdb,v 1.5 90/09/18 22:03:11 hong Exp $
d6 15
d25 7
a31 4
#
# Check arguments:
# 
#
d33 13
a45 1
if (test "$1" = "-d")
a47 8
    if (test ! -z "$2")
    then
        DBNAME=$1
    fi
elif (test -z "$1")
then
    BACKENDARGS="-COQ -ami"
    DBNAME=$USER
a49 1
    DBNAME=$1
d52 12
d73 3
d83 4
a86 1
if (test -f $PG/bin/backend)
d88 24
a111 2
    BACKEND=$PG/bin/backend
    PG_VERSION=$PG/bin/pg_version
d116 5
d123 5
a127 1
if (test -f $PG/files/local1_template1.bki)
d129 2
a130 2
    TEMPLATE=$PG/files/local1_template1.bki
    GLOBAL=$PG/files/global1.bki
a132 1
    echo "$CMDNAME: using template files in <obj-dir>/support"
d135 7
d143 2
d146 4
a149 4
#
# Create the shared relations.
# 

d158 1
d164 11
a174 6
        echo "$CMDNAME: cleaning up."
	for pg in $PGS
	do
           rm -rf $pg/data
	done
        exit 1
d193 11
a203 6
        echo "$CMDNAME: cleaning up."
	for pg in $PGS
	do
           rm -rf $pg/data
	done
        exit 1
d213 11
a223 7
        echo "$CMDNAME: cleaning up."
	for pg in $PGS
	do
           rm -rf $pg/data
	done
        rm -f /tmp/create.$$
        exit 1
d225 1
d229 38
a266 10
if (test -d $PG/data/base/$DBNAME)
then
    echo "$CMDNAME: database $DBNAME already exists!"
else
    for pg in $PGS
    do
        mkdir $pg/data/base/$DBNAME
        echo "$CMDNAME: making directory $pg/data/base/$DBNAME"
        cp $pg/data/base/template1/* $pg/data/base/$DBNAME
    done
a267 15
    echo "open pg_database" > /tmp/create.$$
    echo "insert ($DBNAME $UID $DBNAME)" >> /tmp/create.$$
    echo "show" >> /tmp/create.$$
    echo "close pg_database" >> /tmp/create.$$
    echo "$CMDNAME: creating database $DBNAME"
    $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 

    if (test $? -ne 0)
    then
        echo "$CMDNAME: could not log new database"
        echo "$CMDNAME: cleaning up."
	for pg in $PGS
	do
           rm -rf $pg/data/base/$DBNAME
	done
a268 1
        exit 1
d270 1
a270 3

    rm -f /tmp/create.$$
fi
@


1.6
log
@Updating from revision 1.$t2 to revision 1.$t
@
text
@d2 1
a2 1
# $Header: RCS/createdb,v 1.4 90/08/27 14:16:08 hong Exp Locker: hong $
d34 2
a35 1
    PG=$POSTGRESHOME
d38 7
d49 1
a49 1
elif (test -f $PG/*/support/backend)
d51 2
a52 2
    BACKEND=$PG/*/support/backend
    PG_VERSION=$PG/*/support/pg_version
d62 2
a63 2
    TEMPLATE=`echo $PG/*/support/local.bki`
    GLOBAL=`echo $PG/*/support/dbdb.bki`
d72 4
a75 1
    mkdir $PG/data $PG/data/base $PG/data/base/template1
d84 4
a87 1
        rm -rf $PG/data
d108 4
a111 1
        rm -rf $PG/data
d123 4
a126 1
        rm -rf $PG/data
d137 6
a142 3
    mkdir $PG/data/base/$DBNAME
    cp $PG/data/base/template1/* $PG/data/base/$DBNAME
    echo "$CMDNAME: making directory $PG/data/base/$DBNAME"
d155 4
a158 1
        rm -rf $PG/data/base/$DBNAME
@


1.5
log
@to support file striping
@
text
@d34 1
a34 2
    PG=`echo $POSTGRESHOME | sed -e 's/\(.*\):.*/\1/'`
    PGS=`echo $POSTGRESHOME | sed -e 's/:/ /g'`
a36 7
if (test -z "$POSTGRESTREE")
then
    TREE=$PG
else
    TREE=$POSTGRESTREE
fi

d41 1
a41 1
elif (test -f $TREE/*/support/backend)
d43 2
a44 2
    BACKEND=$TREE/*/support/backend
    PG_VERSION=$TREE/*/support/pg_version
d54 2
a55 2
    TEMPLATE=`echo $TREE/*/support/local.bki`
    GLOBAL=`echo $TREE/*/support/dbdb.bki`
d64 1
a64 4
    for pg in $PGS
    do
       mkdir $pg/data $pg/data/base $pg/data/base/template1
    done
d73 1
a73 4
	for pg in $PGS
	do
           rm -rf $pg/data
	done
d94 1
a94 4
	for pg in $PGS
	do
           rm -rf $pg/data
	done
d106 1
a106 4
	for pg in $PGS
	do
           rm -rf $pg/data
	done
d117 3
a119 6
    for pg in $PGS
    do
        mkdir $pg/data/base/$DBNAME
        echo "$CMDNAME: making directory $pg/data/base/$DBNAME"
        cp $pg/data/base/template1/* $pg/data/base/$DBNAME
    done
d132 1
a132 4
	for pg in $PGS
	do
           rm -rf $pg/data/base/$DBNAME
	done
@


1.4
log
@fixed a few stupid bugs
@
text
@d2 1
a2 1
# $Header: RCS/createdb,v 1.3 90/08/23 16:20:38 kemnitz Exp Locker: hong $
d34 2
a35 1
    PG=$POSTGRESHOME
d38 7
d49 1
a49 1
elif (test -f $PG/*/support/backend)
d51 2
a52 2
    BACKEND=$PG/*/support/backend
    PG_VERSION=$PG/*/support/pg_version
d62 2
a63 2
    TEMPLATE=`echo $PG/*/support/local.bki`
    GLOBAL=`echo $PG/*/support/dbdb.bki`
d72 4
a75 1
    mkdir $PG/data $PG/data/base $PG/data/base/template1
d84 4
a87 1
        rm -rf $PG/data
d108 4
a111 1
        rm -rf $PG/data
d123 4
a126 1
        rm -rf $PG/data
d137 6
a142 3
    mkdir $PG/data/base/$DBNAME
    cp $PG/data/base/template1/* $PG/data/base/$DBNAME
    echo "$CMDNAME: making directory $PG/data/base/$DBNAME"
d155 4
a158 1
        rm -rf $PG/data/base/$DBNAME
@


1.3
log
@Added debugging option; added some cleanup code.
@
text
@d2 1
a2 1
# $Header: RCS/createdb,v 1.2 90/08/22 17:53:47 kemnitz Exp Locker: kemnitz $
d54 2
a55 2
    TEMPLATE=$PG/*/support/local.bki
    GLOBAL=$PG/*/dbdb.bki
@


1.2
log
@Added some error checking and cleanup code.
@
text
@d2 1
a2 1
# $Header: RCS/createdb,v 1.1 90/08/22 17:31:02 kemnitz Exp Locker: kemnitz $
a6 1
DBNAME=$1
d8 21
d31 2
a32 2
	echo "$CMDNAME: POSTGRESHOME not set."
	exit 1
d34 1
a34 1
	PG=$POSTGRESHOME
d39 2
a40 2
	BACKEND=$PG/bin/backend
	PG_VERSION=$PG/bin/pg_version
d43 2
a44 2
	BACKEND=$PG/*/support/backend
	PG_VERSION=$PG/*/support/pg_version
d49 2
a50 2
	TEMPLATE=$PG/files/local1_template1.bki
	GLOBAL=$PG/files/global1.bki
d52 4
a55 4
	echo "$CMDNAME: warning: Make install has not been run"
	echo "$CMDNAME: using template files in $PG/*/support"
	TEMPLATE=$PG/*/support/local.bki
	GLOBAL=$PG/*/dbdb.bki
d64 4
a67 4
	mkdir $PG/data $PG/data/base $PG/data/base/template1
	echo "$CMDNAME: creating SHARED relations in $PG/data"
	echo "$CMDNAME: creating template database in $PG/data/base/template1"
	$BACKEND -COQ -ami template1 < $TEMPLATE 
d69 7
a75 7
	if (test $? -ne 0)
	then
		echo "$CMDNAME: could not create template database"
		echo "$CMDNAME: cleaning up."
		rm -rf $PG/data
		exit 1
	fi
d77 1
a77 3
	#
	# Add the template database to pg_database
	#
d79 3
a81 4
	echo "open pg_database" > /tmp/create.$$
	echo "insert (template1 $UID template1)" >> /tmp/create.$$
	echo "show" >> /tmp/create.$$
	echo "close pg_database" >> /tmp/create.$$
d83 4
a86 1
	$BACKEND -COQ -ami template1 < $GLOBAL 
d88 1
a88 7
	if (test $? -ne 0)
	then
		echo "$CMDNAME: could create shared relations"
		echo "$CMDNAME: cleaning up."
		rm -rf $PG/data
		exit 1
	fi
d90 7
a96 1
	$BACKEND -COQ -ami template1 < /tmp/create.$$ 
d98 13
a110 9
	if (test $? -ne 0)
	then
		echo "$CMDNAME: could not log template database"
		echo "$CMDNAME: cleaning up."
		rm -rf $PG/data
		rm -f /tmp/create.$$
		exit 1
	fi
	rm -f /tmp/create.$$
d115 1
a115 1
	echo "$CMDNAME: database $DBNAME already exists!"
d117 3
a119 3
	mkdir $PG/data/base/$DBNAME
	cp $PG/data/base/template1/* $PG/data/base/$DBNAME
	echo "$CMDNAME: making directory $PG/data/base/$DBNAME"
d121 6
a126 6
	echo "open pg_database" > /tmp/create.$$
	echo "insert ($DBNAME $UID $DBNAME)" >> /tmp/create.$$
	echo "show" >> /tmp/create.$$
	echo "close pg_database" >> /tmp/create.$$
	echo "$CMDNAME: creating database $DBNAME"
	$BACKEND -COQ -ami template1 < /tmp/create.$$ 
d128 8
a135 8
	if (test $? -ne 0)
	then
		echo "$CMDNAME: could not log new database"
		echo "$CMDNAME: cleaning up."
		rm -rf $PG/data/base/$DBNAME
		rm -f /tmp/create.$$
		exit 1
	fi
d137 1
a137 1
	rm -f /tmp/create.$$
@


1.1
log
@Initial revision
@
text
@d2 1
a2 1
# $Header: RCS/createdb,v 1.2 89/09/05 16:53:38 mao Version_2 $
d20 1
d24 1
d49 8
d68 8
d77 9
a86 1

d101 1
d103 10
a113 2

	echo "$CMDNAME: creating database $DBNAME"
@
