#!/bin/sh
# Test for linux distributions.
set -e

. /usr/share/os-prober/common.sh

partition="$1"
dir="$2"
type="$3"

# This test is inaccurate, but given separate / and /boot partitions and the
# fact that only some architectures have ld-linux.so, I can't see anything
# better. Make sure this test has a high number so that more accurate tests
# can come first.
# TODO: look for ld-linux.so on arches that have it
if ls $dir/lib/ld*.so* >/dev/null 2>/dev/null; then
	if [ -e "$dir/etc/debian_version" ]; then
		short="Debian"
		long=$(printf "Debian GNU/Linux (%s)\n" "$(cat $dir/etc/debian_version)")
	# Mandrake and Fedora may also have a redhat-release, so
	# check their files first.
	elif [ -e "$dir/etc/mandrake-release" ]; then
		short="Mandrake"
		long=$(cat "$dir/etc/mandrake-release")
	elif [ -e "$dir/etc/fedora-release" ]; then
		short="Fedora"
		long=$(cat "$dir/etc/fedora-release")
	elif [ -e "$dir/etc/redhat-release" ]; then
		short="RedHat"
		long=$(cat "$dir/etc/redhat-release")
	elif [ -e "$dir/etc/SuSE-release" ]; then
		short="SuSE"
		long=$(head -1 "$dir/etc/SuSE-release")
	elif [ -e "$dir/etc/gentoo-release" ]; then
		short="Gentoo"
		long=$(cat "$dir/etc/gentoo-release")
	elif [ -e "$dir/etc/cobalt-release" ]; then
		short="Cobalt"
		long=$(cat "$dir/etc/cobalt-release")
	elif [ -e "$dir/etc/yellowdog-release" ]; then
		short="YellowDog"
		long=$(cat "$dir/etc/yellowdog-release")
	elif [ -e "$dir/etc/turbolinux-release" ]; then
		short="Turbolinux"
		long=$(cat "$dir/etc/turbolinux-release")
	elif [ -e "$dir/etc/slackware-version" ]; then
		short="Slackware"
		long=$(printf "Slackware Linux (%s)\n" "$(cat $dir/etc/slackware-version)")
	elif [ -e "$dir/sbin/pkgtool" ]; then
		short="Slackware"
		long="Slackware Linux"
	elif grep -q OpenLinux "$dir/etc/issue"; then
		short="Caldera"
		long="Caldera OpenLinux"
	else
		short="Linux"
		long="unknown Linux distribution"
	fi
	
        label=$(count_next_label $short)
	result "$partition:$long:$label:linux"
	exit 0
else
	exit 1
fi
