#!/bin/bash
#
# $Id: get_account_by_domain 22 2005-04-11 17:21:15Z jerome $
# ----------------------------------------------------------------------
# AlternC - Web Hosting System
# Copyright (C) 2002 by the AlternC Development Team.
# http://alternc.org
# ----------------------------------------------------------------------
# Based on:
# Valentin Lacambre's web hosting softwares: http://altern.org/
# ----------------------------------------------------------------------
# LICENSE
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# To read the license please visit http://www.gnu.org/copyleft/gpl.html
# ----------------------------------------------------------------------
# Original Author of file: Jerome Moinet
# Purpose of file: gives account by domains or sub-domains
# ----------------------------------------------------------------------
#
PATH=""
PROG_NAME=get_account_by_domain
PROG_VERSION=0.1.0
ALTERNC_ETC=/etc/alternc
ALTERNC_CONF_FILE=$ALTERNC_ETC/local.sh
export TEXTDOMAIN=alternc-admintools

# Be sure to use the right programs on Debian
# and be sure they are there
id=/usr/bin/id
mysql=/usr/bin/mysql
sed=/bin/sed
gettext=/usr/bin/gettext
printf=/usr/bin/printf


# Must have gettext first to display error messages
[ -x "$gettext" ] || { echo "Cannot execute $gettext"; exit 1 ; }

for i in $id $mysql $sed $printf
do
  ! [ -x "$i" ] && { echo "$($gettext "Unable to execute") ${i}."; exit 1 ; }
done


# Language-dependent messages
# Uses gettext and mo files.
# Don't change these messages, change the .po file instead.
HELP=$($gettext "Gives account hosting domain or sub-domain.")
USAGE=`$printf "$($gettext "Usage: %s [domain|sub-domain].")" $PROG_NAME`
NOT_FOUND_MSG=$($gettext "does not exist.")
NON_ROOT_MSG=$($gettext "You have to be root (uid 0) to execute this program.")
MISSING_PROG=$($gettext "Unable to execute")
MISSING_CONF_FILE=`$printf "$($gettext "Can't find %s. Are you sure AlterncC is properly installed?")" $ALTERNC_CONF_FILE`
MYSQL_UNREACHABLE_DATABASE=`$printf "$($gettext "Cannot access accounts database. Please check either %s or Mysql state.")" $ALTERNC_CONF_FILE`


#-------------------------
# Main
#-------------------------
# Must be root
[ "`$id -u`" != "0" ] && { echo $NON_ROOT_MSG ; exit 1 ; }
# Must have minimum 1 parameter
[ -z "$1" ] && { echo $USAGE ; exit 1 ; }
# Handle -h and --help flags
[ "$1" = "-h" ] || [ "$1" = "--help" ] && { echo $HELP ; echo $USAGE ; exit 0 ; }
# Have to get AlternC conf file :
! [ -f "$ALTERNC_CONF_FILE" ] && { echo $MISSING_CONF_FILE ; exit 1 ; } || . $ALTERNC_CONF_FILE
mysql="$mysql --defaults-file=/etc/alternc/my.cnf"
$mysql -e "desc domaines;" > /dev/null 2>&1
[ "$?" != 0 ] && { echo "$MYSQL_UNREACHABLE_DATABASE" ; exit 1 ; }

# Does the stuff
$mysql -B -N -e "select concat(a.login, \" (\", a.mail, \")\") from membres a, sub_domaines b where a.uid = b.compte and concat(if(sub=\"\", \"\", concat(sub, \".\")), domaine)  = \"${1}\";"


