#!/bin/sh

# Installs a dev copy of live build
# Requires running with sudo
# Can also remove the installed version with 'remove' command which may help clean things up

set -e

RESET="\033[0m"
RED="\033[1;31m"

Exit_exit ()
{
	local VALUE=$?
	if [ "${VALUE}" -ne 0 ]; then
		echo "${RED}Error${RESET}: An unexpected failure occurred, exiting..." >&2
	fi
	return ${VALUE}
}

Exit_other ()
{
	local VALUE=$?
	echo "${RED}Error${RESET}: Unexpected early exit caught, attempting cleanup..." >&2
	return ${VALUE}
}

trap 'Exit_other' HUP INT QUIT TERM
trap 'Exit_exit' EXIT

# Checking user account
if [ "$(id -u)" -ne "0" ]; then
	echo "${RED}Error${RESET}: Root privileges needed!"
	exit 1
fi

cd live-build

MODE=${1:-install}

echo "removing existing..."

rm -f /usr/bin/lb
rm -f /usr/bin/live-build
rm -f /usr/lib/live/build.sh
rm -f /usr/share/live/build/VERSION
rm -rf /usr/lib/live/build/*
rm -rf /usr/share/live/build/bootloaders/*
rm -rf /usr/share/live/build/functions/*
rm -rf /usr/share/live/build/hooks/*
rm -rf /usr/share/live/build/bin/*
rm -rf /usr/share/live/build/data/*
rm -rf /usr/share/doc/live-build/examples/*

rm -f /usr/share/man/man7/live-build.7.gz
rm -f /usr/share/man/man1/lb.1.gz
rm -f /usr/share/man/man1/lb_*.1.gz

# if remove mode, exit now
if [ "${MODE}" = "remove" ]; then
    echo "reinstalling official package..."

    apt-get reinstall live-build -y

    echo "done."

    exit 0
fi

echo "installing new..."

echo "custom" > /usr/share/live/build/VERSION

echo "copying files..."

cp -a frontend/lb /usr/bin/
cp -a frontend/live-build /usr/bin/
cp -a scripts/build.sh /usr/lib/live/
cp -ra scripts/build/* /usr/lib/live/build/
cp -ra functions/* /usr/share/live/build/functions/
cp -ra share/hooks /usr/share/live/build/
cp -ra share/bin /usr/share/live/build/
cp -ra share/bootloaders /usr/share/live/build/
cp -ra data /usr/share/live/build/
cp -ra examples /usr/share/doc/live-build/

echo "fixing ownership..."

chown root:root /usr/bin/lb
chown root:root /usr/bin/live-build
chown root:root /usr/lib/live/build.sh
chown root:root /usr/share/live/build/VERSION

chown -Rh root:root /usr/lib/live/build/*
chown -Rh root:root /usr/share/live/build/functions
chown -Rh root:root /usr/share/live/build/hooks
chown -Rh root:root /usr/share/live/build/bin
chown -Rh root:root /usr/share/live/build/bootloaders
chown -Rh root:root /usr/share/live/build/data
chown -Rh root:root /usr/share/doc/live-build/examples

#echo "fixing chmod perms..."
#
#chmod 755 /usr/bin/lb
#chmod 755 /usr/bin/live-build
#chmod 755 /usr/lib/live/build.sh
#chmod 644 /usr/share/live/build/VERSION
#
## directories
#find /usr/lib/live/build/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/functions/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/hooks/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/bin/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/bootloaders/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/data/ -type d -name '*' -exec chmod 755 {} +
#find /usr/share/doc/live-build/examples/ -type d -name '*' -exec chmod 755 {} +
#
## files
#find /usr/lib/live/build/ -type f -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/functions/ -type f -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/hooks/ -type f -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/bin/ -type f -name '*' -exec chmod 755 {} +
#find /usr/share/live/build/bootloaders/ -type f -name '*' -exec chmod 644 {} +
#find /usr/share/live/build/bootloaders/ -type f -name '*.sh' -exec chmod 755 {} +
#find /usr/share/live/build/data/ -type f -name '*' -exec chmod 644 {} +
#find /usr/share/doc/live-build/examples/ -type f -name '*' -exec chmod 644 {} +
#find /usr/share/doc/live-build/examples/auto/ -type f -name '*' -exec chmod 755 {} +
#find /usr/share/doc/live-build/examples/hooks/ -type f -name '*' -exec chmod 755 {} +

echo "regenerating man pages..."

gzip -ckN manpages/en/live-build.7 >> /usr/share/man/man7/live-build.7.gz
for FILE in manpages/en/*.1; do
    FILENAME="$(basename "${FILE}")"
    gzip -ckN "${FILE}" >> "/usr/share/man/man1/${FILENAME}.gz"
done
mandb -q

echo "done."
