#!/bin/bash
#
# Sets the hostname by looking at an eventual clue in the adaptation and,
# failing that, by looking at the Android vendor.

set -e

PREFERRED_HOSTNAME_FILES="/usr/lib/furios/device/preferred-hostname"
ANDROID_VENDOR_PROPS="/vendor/build.prop"

for file in ${PREFERRED_HOSTNAME_FILES}; do
	if [ -e "${file}" ]; then
		new_hostname="$(cat ${file})"
		break
	fi
done

if [ -z "${new_hostname}" ] && [ -e "${ANDROID_VENDOR_PROPS}" ]; then
	new_hostname="$(grep ro.product.vendor.model= ${ANDROID_VENDOR_PROPS} | cut -d'=' -f2)"
fi

[ -n "${new_hostname}" ] || exit 1

old_hostname="$(hostnamectl hostname)"

sed -i "s/${old_hostname}/${new_hostname}/g" /etc/hosts

hostnamectl hostname "${new_hostname}"
