#!/bin/sh

export PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
UPDATE_TARBALL=update.tar.gz

echo "Update Script started"
pre_vers=`head -n 1 /etc/cabversion`

path=$PWD

# Working directory for passwd, group, and shadow merges
pwdupdir="/tmp/pwdupdir"
mkdir -p ${pwdupdir}
#<PASSWD>
cat <<EOF >>${pwdupdir}/passwd
root:x:0:0:root:/root:/bin/sh
config:x:0:0:Configuration user:/home/config:/bin/sh
www-data:*:33:33:www-data:/var/www:/bin/sh
nobody:*:65534:65534:nobody:/nonexistent:/bin/sh
tss:x:120:120:TPM Daemon:/:/bin/false
dbus:x:1000:1000:DBus messagebus user:/var/run/dbus:/bin/false
frr:x:1001:1001:FRR user priv:/var/run/frr:/bin/false
nslcd:x:1002:1002:nslcd user:/:/bin/false
ftp:x:50:50:Anonymous FTP User:/home/ftp:/bin/false
EOF
#<GROUP>
cat <<EOF >>${pwdupdir}/group
root:*:0:
www-data:*:33:
users:*:100:
nogroup:*:65534:
tss:x:120:
pkcs11:x:121:
frr:x:1001:
nslcd:x:1002:
ftp:x:50:
dbus:x:1000:dbus
frrvty:x:1003:frr
EOF
#<SHADOW>
cat <<EOF >>${pwdupdir}/shadow
root:$6$qNN2nB0CRAxTGZWo$smyZocp5fsjM01DyvP9plPvVcXW2Kk3wYtn56WdVFiHEhcBXUSNIrxoEUqKmw5ZhP0bRXbeAEdDjjKd0OnK0Z1:17120:0:::::
config:$6$IvenxEgq0ILJoO41$pBNLxQSaPxt4qY6IfNpSxsETa6PvDyknCwoamaXFeqe4ySsuJWyQDdmRmQedXtZsy7PpDmwaz7N2mhl5naa0Y0:17120:0:::::
tss:*:::::::
dbus:*:::::::
frr:*:::::::
nslcd:*:::::::
ftp:*:::::::
EOF

# Save password files before extracting the TAR archive
for f in passwd group shadow; do
	cp "/etc/${f}" "${pwdupdir}/${f}.bkp"
done

cd /
## tar will fail immediately if a directory is about to be replaced
## by a file or a symlink. Thus, check here for this problem and
## remove directories before unpacking the tar file.
tar tf ${path}/${UPDATE_TARBALL} | while IFS= read -r line; do
	test -z "${line}" && continue

	case "${line}" in
	*/)
		f="${line%/}"
		if [ -L "${f}" ] || [ -e "${f}" -a ! -d "${f}" ]; then
			echo "  Replacing file or symlink '${f}' with directory"
			rm -f "${f}"
		fi
		;;
	*)
		if [ -d "${line}" -a ! -L "${line}" ]; then
			echo "  Replacing directory '${line}' with file or symlink"
			rm -rf "${line}"
		fi
		;;
	esac
done
tar xzf ${path}/${UPDATE_TARBALL}
sync

#<REMOVE>
rm -f "/etc/raddb/server.default"
rm -f "/etc/raddb/server"
rm -rf "/etc/raddb/"
rm -f "/etc/cron.d/e2scrub_all"
rm -rf "/etc/cron.d/"
rm -f "/etc/e2scrub.conf"
rm -f "/sbin/e2scrub_all"
rm -f "/sbin/e2scrub"
rm -f "/usr/lib/e2fsprogs/e2scrub_all_cron"
rm -rf "/usr/lib/e2fsprogs/"
rm -f "/usr/lib/libexpat.so.1.9.2"
sync

# Unpack tar file again.
# If directories are replaced by a symlink,
# the removes above might delete files.
# Restore them here again.
tar xzf ${path}/${UPDATE_TARBALL}
sync

