#!/bin/bash
#
# Version: 0.0.1 (Dec 4th 2013)
#
# Copyright (c) 2013 - F1RMB, Daniel Caujolle-Bert <f1rmb.daniel@gmail.com>
#                      F4FPQ, Thierry Mille <f4fpq@free.fr>
# Licended under GPL v2 or later
#
# ############################# #
# EchoLink Links/Repeaters list #
# ############################# #
#
CONF_FILE=svxlink.conf
CONF_DIR=`dirname ${2:-/etc/svxlink}/CONF_FILE`

full_list()
{
#
# all empty lines and commented with two # are ignored
#
cat <<EOF

##*        *#
###** Test **#
##*       *#
##D1#    = EchoLink Test
##1=EchoLink:9999#

#*                   *#
#** Links/Repeaters **#
#*                   *#
#D3#    = F4DKF-L - 145.3375 - CTCSS: 94.8 - Montlucon - Dep. 03
3=EchoLink:469611#
#D5#    = F4EGG-L - 145.2425 - Briancon - Dep. 05
5=EchoLink:242142#
#D9#    = F6DEV-R - 145.650 - Ariege - Rimont - Dep. 09
9=EchoLink:905907#
#D17#   = F4ERG-L - 144.650 - Dep. 17
17=EchoLink:540983#
#D117#  = F4HES-L - 145.300 - Charente-Maritime - Royan - Dep. 17
117=EchoLink:5727#
#D217#  = F8DHA-L - 144.6875 - Charente-Maritime - Saint-Leger - Dep. 17
217=EchoLink:678177#
#D18#   = F4CQA-L - 145.2375 - Cher - Trouy - Dep. 18
18=EchoLink:774130#
#D24#   = F5ZFX-R - 430.200 - Dordogne - Sarlet / Perigord Noir - Dep. 24
24=EchoLink:827203#
#D30#   = F5ONG-L - 145.300 / 431.875 - Lezan - Dep. 33/17
30=EchoLink:142741#
#D31#   = F5UFI-L - 144.6125 Toulouse - Dep. 31
31=EchoLink:386537#
#D33#   = F5PON-L - 145.287 / 433.450 - Dep. 33/14
33=EchoLink:293547#
#D37#   = F1RIQ-L - 433.375 - Tours - Dep. 37
37=EchoLink:457662#
#D38#   = F4FPQ-L - 145.3375 - Grenoble - Dep. 38
38=EchoLink:386535#
#D138#  = F1RMB-L - 433.9875 - Grenoble - Dep. 38
138=EchoLink:918995#
#D41#   = F4BEV-L - 145.3375 - Romorantin - Dep. 41
41=EchoLink:256796#
#D45#   = F0DYA-L - 145.300 - Orleans - Dep. 45
45=EchoLink:175199#
#D51#   = F5RZU-L - 145.6375 - Connected to F5ZDL Repeater - Champagne - Dep. 51
51=EchoLink:51005#
#D57#   = F8KGY-L - 145.288 - Moselle - Thionville - Dep. 57
57=EchoLink:850955#
#D59#   = F5NTS-L - 144.800 - Cambrais - Dep.59
59=EchoLink:486287#
#D159#  = F4FHM-L - 145.3375 - Nord-Pas-de-Calais - Aulnoye-Aymeries - Dep. 59
159=EchoLink:484681#
#D66#   = F8BSY-L - 145.3625 - Perpignan - Dep. 66
66=EchoLink:127113#
#D69#   = F1JMN-L - 435.2625 Lyon - Dep.69
69=EchoLink:496345#
#D83#   = F1VJT-L - 431.4000 CTCSS: 88.5 Local - 29.5100 MHz CTCSS: 88.5 Remote - Hyeres - Dep. 83
83=EchoLink:198079#
#D85#   = F8BIB-L - 145.2625 - Vendee - Dep. 85
85=EchoLink:688734#
#D185#  = F6ITK-L - 145.2375 - Vendee - Saint Gilles Croix de Vie - Dep. 85
185=EchoLink:920345#
#D87#   = F5ZGM-R - 145.650 - CTCSS: 88.5 -Limoge - Dep. 87/23/19
87=EchoLink:801252#
#D91#   = F4GLU-L - 145.3375 - Essonne - Morsang-Sur-Orge - Dep. 91
91=EchoLink:879763#
#D92#   = F1TUV-L - 145.2875 - Nanterre - Dep. 92
92=EchoLink:890232#
#D561#  = HB9VSD-R - 145.3375 - Suisse - Valais - Bagnes - Verbier
561=EchoLink:903017#
#D972#  = FM0JZ-L - 145.2375 - La Martinique - Dep. 972
972=EchoLink:792677#
#D988#  = FK8HM-L - 145.600 - Nouvelle Caledonie - Noumea - Dep. 988
988=EchoLink:224846#

#*              *#
#** Conference **#
#*              *#
######################################################
##            DTMF Coding Table Helper:
##
##      | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
##      +---+---+---+---+---+---+---+---+---+---+
##    0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
##    1 | Q | A | D | G | J | M | P | T | W |   |
##    2 | Z | B | E | H | K | N | R | U | X |   |
##    3 |   | C | F | I | L | O | S | V | Y |   |
##      +---+---+---+---+---+---+---+---+---+---+
##
##      A: 21, B: 22, Y: 93, K: 52
##
##      Language Prefixes:
##        F: Français: 1 33
##        E: English:  1 32
##        G: German:   1 41
##        ...more to come...
##
## Conf Prefix:
## 1 <Language Prefix><Number>, eg: F1: 1331, G0: 1410
##
######################################################
#D1330# = French conferency "Bonjour"
1330=EchoLink:9263#
#D1331# = Belgium conferency
1331=EchoLink:525838#
#D1333# = Quebec conferency
1333=EchoLink:552266#
#D1334# = Switzerland conferency
1334=EchoLink:338224#

EOF
}

case "$1" in
    list)
	full_list | grep ^#D | sed -e 's/^#//g'
	;;

    conf)
	full_list | grep -v ^## | grep -v "^[[:space:]]*$"
	;;

    rebuild)
	if [ -f $CONF_DIR/$CONF_FILE ]; then
	    export LINKLIST=`full_list | grep -v ^## | grep -v "^[[:space:]]*$"`
	    export retval=0
	    echo -n "Rebuilding $CONF_DIR/$CONF_FILE: "
	    ( cat $CONF_DIR/$CONF_FILE | awk '/#@@MACROS_BEGIN/{OFS="";p=1;print;print ENVIRON["LINKLIST"]}/#@@MACROS_END/{p=0}!p' 2>/dev/null > /tmp/$CONF_FILE && \
		mv -f /tmp/$CONF_FILE $CONF_DIR/$CONF_FILE > /dev/null 2<&1 ) && retval=1 || retval=0;

	    if [ "$retval" -eq 1 ]; then
		echo "[Success]"
	    else
		echo "[Failure]"
		exit 1
	    fi
	else
	    echo "File $CONF_DIR/$CONF_FILE is missing..."
	fi
	;;

    *)
	echo "Usage: <list | conf | rebuild [directory]>"
	exit 1
	;;
esac

exit 0
