recorded this file from former fc4-based build
[infrastructure.git] / scripts / onelab-aliases.sh
1 #!/bin/bash
2
3 # $Id$
4
5 # to be run on the mail server at one-lab.org, to add aliases in that domain
6 # Example:
7 # new-alias.sh francois2.jan@orange-ftgroup.com
8 # -> creates two aliases
9 # francois2.jan@one-lab.org
10 # francois2.jan.private@one-lab.org
11
12 COMMAND=$(basename $0)
13
14 SQL="psql -U mail mail"
15
16 function usage () {
17   echo "Usage: $command alias1 [ .. aliasn]"
18   exit 1
19 }
20
21 function create () {
22
23   target=$1; shift
24
25   namepart=$(echo $target | cut -s -d @ -f 1)
26   domainpart=$(echo $target | cut -s -d @ -f 2)
27   if [ -z "$namepart" -o -z "$domainpart" ] ; then
28     echo "WARNING: $target skipped"
29     continue
30   fi
31   for suffix in "" ".private" ; do
32     aliasname=${namepart}${suffix}@one-lab.org
33     sqlcommand="insert into alias values ( '${aliasname}', '${target}' )"
34     echo "Sending $sqlcommand"
35     echo "$sqlcommand" | $SQL
36   done
37
38 }
39
40 function list_aliases () {
41   echo 'select * from alias;' | $SQL
42 }
43   
44
45 function main () {
46
47   targets="$@" 
48
49   if [[ -z "$targets" ]] ; then
50     list_aliases
51   else
52     for target in "$@" ; do
53       create $target
54     done
55   fi
56 }
57
58 main "$@"