#!/bin/sh

# ######################################################
# Program:  install-book
# Author :  Jay Sissom
# Date   :  12/9/98
# Purpose:  This will install all the objects needed for
#           the book store demo program.
# ######################################################
# Pass it the name of the server and the sa password
# ######################################################

DATABASE=$1
PASSWORD=$2

if [ "X${DATABASE}" = "X" ]
then
  echo "You must type $0 databaseserver password"
  exit 1
fi

if [ "X${PASSWORD}" = "X" ]
then
  echo "You must type $0 databaseserver password"
  exit 1
fi

# Check the $SYBASE variable
if [ ! -f $SYBASE/interfaces ]
then
  echo "The SYBASE environment variable is not set"
  exit 1
fi

# Check the name of the database
if [ `grep -i "^${DATABASE} " $SYBASE/interfaces | wc -l` -ne 1 ]
then
  echo "The database server ${DATABASE} does not exist"
  exit 1
fi

# Make sure /opt/sybase/devices exists
if [ ! -d /opt/sybase/devices ]
then
  echo "The directory /opt/sybase/devices does not exist."
  echo "Type Y to create it?"
  read ans
  if [ ${ans} != 'Y' ]
  then
    echo "Aborting installation."
    exit 1
  fi

  mkdir /opt/sybase/devices

  if [ ! -d /opt/sybase/devices ]
  then
    echo "Unable to create /opt/sybase/devices.  Aborting."
    exit 1
  fi
fi
  
echo "Ready to install in database server ${DATABASE}.  This will require"
echo "30MB of space and install the devices in "
echo "/opt/sybase/devices. If you want to change this directory, edit the"
echo "file ./devices/make_devices.sql.  Type Y to continue."
read ans
if [ ${ans} != 'Y' ]
then
  echo "Aborting installation."
  exit 1
fi

# Now we're ready to install

# Do the devices directory
for file in `ls ./devices/*.sql | sort`
do
  $SYBASE/bin/isql -Usa -P${PASSWORD} -i $file -S ${DATABASE}
done
 
# Do the database directory
for file in `ls ./database/*.sql | sort`
do
  $SYBASE/bin/isql -Usa -P${PASSWORD} -i $file -S ${DATABASE}
done

# Do the tables directory
for file in `ls ./tables/*.sql | sort`
do
  $SYBASE/bin/isql -Usa -P${PASSWORD} -i $file -S ${DATABASE}
done

# Do the procs directory
for file in `ls ./procs/*.sql | sort`
do
  $SYBASE/bin/isql -Usa -P${PASSWORD} -i $file -S ${DATABASE}
done

