#!/bin/sh
#
# Create ~/.mediaserv and copy the static files we care about into it.

STATIC_DIR="/opt/local/share/mediaserv"

[ -d ~/.mediaserv ] || mkdir ~/.mediaserv || exit
[ -f ~/.mediaserv/config ] && touch ~/.mediaserv/config

cp -vrp "${STATIC_DIR}/"* ~/.mediaserv/

cp ~/.mediaserv/config ~/.mediaserv/config~
perl -e '
  open(IN, "<$ENV{HOME}/.mediaserv/config~");
  while(<IN>) { $settings{$1} = $2 if /^\s*(\w+)\s*=\s*(.*)$/; }
  close(IN);
  
  while(<>) {
    print;
    print "$1 = $settings{$1}\n" if /(\w+)\s*=\s*/ and $settings{$1};
  }
' "${STATIC_DIR}/config" >~/.mediaserv/config

echo "Install of mediaserv complete."