## Merge password files if they have changed.
## Therefore, determine which entries are new
## or have changed and apply them to the new
## file from the update.
## A changed entry will be dropped if the entry
## is removed by the update.
for file in passwd group shadow; do
	# Variables
	fnew="/etc/${file}"
	fbkp="${pwdupdir}/${file}.bkp"
	fold="${pwdupdir}/${file}"
	fdiff="${pwdupdir}/${file}.diff"
	mode="$(stat -c '%a' ${fnew})"
	fileowner="$(stat -c '%U' "${fnew}")"
	filegroup="$(stat -c '%G' "${fnew}")"

	# Skip if file has not changed
	diff -q ${fnew} ${fbkp} &>/dev/null && continue

	# Removing all exactly matching lines of old and new from the
	# backup file. The result is changed or new entries.
	grep -v -x -F -f ${fnew} -f ${fold} ${fbkp} > ${fdiff}

	# Create a temporary file for the modifications
	tmp="$(mktemp ${pwdupdir}/${file}-XXXXXX)"
	# Apply the changes to the new file
	awk -F: '
	function pr(type, msg) {
		printf( "  %-7s %7s %s\n", "'"${file}"':", type, msg) > "/dev/stderr";
	}
	function FileToMap(file, map,    a, cmd, line, n) {
		# Get entries from file and store them in an array
		# using the first token until ":" as key.
		while ( (getline line < file) > 0 ) {
			n = split(line, a, ":");
			if ( n < 2 )
				continue;
			map[a[1]] = line;
		}
		close(file);
	}
	function append(a, b) {
		if ( a != "" )
			a = a ", ";

		return a b;
	}
	BEGIN {
		# Store file entries in arrays
		# Note that awk passes arrays as reference
		FileToMap("'"${fdiff}"'", dMap);
		FileToMap("'"${fold}"'", oMap);
		FileToMap("'"${fbkp}"'", bMap);
	}
	/^[^:]+:/ {
		# Check if this entry has been changed
		if ( $1 in dMap ) {
			# Yes, use changed entry
			print dMap[$1];
			delete dMap[$1];
			msg["MERGED"] = append(msg["MERGED"], $1);
		} else {
			# No, keep the entry
			if ( !($1 in oMap) && !($1 in bMap) ) {
				# Was neither in old nor in bkp file,
				# so it must be a new entry
				msg["NEW"] = append(msg["NEW"], $1);
			}
			print $0;
		}
		# Remove entries from old map. Eventually, the remaining
		# entries in the old map are the entries that were removed.
		delete oMap[$1];
		next;
	}
	{
		# Other lines, keep them as they are
		print $0;
		next;
	}
	END {
		# Loop over entries that were in the old but not in the
		# new file anymore.
		for ( i in oMap ) {
			if ( i in dMap ) {
				# Entry has been changed but is removed now.
				delete dMap[i];
				msg["DROPPED"] = append(msg["DROPPED"], i);
			} else {
				# Entry was removed during the update.
				msg["REMOVED"] = append(msg["REMOVED"], i);
			}
		}

		# Loop over remaining changed entries
		for ( i in dMap ) {
			# Entries that were neither in old nor new file.
			print dMap[i];
			msg["EXTRA"] = append(msg["EXTRA"], i);
		}

		# Print results
		for ( i in msg )
			pr(i, msg[i]);

	}
	' ${fnew} 2>&1 >${tmp}
	if [ $? -ne 0 ]; then
		echo -e "${file}:\tMERGE FAILED, CHECK PASSWORDS AND GROUPS!!!"
		continue
	fi

	# Update new file but keep the previous file as backup
	mv ${fbkp} ${fnew}-
	mv ${tmp} ${fnew}
	# ${tmp} had mode 600, so change it correctly
	chown "${fileowner}:${filegroup}" ${fnew} ${fnew}-	
	chmod ${mode} ${fnew} ${fnew}-
done

# Clean-up
rm -rf ${pwdupdir}
sync

cd ${path}

## Update Kernel if present
if [ -r /root/apf6-linux.bin ]; then
	echo "Installing new kernel"
	mount -t ext4 /dev/mmcblk2p1 /mnt

	mv /root/apf6-linux.bin /mnt/

	sync
	umount /mnt
fi

## Update Device Trees if present
if ls /root/imx6*.dtb &> /dev/null; then
	echo "Installing new Device Trees"
	mount -t ext4 /dev/mmcblk2p1 /mnt

	for i in /root/imx6*.dtb; do
		if [ -r "$i" ]; then
			echo "  * $(basename "$i")"
			mv $i /mnt/
		fi
	done
	sync
	umount /mnt
fi

## Update Bootloader (u-boot) if present
if [ -r /root/apf6-u-boot.img -a -r /root/apf6-u-boot.spl ]; then
	echo "Installing new bootloader"

	echo 0 > /sys/block/mmcblk2boot0/force_ro

	# Flash SPL
	dd if=/root/apf6-u-boot.spl of=/dev/mmcblk2boot0 bs=512 seek=2 status=none
	# Flash bootloader
	dd if=/root/apf6-u-boot.img of=/dev/mmcblk2boot0 bs=512 seek=138 status=none
	# Erase environment and backup environment
	dd if=/dev/zero of=/dev/mmcblk2boot0 bs=512 seek=2048 count=2048 status=none

	echo 1 > /sys/block/mmcblk2boot0/force_ro

	rm /root/apf6-u-boot.*

	sync
fi
ldconfig

echo "Update finished."


post_vers=`head -n 1 /etc/cabversion`
echo "Updated from ${pre_vers} to ${post_vers}"

echo "The config file /etc/cablynx.conf was untouched."
echo "Please use the new config file in /etc/conf.d/cablynx.factory"
