#!/bin/bash

## Copyright (C) 2020-2021 Aditya Shakya <adi1090x@gmail.com>
## Everyone is permitted to copy and distribute copies of this file under GNU-GPL3

## Run `archcraft-hooks` related commands.

fix_lsb_release() {
    local file=/etc/lsb-release

    if [[ -z "`grep "^DISTRIB_RELEASE=" $file`" ]] ; then
        # add missing DISTRIB_RELEASE=
        echo "DISTRIB_RELEASE=rolling" >> "$file"
    fi
    sed -i "$file" \
        -e 's|^DISTRIB_ID=.*$|DISTRIB_ID=ultimate|' \
        -e 's|^DISTRIB_RELEASE=.*$|DISTRIB_RELEASE=rolling|' \
        -e 's|^DISTRIB_DESCRIPTION=.*$|DISTRIB_DESCRIPTION=\"Ultimate Arch #DISTTYPEUPPER\"|'
}

fix_os_release() {
    local file=/usr/lib/os-release

    sed -i "$file" \
        -e 's|^NAME=.*$|NAME=\"Ultimate Arch #DISTTYPEUPPER\"|' \
        -e 's|^PRETTY_NAME=.*$|PRETTY_NAME=\"Ultimate Arch #DISTTYPEUPPER\"|' \
        -e 's|^ID=.*$|ID=ultimate|' \
        -e 's|^ID_LIKE=.*$|ID_LIKE=arch|' \
        -e 's|^BUILD_ID=.*$|BUILD_ID=rolling|' \
        -e 's|^HOME_URL=.*$|HOME_URL=\"https://ultimateedition.info\"|' \
        -e 's|^DOCUMENTATION_URL=.*$|DOCUMENTATION_URL=\"https://ultimateedition.info\"|' \
        -e 's|^SUPPORT_URL=.*$|SUPPORT_URL=\"https://ultimateedition.info\"|' \
        -e 's|^BUG_REPORT_URL=.*$|BUG_REPORT_URL=\"https://ultimateedition.info\"|' \
        -e 's|^LOGO=.*$|LOGO=uelogo|'

    if [ -z "$(grep "^ID_LIKE=" $file)" ] && [ -n "$(grep "^ID=" $file)" ] ; then
        # add missing ID_LIKE=
        sed -i $file -e '/^ID=/a \ID_LIKE=arch'
    fi

	# fix issue file
    sed -i 's|Arch Linux|Ultimate Arch #DISTTYPEUPPER|g' /etc/issue /usr/share/factory/etc/issue
}

main()
{
    local hookname="$1"

    case "$hookname" in
        os-release)  fix_os_release ;;
        lsb-release) fix_lsb_release ;;
        "")          fix_os_release
                     fix_lsb_release
                     ;;
    esac
}

main "$@"
