#!/bin/bash
####################################################### 
#
# Checks
#
# This file contains checks run by createusers and its
# supporting programs.
#
#######################################################

#############
# Main code #
#############

# See if we are root. If not, complain on the standard error
# and exit with return code false.
if ! [ $UID -eq 0 ]
then
     echo "You must be root to run this program." >&2
     exit 1
fi 

#####################
# Input file checks #
#####################

echo "Running initial checks..."

# See if we can read the input file.
if ! test -r $1
then
   echo "Could not read input file '$1'" >&2
   exit 1
fi


###########################
# File dependency checks  #
###########################


# Check config file is readable.
if ! test -e /etc/createusers.conf; then
     echo "The createusers configuration file" \  
          "'/etc/createusers.conf' was not found" \
          "or is not readable." >&2
     error=1
fi

# See if we have Expect.
if ! test -x /usr/bin/expect; then
     echo "The Expect programming language is required" \
          "but was not found in the location /usr/bin/expect." \
          "Please make sure Expect is installed and it is available" \
          "in that location. Expect requires the TCL scripting" \
          "language installed." >&2
     error=1 
fi

# See if all the script files are available.
for i in "autopasswd" \
         "createusers" \
         "removeusers" \
         "updateuserpasswd"
      do
	 if ! test -x /usr/sbin/$i; then
	      echo "The program '$i' supplied with" \
              "createusers is not in the location" \
              "/usr/sbin/$i, or it is not executable." >&2
	      error=1 
	 fi
done


#####################
# Parameter checks  #
#####################

###################
# General section #
###################

echo "Reading /etc/createusers.conf..."
# Get parameters.
create_home_dir=`grep "^create_home_dir" \
                 /etc/createusers.conf` \
                 > /dev/null

default_shell=`grep "^default_shell" \
               /etc/createusers.conf | cut -f2 -d "="` \
               > /dev/null

if ! grep "$default_shell" /etc/shells > /dev/null; then
   echo "The default shell in '/etc/createusers.conf' is set to" \
        "'$default_shell', but no corresponding shell was found" \
        "in '/etc/shells'." >&2
   error=1
fi

#################
# MySQL section #
#################

# See if MySQL support is enabled.
if grep "^make_mysql_databases" \
         /etc/createusers.conf \
         > /dev/null
then

   # Get MySQL parameters.
   mysql_server=`grep "^mysql_server" \
                 /etc/createusers.conf \
                 | cut -f2 -d "="` \
                 > /dev/null

   mysql_allowed_hosts=`grep "^mysql_allowed_hosts" \
                        /etc/createusers.conf \
                        | cut -f2 -d "="` \
                        > /dev/null

   # See if the MySQL server is alive.
   if ! mysqladmin -h $mysql_server \
                    ping \
                    > /dev/null
   then
      echo "The MySQL server on '$mysql_server' is not accessible." \
           "Perhaps the server is not running, or the MySQL password" \
           "in ~/.my.cnf is not correctly set." >&2
      error=1
   fi

fi


######################
# PostgreSQL section #
######################

# See if PostgreSQL is enabled.

if grep "^make_postgres_databases" \
        /etc/createusers.conf \
        > /dev/null
then
   # Get PostgreSQL parameters.
   postgres_server=`grep "^postgres_server" \
                   /etc/createusers.conf \
                   | cut -f2 -d "="`

   if ! cat ~/.pg.password | psql -h $postgres_server -U postgres -W \
                             --command "\q" 
   then
        echo "The PostgreSQL server on '$postgres_server' is not accessible." \
             "Perhaps the server is not running, or the PostgreSQL password" \
             "in /.pg.password is not correctly set." >&2
        error=1
   fi
fi


#################
# Samba section #
#################

# See if Samba is enabled.
if grep "^create_samba_users" \
         /etc/createusers.conf \
         > /dev/null
then

  smbpasswd_file=`grep "^smbpasswd_file" \
                        /etc/createusers.conf \
                        | cut -f2 -d "="` \
                        > /dev/null

  if ! test -e $smbpasswd_file; then
       echo "The Samba password file '$smbpasswd_file'" \
            "not found. Perhaps Samba is not installed," \
            "or the 'smbpasswd' file is in another location." \
            "If you have just installed Samba, the smbpasswd file " \
            "may not be created automatically. Check your smb.conf" \
            "Samba configuration file to see where the smbpasswd file" \
            "should be. Create an empty file there if necessary." >&2
       error=1
  fi

  if ! [ $create_home_dir ]; then
     echo "The 'create_home_dir' directive in '/etc/createusers.conf'" \
          "is missing or commented out, but users must have a home" \
          "directory in order to be added to Samba." >&2
     error=1
  fi

fi
# end if Samba enabled


###############
# Final check #
###############

if [ $error ]; then
   exit 1
fi
