## arg 1:  the new package version
#pre_install() {
	# do something here
#}

# arg 1:  the new package version
post_install() {

    #setting the bare installation folder as safe.directory
    git config --system --add safe.directory /srv/git/ethoscope.git

    # Installing python modules in editable mode
    # This creates .egg-info metadata and registers console script entry points
    # --no-build-isolation: Use system setuptools/wheel instead of downloading (no network in chroot)
    echo "Installing Python packages in editable mode..."
    cd /opt/ethoscope/src/node/ && pip install -e . --break-system-packages --no-build-isolation
    cd /opt/ethoscope/src/ethoscope && pip install -e . --break-system-packages --no-build-isolation

    echo "enabling NODE specific systemd service files"
    systemctl enable ethoscope_node.service ethoscope_update_node.service ethoscope_backup_unified.service ethoscope_backup_video.service
    echo "enabling the GIT server on the node and NTP service"
    systemctl enable git-daemon.socket ntpd.service 
    
    #changing hostname on node
    hostnamectl set-hostname node
    
    #setting host settings on node
    echo "127.0.0.1    node" > /etc/hosts

    echo ""
    echo "Installation complete. Console scripts are now available:"
    echo "  - server, backup_tool, make_index, video_backup_tool (from ethoscope_node)"
    echo "  - device_server, device_server_optimized (from ethoscope)"
    echo ""
    echo "PLEASE REBOOT NOW."
}

## arg 1:  the new package version
## arg 2:  the old package version
#pre_upgrade() {
	# do something here
#}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
    #setting the bare installation folder as safe.directory
    git config --system --add safe.directory /srv/git/ethoscope.git

    # Update python packages in editable mode
    echo "Updating Python packages in editable mode..."
    cd /opt/ethoscope/src/node && pip install -e . --break-system-packages --no-build-isolation
    cd /opt/ethoscope/src/ethoscope && pip install -e . --break-system-packages --no-build-isolation
}

# arg 1:  the old package version
pre_remove() {
    echo "disabling systemd service files"
    systemctl stop ethoscope_node.service ethoscope_update_node.service ethoscope_backup_unified.service ethoscope_backup_video.service
    systemctl disable ethoscope_node.service ethoscope_update_node.service ethoscope_backup_unified.service ethoscope_backup_video.service
    systemctl stop git-daemon.socket ntpd.service
    systemctl disable git-daemon.socket ntpd.service

}

# arg 1:  the old package version
post_remove() {
    #removes the git directorie just in case
    rm -rf /opt/ethoscope
    rm -rf /srv/git/ethoscope.git
}

# Note: pip install -e is required to:
# 1. Generate .egg-info metadata for proper package registration
# 2. Create console script entry points (server, backup_tool, device_server, etc.)
# 3. Enable proper import resolution for the ethoscope packages
#
# Dependencies are still managed via pacman; pip only handles package registration.
