#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

add_user system x 430 430 "service" "/var/run/connman" "/bin/sh"
add_group system 430

mkdir -p $INSTALL/etc
  ln -sf /var/cache/resolv.conf $INSTALL/etc/resolv.conf

mkdir -p $INSTALL/etc/connman
  cp $PKG_BUILD/src/main.conf $INSTALL/etc/connman
    sed -i $INSTALL/etc/connman/main.conf \
        -e "s|^# BackgroundScanning.*|BackgroundScanning = true|g" \
        -e "s|^# FallbackNameservers.*|FallbackNameservers = 8.8.8.8,8.8.4.4|g" \
        -e "s|^# PreferredTechnologies.*|PreferredTechnologies = ethernet,wifi,cellular|g" \
        -e "s|^# TetheringTechnologies.*|TetheringTechnologies = wifi|g" \
        -e "s|^# PersistentTetheringMode.*|PersistentTetheringMode = true|g"

mkdir -p $INSTALL/etc/dbus-1/system.d
  cp $PKG_BUILD/src/connman.conf $INSTALL/etc/dbus-1/system.d

mkdir -p $INSTALL/usr/bin
  cp -P $PKG_BUILD/client/connmanctl $INSTALL/usr/bin

mkdir -p $INSTALL/usr/lib/connman
  cp -P $PKG_BUILD/test/* $INSTALL/usr/lib/connman

mkdir -p $INSTALL/usr/lib/connman/plugins
  cp -P $PKG_BUILD/plugins/.libs/*.so* $INSTALL/usr/lib/connman/plugins || true

mkdir -p $INSTALL/usr/lib/connman/scripts
  cp -P $PKG_BUILD/scripts/.libs/*.so* $INSTALL/usr/lib/connman/scripts || true

mkdir -p $INSTALL/usr/sbin
  cp -P $PKG_BUILD/src/connmand $INSTALL/usr/sbin

mkdir -p $INSTALL/usr/config
  cp $PKG_DIR/config/hosts.conf $INSTALL/usr/config

mkdir -p $INSTALL/usr/share/connman/
  cp $PKG_DIR/config/settings $INSTALL/usr/share/connman/

if [ "$PPTP_SUPPORT" = yes -o "$OPENVPN_SUPPORT" = yes ]; then
  mkdir -p $INSTALL/etc/dbus-1/system.d
    cp $PKG_BUILD/vpn/connman-vpn-dbus.conf $INSTALL/etc/dbus-1/system.d

  mkdir -p $INSTALL/usr/lib/connman/plugins-vpn
    cp -P $PKG_BUILD/vpn/plugins/.libs/*.so $INSTALL/usr/lib/connman/plugins-vpn || true

  mkdir -p $INSTALL/usr/sbin
    cp -P $PKG_BUILD/vpn/connman-vpnd $INSTALL/usr/sbin

  mkdir -p $INSTALL/usr/share/dbus-1/system-services
    cp -P $PKG_BUILD/vpn/net.connman.vpn.service $INSTALL/usr/share/dbus-1/system-services
fi

if [ "$OPENVPN_SUPPORT" = yes ]; then
  mkdir -p $INSTALL/usr/lib/connman/scripts
    cp -P $PKG_BUILD/scripts/openvpn-script $INSTALL/usr/lib/connman/scripts
fi
