#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2017 Stephan Raue (stephan@openelec.tv)

SMB_CONF="/run/samba/smb.conf"
SMB_TMP="$(mktemp -p /run/samba)"

cp -f $SMB_CONF $SMB_TMP

if [ ! -f /storage/.cache/services/samba.conf ]; then
  cp /usr/share/services/samba.conf /storage/.cache/services
fi

# Specify defaults here, in case these new properties not yet added in .cache
SAMBA_WORKGROUP=WORKGROUP
SAMBA_MINPROTOCOL=SMB2
SAMBA_MAXPROTOCOL=SMB3

. /storage/.cache/services/samba.conf

# fixup synonyms
sed -i 's/browsable/browseable/g; s/writable/writeable/g' $SMB_TMP

# handle external drives
if [ "$SAMBA_AUTOSHARE" == "true" ] ; then
  for dir in /media/* ; do
    if [ -d "$dir" ] ; then
      name=$(basename "$dir")
      echo -e "[$name]\n  path = $dir\n  available = yes\n  browseable = yes\n  public = yes\n  writeable = yes\n" >> $SMB_TMP
    fi
  done
fi

# Allow access to a "failed" (safe mode) Kodi installation
if [ -d /storage/.kodi.FAILED ]; then
  echo -e "[Kodi-Failed]\n  path = /storage/.kodi.FAILED\n  available = yes\n  browseable = yes\n  public = yes\n  writeable = yes\n" >> $SMB_TMP
fi

ADD_CONFIG=

# If workgroup is not set, don't set it - who knows, user may know better.
if [ -n "$SAMBA_WORKGROUP" ]; then
  # Remove any existing workgroup setting
  sed -E '/^[[:space:]]*workgroup[[:space:]]*=/d' -i $SMB_TMP
  ADD_CONFIG="${ADD_CONFIG}  workgroup = ${SAMBA_WORKGROUP:-WORKGROUP}\n"
fi

ADD_CONFIG="${ADD_CONFIG}  server min protocol = ${SAMBA_MINPROTOCOL/SMB1/NT1}\n"
ADD_CONFIG="${ADD_CONFIG}  server max protocol = ${SAMBA_MAXPROTOCOL/SMB1/NT1}\n"

# Add extra config after [global], escaping spaces so that all are retained by sed
sed -e "/\[global\]/ a ${ADD_CONFIG// /\\ }" -i $SMB_TMP

# only letters & numbers permitted for username & password
  SAMBA_USERNAME=`echo $SAMBA_USERNAME | sed "s/[^a-zA-Z0-9]//g;"`
  SAMBA_PASSWORD=`echo $SAMBA_PASSWORD | sed "s/[^a-zA-Z0-9]//g;"`

if [ "$SAMBA_SECURE" == "true" -a ! "$SAMBA_USERNAME" == "" -a ! "$SAMBA_PASSWORD" == "" ] ; then
  # username map: first line makes sure plain root does not work all the time
  # processing continues, so if user chooses root as username, second line overrides the first
  # this is done always in case user uses passwords in userconf.
  # many thanks to viljoviitanen for this
  echo -e "$SAMBA_PASSWORD\n$SAMBA_PASSWORD" | smbpasswd -s -a root >/dev/null 2>&1
  echo -e "nobody = root\nroot = $SAMBA_USERNAME" > /run/samba/samba.map

  sed -e 's|^.[ \t]*.public.=.*|  public = no |' \
      -e 's|^.[ \t]*.username map.=.*||' \
      -e 's|^.[ \t]*.security.=.*|  security = user\n  username map = /run/samba/samba.map|' \
      -e 's|^.[ \t]*.map.to.guest.=.*|  map to guest = Never|' \
      -i $SMB_TMP
else
  sed -e 's|^.[ \t]*.public.=.*|  public = yes |' \
      -e 's|^.[ \t]*.username map.=.*||' \
      -e 's|^.[ \t]*.security.=.*|  security = user|' \
      -e 's|^.[ \t]*.map.to.guest.=.*|  map to guest = Bad User|' \
      -i $SMB_TMP
fi

mv -f $SMB_TMP $SMB_CONF
