# migrate file to specify files and/or directories to copy
# from some other "var" to the new "var".
#
# Example entries:
#
#  Copy to a new dir:
#  "${PREVVARDIR}/etc/sysconfig/test       ${VARDIR}/etc/sysconfig/test"  \
#  New dir can have a different location:
#  "${PREVVARDIR}/etc/sysconfig/test       ${VARDIR}/etc/sysconfig/test2"  \
#  Copy the contents of an existing dir:
#  "${PREVVARDIR}/root/test                     ${VARDIR}/root"  \
#  Note that you have to remove the last dir entry to copy this way.
#
#  Not necessary to include the /etc/sysconfig/network directory.  That is
# handled separately.
#
#  Individual file may be specified as shown below.  Every entry except for the last 
# needs a trailing backslash (\).   See 'config.sh' script for similar example.


info "Migrating conf files..."
for LINE in \
  "${PREVVARDIR}/etc/systemd/network/eth0.network     ${VARDIR}/etc/systemd/network/eth0.network" \
  "${PREVVARDIR}/etc/systemd/network/eth1.network     ${VARDIR}/etc/systemd/network/eth1.network" \
  "${PREVVARDIR}/etc/systemd/timesyncd.conf      ${VARDIR}/etc/systemd" \
  "${PREVVARDIR}/etc/afiniti/chassis.conf        ${VARDIR}/etc/afiniti" \
  "${PREVVARDIR}/var/local/lib/menud      ${VARDIR}/var/local/lib" \
  "${PREVVARDIR}/var/local/afiniti        ${VARDIR}/var/local" \
  "${PREVVARDIR}/etc/sysconfig/features    ${VARDIR}/etc/sysconfig" \
  "${PREVVARDIR}/etc/HOSTNAME             ${VARDIR}/etc/HOSTNAME" \
  "${PREVVARDIR}/etc/hostname             ${VARDIR}/etc/hostname" \
  "${PREVVARDIR}/etc/TZ                   ${VARDIR}/etc/TZ" \
  "${PREVVARDIR}/etc/localtime            ${VARDIR}/etc/localtime" \
  "${PREVVARDIR}/etc/hosts                ${VARDIR}/etc/hosts" \
  "${PREVVARDIR}/etc/machine-id           ${VARDIR}/etc/machine-id" \
  "${PREVVARDIR}/etc/services             ${VARDIR}/etc/services" \
  "${PREVVARDIR}/etc/SERIAL               ${VARDIR}/etc/SERIAL" \
  "${PREVVARDIR}/etc/avahi/services       ${VARDIR}/etc/avahi" \
  "${PREVVARDIR}/etc/httpd/conf/stealth.conf   ${VARDIR}/etc/httpd/conf/stealth.conf" \
  "${PREVVARDIR}/etc/httpd/conf/listen.conf   ${VARDIR}/etc/httpd/conf/listen.conf"  \
  "${PREVVARDIR}/etc/passwd                  ${VARDIR}/etc/passwd" \
  "${PREVVARDIR}/etc/hosts.allow          ${VARDIR}/etc/hosts.allow" \
  "${PREVVARDIR}/root                     ${VARDIR}/"
do
  do_copy "${LINE}"
done

### Section for fixing some of the migration  ###
# Some files have settings that need to migrate, but
# have other sections that are updated.
### consider putting this in a common file.

# Intelligently migrate SNMP file.   Old file may exist.
# check to see if comsec is in the file (old)
if [ -e ${PREVVARDIR}/etc/snmp/snmpd.conf ]; then
  info "Migrate fix: snmpd.conf"
  COM2SEC=$(grep -c com2sec ${PREVVARDIR}/etc/snmp/snmpd.conf)
  if [ $COM2SEC -eq 0 ]; then  # not there, migrate.
    do_copy "${PREVVARDIR}/etc/snmp" "${VARDIR}/etc/snmp"
  fi
fi

# Here is where we can migrate passwd file, but add new ID's
# test if ID exists.
# useradd newid.
#if [ "$PRODUCT" == "SoloistHD_Pro" ]; then
#	${RFSDIR}/bin/sed -i -e"s/he7TRuXjJjxfc/IqVk8gwYTCAY2/" ${VARDIR}/etc/passwd
#fi

# Original timesyncd.conf only had "default" (commented out) NTP directive
# so we activate by default so afiniti can manipulated NTP Server
if [ -e "${VARDIR}/etc/systemd/timesyncd.conf" ]; then
  info "Migrate fix: timesyncd.conf"
  #  uncomment NTP= directive
  sed -i 's/^#NTP=/NTP=/' ${VARDIR}/etc/systemd/timesyncd.conf
fi

# If avahi was disabled in previous version, disable in new version
if [ ! -h "${PREVVARDIR}/etc/systemd/system/multi-user.target.wants/avahi-daemon.service" ]; then
  info "Migrate fix: avahi-daemon"
  rm ${VARDIR}/etc/systemd/system/dbus-org.freedesktop.Avahi.service
  rm ${VARDIR}/etc/systemd/system/multi-user.target.wants/avahi-daemon.service
  rm ${VARDIR}/etc/systemd/system/sockets.target.wants/avahi-daemon.socket
fi

