#
# thomas@linuxmuster.net
# 20161021
# GPL v3
#

echo "### 40_linbo prepatch"

# stop relevant services if not done yet
service rsync status | grep -q running && service rsync stop
ps ax | grep -v grep | grep -q tftpd && service tftpd-hpa stop
ps ax | grep -v grep | grep -q torrent && ( service linbo-bittorrent stop ; service bittorrent stop )
ps ax | grep -v grep | grep -q mcast && service linbo-multicast stop

# stuff to do on first time install
if [ "$1" = "--first" ]; then

 # patch rsyncd.secrets with admin password
 if grep -q ^linbo: /etc/rsyncd.secrets; then
  sed -e "s/^linbo:.*/linbo:$adminpw/" -i /etc/rsyncd.secrets
 else
  echo "linbo:$adminpw" >> /etc/rsyncd.secrets
 fi

 # provide empty workstations data
 rm -rf $SYSCONFDIR/workstations

fi

touch $SYSCONFDIR/workstations
chmod 600 /etc/rsyncd.secrets

# open internal firewall for multicasting and rsync
. /etc/default/linuxmuster-linbo
if ! grep ^udp $ALLOWEDPORTS | grep -q $PORTBASE:; then
 backup_file $ALLOWEDPORTS
 PORTMAX=$PORTBASE
 let PORTMAX+=100
 PORTS=`grep ^udp $ALLOWEDPORTS | awk '{ print $2 }'`
 PORTS="$PORTS,$PORTBASE:$PORTMAX"
 sed -e "s/^udp.*/udp $PORTS/" -i $ALLOWEDPORTS
fi
if ! grep ^tcp $BASEPORTS | grep -qw rsync; then
 backup_file $BASEPORTS
 PORTS=`grep ^tcp $BASEPORTS | awk '{ print $2 }'`
 PORTS="$PORTS,rsync"
 sed -e "s/^tcp.*/tcp $PORTS/" -i $BASEPORTS
fi

# patch regs with workgroup
for i in `find $LINBODIR/ -type f -name '*.reg'`; do
 backup_file $i
 sed -e "s/^\"DefaultDomainName\"=.*/\"DefaultDomainName\"=\"$workgroup\"/
         s/^\"Workgroup\"=.*/\"Workgroup\"=\"$workgroup\"/" -i $i
done

# patch rsync start variable
backup_file /etc/default/rsync
sed -e "s/^RSYNC_ENABLE=.*/RSYNC_ENABLE=true/" -i /etc/default/rsync

# linbo administrative user
if ! check_id linbo; then
 linbopasswd=`grep ^linbo /etc/rsyncd.secrets | awk -F\: '{ print $2 }'`
 if [ -n "$linbopasswd" ]; then
  echo "Creating linbo user ..."
  if which sophomorix-passwd &> /dev/null; then
   sophomorix-useradd --administrator linbo --unix-group $ADMINGROUP --shell /bin/false --gecos "LINBO Administrator" &> /dev/null || true
   sophomorix-passwd --user linbo --pass $linbopasswd &> /dev/null || true
  else
   smbldap-useradd -a -c "LINBO Administrator" -N LINBO -S Administrator -s /bin/false -g $ADMINGROUP -d $ADMINSHOME/linbo -m linbo
   echo -e "$linbopasswd\n$linbopasswd\n" | smbldap-passwd -s linbo
   mkdir -p $ADMINSHOME/linbo
   chown linbo:$ADMINGROUP $ADMINSHOME/linbo
  fi
  smbldap-usermod -H '[UX         ]' linbo || true
 fi
fi

# update machine account files with basedn
maccts="$(ls "$LINBODIR"/*.macct 2> /dev/null)"
if [ -n "$maccts" ]; then
 for i in $maccts; do
  grep -q "$basedn" $i || ( backup_file "$i" ; sed -e "s|ou=machines,.*|ou=machines,$basedn|" -i "$i" )
 done
fi

# enable bittorrent by default, remove ALLOWED_DIR
if [ -e /etc/default/bittorrent ]; then
 backup_file /etc/default/bittorrent
 sed -e 's|^START_BTTRACK=.*|START_BTTRACK=1|
         s|^ALLOWED_DIR|#ALLOWED_DIR|g' -i /etc/default/bittorrent
fi
[ -e /etc/default/linbo-bittorrent ] && ( backup_file /etc/default/linbo-bittorrent ; sed -e 's|^START_BITTORRENT=.*|START_BITTORRENT=1|' -i /etc/default/linbo-bittorrent )

# start relevant services
for i in tftpd-hpa rsync linbo-bittorrent linbo-multicast; do
 service "$i" start
done
