1#!/bin/bash
2
3RML_VERSION=2.0.0
4DEFAULT_TGWB=0
5MCASTPROXY="mcastproxy"
6TGWB_CONFIG_DIR="$HOME/.tgwb"
7TGWB_CONFIG="$TGWB_CONFIG_DIR/tgwb.conf"
8PROXY_CONFIG="$TGWB_CONFIG_DIR/mcastproxy.conf"
9PROXY_PIDFILE="$TGWB_CONFIG_DIR/mcastproxy.pid"
10
11usage(){
12    cat <<END
13
14    Usage:
15        $0 [<--default-tgwb-only>]
16    Where
17        --default-tgwb-only: do not ask anything just run tgwb with default config
18    
19END
20}
21
22create_tgwb_config(){
23
24    cat <<END>$TGWB_CONFIG
25#Reliable Multicast Library configuration file
26
27RM_VERSION=$RML_VERSION
28TRANSMISSION_MODE=0
29DEST_IP=$TGWB_IP
30DEST_PORT=$TGWB_PORT
31TTL=1
32MICROSLEEP=10
33LOG_FILE=$TGWB_LOG
34
35TIMER_DISTRIBUTION=0
36TIMER_PARAM_A=2
37TIMER_PARAM_B=2
38TIMER_PARAM_C=5
39TIMER_PARAM_D=2
40TIMER_PARAM_E=2
41TIMER_PARAM_F=2
42
43HOSTS_IDENTIFIED=0
44DEFAULT 50
45
46MAX_NAK=100
47
48MAX_MEMBER_CACHE_SIZE=4000 
49NEW_MEMBER_SUPPORT=0
50STATISTICS=0
51REFRESH_TIMER=10
52LOSS_PROB=0
53LEAVE_GROUP_WAIT_TIME=500000
54RCV_BUFFER_SIZE=10000
55
56END
57}
58
59create_proxy_config(){
60
61    cat <<END>$PROXY_CONFIG 
62GROUPADDR=$TGWB_IP
63NADDR=1
64ADDRLIST
65$PROXY_IP
66TTL=1
67REUSEADDR=1
68LOOPBACK=1
69UCASTPORT=$PROXY_PORT
70MCASTPORT=$TGWB_PORT
71
72END
73}
74
75create_config_files(){
76    echo "Multicast IP address to use [default 225.1.2.3]:"
77    read TGWB_IP
78    if [ "x"$TGWB_IP = "x" ]; then
79        echo "  Using default IP address 225.1.2.3"
80        TGWB_IP="225.1.2.3"
81    fi
82    echo "Port [default 5151]"
83    read TGWB_PORT
84    if [ "x"$TGWB_PORT = "x" ]; then
85        echo "   Using default Port 5151"
86        TGWB_PORT="5151"
87    fi
88    if [ "x"$RUN_PROXY = "xy" ]; then
89        while [ "x"$PROXY_IP = "x" ];do
90            echo "Receiver mcastproxy IP address:"
91            read PROXY_IP
92        done
93
94        echo "Receiver mcastproxy Port [32566]:"
95        read PROXY_PORT
96        if [ "x"$PROXY_PORT = "x" ]; then
97            echo "  Using default Port 32566"
98            PROXY_PORT=32566
99        fi
100        create_proxy_config
101    fi
102
103    create_tgwb_config
104}
105
106echo
107echo "##########################################"
108echo "    Tangram-II Whiteboard version $RML_VERSION"
109echo "         by Jorge Allyson Azevedo"
110echo "          allyson@land.ufrj.br"
111echo "         http://www.land.ufrj.br"
112echo "#########################################"
113echo
114
115if [ $# -gt 0 ]; then
116    if [ $# = 1 ] && [ "$1" = "--default-tgwb-only" ]; then
117        DEFAULT_TGWB=1
118    else
119        usage
120        exit 1
121    fi
122fi
123
124TGWB_CMD=`which tgwb-bin1`
125if [ $? != 0 ]; then
126    echo
127    echo -n "[Warning] tgwb-bin not found, trying to find tgif ... "
128    TGWB_CMD=`which tgif`
129    if [ $? != 0 ]; then
130        echo
131        echo "[Error] tgif not found."
132        exit 1
133    else
134        tgif -print -justversion 2>&1 | awk '{if(( $3 == 4.1 ) && ( $5 >= 46 )){exit 0}else{exit 1}}'
135        if [ $? != 0 ]; then
136            echo
137            echo "[Error] tgif version must be equal or greater than 4.1.46"
138            echo
139            exit 1
140        else
141            echo "OK"
142            TGWB_CMD="tgif -tgrm2 -tgwb2 -sbim xim"
143        fi
144    fi
145fi
146
147if [ -d $TGWB_CONFIG_DIR ]; then
148    echo "Config directory $TGWB_CONFIG_DIR found."
149else
150    echo "Config directory $TGWB_CONFIG_DIR not found, creating it ..."
151    mkdir $TGWB_CONFIG_DIR && STATUS=0 || STATUS=1
152    if [ $STATUS -eq 0 ]; then
153        echo  "  $TGWB_CONFIG_DIR created."
154    else
155        echo "could not create $TGWB_CONFIG_DIR"
156        exit 1
157    fi
158fi
159
160if [ -e $TGWB_CONFIG ] && [ $DEFAULT_TGWB != 1 ]; then
161    echo "Do you want to change your TGWB configuration? [y|n]"
162    read RECONFIGURE
163fi
164
165while [ "x"$RUN_PROXY != "xy" ] && [ "x"$RUN_PROXY != "xn" ] && [ $DEFAULT_TGWB != 1 ];do
166    echo "Do you want to run the mcastproxy program? [y|n]"
167    read RUN_PROXY
168done
169
170MCASTPROXY_CMD=`which $MCASTPROXY | head -1`
171if [ $? != 0 ] && [ $DEFAULT_TGWB != 1 ]; then
172    echo "[Error] mcastproxy not found. Check your installation!"
173    exit 1
174fi
175
176if [ "x"$RECONFIGURE = "x" ] || [ "x"$RECONFIGURE = "xy" ] && [ $DEFAULT_TGWB != 1 ]; then
177   create_config_files
178fi
179
180if [ -e $PROXY_PIDFILE ] && [ $DEFAULT_TGWB != 1 ]; then
181    PROXY_PID=`cat $PROXY_PIDFILE`
182    kill -9 $PROXY_PID > /dev/null 2>&1
183    killall -9 mcastproxy > /dev/null 2>&1
184    rm -f $PROXY_PIDFILE
185fi
186
187# Execute mcastproxy first ...
188if [ "x"$RUN_PROXY = "xy" ]; then
189    $MCASTPROXY_CMD &
190fi
191
192# ... and then tgwb
193$TGWB_CMD
194
195if [ "x"$RUN_PROXY = "xy" ]; then
196    PROXY_PID=`cat $PROXY_PIDFILE`
197    kill -9 $PROXY_PID > /dev/null 2>&1
198    rm -f $PROXY_PIDFILE
199fi
200