# boot_migrate
# MPG
#
# general purpose migrate file.  
# gets source by /init.d/boot.local once NFCMS complete and before runlevels start.
# boot.local deletes this file once complete

HD0=/media/hd0
FOLDER_LIST="rdy media dvc dvc list osd shd schmi mvl"
DPI_DOLDER_LIST="rdy schins verins"
SYS_FOLDER_LIST="log .meta"

OSD_LIST="jpeg jpg bmp png"
LIST_TYPE=smil
DVC_TYPE=dvc
MLF_MVL="mvl mlf"


if [ ! -e $HD0 ]; then   # large banner, no logger?
   logger -s -t "[MIGRATE]" -p user.warn "WARNING: No mounted drive found!"
   logger -s -t "[MIGRATE]" -p user.warn "WARNING: No mounted drive found!"
   logger -s -t "[MIGRATE]" -p user.warn "WARNING: No mounted drive found!"
   logger -s -t "[MIGRATE]" -p user.warn "WARNING: No mounted drive found!"
   exit 1
fi

# make folders if not exist
for FOLD in $FOLDER_LIST; do
  if [ ! -e ${HD0}/${FOLD} ]; then
    mkdir ${HD0}/${FOLD}
  fi
  chown adtec:users ${HD0}/${FOLD}
  chmod g+s ${HD0}/${FOLD}
done
logger -s -t "[MIGRATE]" -p user.info "Checked main folders in ${HD0}"

# make system folders
for FOLD in $SYS_FOLDER_LIST; do
  if [ ! -e ${HD0}/${FOLD} ]; then
    mkdir ${HD0}/${FOLD}
    chown root:root ${HD0}/${FOLD}
  	 chmod 755 ${HD0}/${FOLD}
    logger -s -t "[MIGRATE]" -p user.info "Checked system folders in ${HD0}"
  fi
done

# make dpi folders
. etc/env.global
if [ "$PRODUCT" == "dpi" ]; then
	for FOLD in $DPI_FOLDER_LIST; do
	  if [ ! -d ${HD0}/${FOLD} ]; then
	     mkdir ${HD0}/${FOLD}
	  fi
	  chown adtec:users ${HD0}/${FOLD}
	  chmod g+s ${HD0}/${FOLD}
	done
	logger -s -t "[MIGRATE]" -p user.info "Created dpi folders in ${HD0}" 
fi


# update services file for syslog to use port 1514
# we put this here since services got migrated and an update has to be after it here.
# MPG - commenting since this will overwrite what was migrated.  Installers have to manually
# set the proper port.  We could test out for an unmodified file (setting at 514), but if 
# someone wants to use 514 we would be overwriting it each upgrade.
#/bin/sed -i "s/\(^syslog[[:space:]]\{1,\}\)[0-9]*\(\/udp$\)/\11514\2/" /etc/services

# get the HIP and set the BARK server.  After this, mirror will maintain it
BARK_SRV=$(/usr/local/bin/mirror HOSTIPADDRESS)
/usr/local/bin/bark -h $BARK_SRV

# update syslog.conf with BARK SRV
COUNT=$(grep -c daemon.notice /etc/syslog.conf)  # get count of daemon.notice
if [ $COUNT -eq 0 ]; then
   sed -i -e 's/\([.*]\)\( *-\/var\/log\/all\)/\1;daemon.!notice\2/' /etc/syslog.conf
   sed -i -e 's/\(local1.notice\)\(.*\)/\1;daemon.notice\2/' /etc/syslog.conf
fi

# add the error pipe
COUNT=$(grep -c '/var/log/error' /etc/syslog.conf)  # get count of var/log/error
if [ $COUNT -eq 0 ]; then
   echo -e "\nlocal0.error          |/var/log/error" >> /etc/syslog.conf
fi
# fix existing syslog.conf files that had daemon.error to local0.error
sed -i -e 's/daemon.error/local0.error/' /etc/syslog.conf

#fix changing adtec processes from daemon to local0. MID 2552
sed -i -e 's/daemon/local0/' /etc/syslog.conf

# setup avahi static services file
if [ ! -d /etc/avahi/services ]; then 
	mkdir -p /etc/avahi/services
fi 
if [ ! -e /etc/avahi/services/adtec.service ]; then 
   HW=$(/sbin/ifconfig eth0 | /bin/grep -o "..:..:..:..:..:.." | /usr/bin/tr -d : | /usr/bin/cut -c 7-12)
   HN=$(cat /etc/HOSTNAME)
   VN=$(basename $(vn -t))
	printf "<service-group>\n<name>%s</name>\n<service>\n<type>_http._tcp</type>\n<port>80</port>\n</service>\n" $HN > /etc/avahi/services/adtec.service
	printf "<service>\n<type>_adtec._tcp</type>\n<port>23</port>\n<txt-record>product=%s</txt-record>\n<txt-record>serial=%s</txt-record>\n" $PRODUCT $HW >> /etc/avahi/services/adtec.service
	printf "<txt-record>version=%s</txt-record>\n</service>\n</service-group>\n" $VN >> /etc/avahi/services/adtec.service
fi