<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:strip-space elements="*"/>
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template name="option">
  <xsl:if test="$value!=''">
    <xsl:text>    option </xsl:text>
    <xsl:value-of select="$name"/>
    <xsl:text> "</xsl:text>
    <xsl:value-of select="$value"/>
    <xsl:text>"</xsl:text>
<xsl:text>
</xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="sysconfig">
<xsl:text># This file is automatically generated from XML
</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="system">
<xsl:text>
cat &gt; /etc/config/system &lt;&lt;END_OF_FILE
</xsl:text>
<xsl:text>config system
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">boot_wait</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="settings/@boot_wait"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">hostname</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="settings/@hostname"/></xsl:with-param>
</xsl:call-template>

<xsl:text>END_OF_FILE
</xsl:text>
</xsl:template>

<xsl:template match="network">
<xsl:text>
cat &gt; /etc/config/network &lt;&lt;END_OF_FILE
</xsl:text>
<xsl:apply-templates select="switch"/>
<xsl:apply-templates select="interface"/>

<xsl:text>END_OF_FILE
</xsl:text>

<xsl:apply-templates select="dhcp"/>
</xsl:template>

<xsl:template match="switch">
<xsl:text>config switch </xsl:text>
<xsl:value-of select="@id"/>
<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">vlan0</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@vlan0"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">vlan1</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@vlan1"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="network/dhcp">
<xsl:text>
cat &gt; /etc/config/dhcp &lt;&lt;END_OF_FILE
</xsl:text>
<xsl:text>config dhcp
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">interface</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@interface"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">start</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@start"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">limit</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@limit"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">lease</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@lease"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">options</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@options"/></xsl:with-param>
</xsl:call-template>

<xsl:text>END_OF_FILE
</xsl:text>
</xsl:template>

<xsl:template match="interface">
config interface <xsl:value-of select="@id"/>
<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">type</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@type"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">ifname</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@ifname"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">proto</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@proto"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">ipaddr</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@ipaddr"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">netmask</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@netmask"/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="option">
  <xsl:with-param name="name">gateway</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@gateway"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="hotspot">
<xsl:text>
cat &gt; /etc/config/hotspot &lt;&lt;END_OF_FILE
</xsl:text>
<xsl:apply-templates/>
<xsl:text>END_OF_FILE
</xsl:text>
</xsl:template>

<xsl:template match="nas">
config nas <xsl:value-of select="@id"/>
<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">network</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@network"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="proxy">
config proxy <xsl:value-of select="@id"/>
<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">type</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@type"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="location">
config location <xsl:value-of select="@id"/>
<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">location_name</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@location_name"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="hotspot/dhcp">
config dhcp<xsl:text>
</xsl:text>

<xsl:call-template name="option">
  <xsl:with-param name="name">location_name</xsl:with-param>
  <xsl:with-param name="value"><xsl:value-of select="@location_name"/></xsl:with-param>
</xsl:call-template>

</xsl:template>

</xsl:stylesheet>
