Merge branch 'mw/config'
[aiccu.git] / debian / aiccu.config
blob74c27c5bfd9569422ffe7594b6cf29b42cfb54fe
1 #!/bin/bash
3 set -ex
5 CONFIGFILE=/etc/aiccu.conf
6 BINARY=/usr/sbin/aiccu
8 TMPCONF=$(tempfile -p aiccu -s conf)
9 TMPFILE=$(tempfile -p aiccu -s temp)
10 BROKERS=$(tempfile -p aiccu -s brokers)
11 trap 'rm -f $TMPCONF $TMPFILE $BROKERS' TERM INT EXIT QUIT
13 if [ ! -x $BINARY ]; then
14 # Can't configure yet as we don't have our binary yet
15 exit 0;
18 # Make sure that files we create are not readable by anyone but us (root)
19 umask 077
21 . /usr/share/debconf/confmodule
23 if [ -e $CONFIGFILE ]; then
24 USERNAME=$(grep ^username $CONFIGFILE | awk '{print $2}')
25 PASSWORD=$(grep ^password $CONFIGFILE | awk '{print $2}')
26 PROTO=$(grep ^protocol $CONFIGFILE | awk '{print $2}')
27 SERVER=$(grep ^server $CONFIGFILE | awk '{print $2}')
28 TUNNEL=$(grep ^tunnel_id $CONFIGFILE | awk '{print $2}')
30 if [ "$USERNAME" != "" ]; then
31 db_set aiccu/username "$USERNAME"
34 if [ "$PASSWORD" != "" ]; then
35 db_set aiccu/password "$PASSWORD"
38 if [ "$PROTO" != "" -a "$SERVER" != "" ]; then
39 db_set aiccu/brokername "$PROTO://$SERVER"
42 if [ "$TUNNEL" != "" ]; then
43 db_set aiccu/tunnelname "$TUNNEL"
47 STATE=1
48 if [ "$DEBCONF_RECONFIGURE" = "1" -o "$1" = "reconfigure" ]; then
49 STATE=1
50 elif [ "$TUNNEL" != "" -a "$PROTO" != "" -a "$SERVER" != "" ]; then
51 # Assume that we do not need further configuration.
52 STATE=4
55 db_reset aiccu/badauth
57 # State What
58 # 1 Get Tunnel Brokername
59 # 2 Get User/pass
60 # 3 Get Tunnel ID
61 # 4 Exit
63 # Fetch the list of tunnel brokers
64 $BINARY brokers | sort > $BROKERS
65 BROKERS_RET="${PIPESTATUS[0]}"
67 while [ $STATE -ge 1 -a $STATE -le 3 ]; do
68 case "$STATE" in
70 if [ $BROKERS_RET -ne 0 ]; then
71 # No TunnelBrokers found
72 db_input high aiccu/nobrokers || true
73 echo "No brokers"
74 else
75 # Found Tunnel brokers, present them to the user
76 BROKERS_=$(cat $BROKERS | cut -f1 -d'|' | awk '{print $0","}')
77 BROKERS_=$(echo -n $BROKERS_ | sed 'N;s/\n//g' | sed 's/,$//g')
78 db_get aiccu/brokername
79 if [ "$RET" != "" ]; then
80 SELECTED=$(grep "$RET" $BROKERS | cut -f1 -d'|')
81 db_set aiccu/brokername "$SELECTED"
83 db_subst aiccu/brokername brokers "$BROKERS_"
84 db_input high aiccu/brokername || true
85 db_go || true
90 # Request User / Pass
91 db_input high aiccu/username || true
92 db_input high aiccu/password || true
93 db_go || true
97 # Reset our temp config file
98 echo "# Temporary AICCU config written by debconf" > $TMPCONF
99 #echo "verbose true" >> $TMPCONF
101 # Take the Protocol and server from the Brokername
102 db_get aiccu/brokername
103 URL=$(cat $BROKERS | grep "$RET")
104 PROTO=$(echo $URL | cut -f2 -d'|' | cut -f1 -d:)
105 SERVER=$(echo $URL | cut -f2 -d'|' | cut -f3 -d/)
106 db_set aiccu/brokername "$PROTO://$SERVER"
108 echo "protocol $PROTO" >> $TMPCONF
109 echo "server $SERVER" >> $TMPCONF
111 db_get aiccu/username
112 USERNAME="$RET"
114 db_get aiccu/password
115 PASSWORD="$RET"
117 # Try to get the tunnels using the provided user/pass
118 if [ "$USERNAME" != "" -a "$PASSWORD" != "" ]; then
119 echo "username $USERNAME" >> $TMPCONF
120 echo "password $PASSWORD" >> $TMPCONF
122 TUNNELS=$($BINARY tunnels $TMPCONF >$TMPFILE || echo badauth)
124 if [ "$TUNNELS" = "badauth" ]; then
125 db_input high aiccu/badauth || true
126 else
127 db_set aiccu/badauth "false"
129 TUNNELS=$(cat $TMPFILE | cut -f1 -d' ' | awk '{print $0","}')
130 TUNNELS=$(echo -n $TUNNELS | sed 'N;s/\n//g' | sed 's/,$//g')
132 if [ "$TUNNELS" = "" ]; then
133 db_input high aiccu/notunnels || true
134 else
135 db_subst aiccu/tunnelname tunnels "$TUNNELS"
136 db_input high aiccu/tunnelname || true
137 db_go || true
140 else
141 db_set aiccu/badauth "false"
144 esac
146 db_go
148 case "$STATE" in
150 STATE=2
153 STATE=3
156 db_get aiccu/badauth
158 # When badly authenticated do it all over
159 if [ "$RET" = "true" ]; then
160 STATE=1
161 db_reset aiccu/brokername
162 db_reset aiccu/username
163 db_reset aiccu/password
164 db_reset aiccu/tunnelname
165 else
166 STATE=4
168 db_reset aiccu/badauth
170 esac
171 done
173 rm -f $TMPCONF $TMPFILE $BROKERS