#!/bin/sh -e

##########################################################################
#   Section:
#       8
#
#   Synopsis:
#       auto-install-linux_base [version]
#       
#   Description:
#       .B auto-install-linux_base
#       installs the selected linux_base port on a FreeBSD system and
#       enables Linux kernel compatibility.
#       It simplifies and expedites the setup of a FreeBSD system for
#       directly running Linux binaries.
#
#       Note that FreeBSD's Linux compatibility is not an emulation
#       layer, but a kernel module that provides direct support for Linux
#       system calls where the Linux KBI differs from that of FreeBSD.
#       As a result, there is no performance penalty when running Linux
#       binaries on FreeBSD.  In fact, they may sometimes run faster on
#       FreeBSD than they do on Linux.
#       
#   Arguments:
#       version     Suffix on an existing linux_base port
#       
#   Returns:
#       0 on success, non-zero error codes otherwise
#
#   Examples:
#       auto-install-linux_base rl9
#
#   Files:
#       /etc/fstab
#
#   Environment:
#       PORTSDIR
#
#   See also:
#       linux(4)
#       
#   History:
#   Date        Name        Modification
#   2018-08-27  J Bacon     Begin
#   2024-11-12  Jason Bacon Add man page template and usage
##########################################################################

usage()
{
    printf "Usage:\n\n" >> /dev/stderr
    for base in $(ls -d $PORTSDIR/emulators/linux_base*); do
	printf "$0 ${base##*-}\n" >> /dev/stderr
    done
    exit 1
}


##########################################################################
#   Main
##########################################################################

case $(auto-ostype) in
FreeBSD)
    : ${PORTSDIR:=/usr/ports}
    
    case $# in
    0)
	while [ ! -e /usr/ports/emulators/linux_base-$linux_base ]; do
	    # export AUTO_ASK_TAG_PREFIX=auto-install-linux_base
	    printf "\nPlease enter one of the following linux_base versions:\n\n"
	    for base in $(ls -d $PORTSDIR/emulators/linux_base*); do
		printf "${base##*-}\n" >> /dev/stderr
	    done
	    # linux_base=$(auto-ask auto-install-linux_base )
	    printf "\nWhich linux_base would you like to install? "
	    read linux_base
	done
	;;
    
    1)
	linux_base=$1
	if [ ! -e /usr/ports/emulators/linux_base-$linux_base ]; then
	    printf "No such linux_base: linux_base-$linux_base\n" >> /dev/stderr
	    exit 1
	fi
	;;
    
    *)
	usage
	;;
    
    esac
    
    major=`uname -r | cut -d '.' -f 1`
    
    # Install linux_base.  Can use 64-bit for 10.3 and later.
    major=`uname -r | cut -d . -f 1`
    minor=`uname -r | cut -d . -f 2 | cut -d - -f 1`
    if [ `uname -m` = amd64 ]; then
	linux64=y
    else
	linux64=n
    fi
    pkg delete -f -y linux_base\* || true
    if [ $linux64 = y ]; then
	kldload linux || true
	kldload linux64 || true
    else
	kldload linux || true
    fi
    pkg install -y linux_base-$linux_base
    
    # FIXME: Use auto-set-conf-var
    auto-append-line 'linux_enable="YES"' /etc/rc.conf $0
    
    # Manually mounting Linux special filesystems is no longer needed and
    # will cause problems since:
    # https://github.com/freebsd/freebsd-src/commit/c13f19c0cfa8fe101cf1e7946474a7793c961d9a
    #auto-append-line linprocfs "linprocfs\t/compat/linux/proc\tlinprocfs\trw\t0\t0" /etc/fstab $0
    #if ! df | fgrep -q /compat/linux/proc; then
    #    mount /compat/linux/proc
    #fi
    #auto-append-line /compat/linux/dev/shm "tmpfs\t\t/compat/linux/dev/shm\ttmpfs\trw,mode=1777\t0\t0" /etc/fstab $0
    #if ! df | fgrep /compat/linux/dev/shm; then
    #    mount /compat/linux/dev/shm
    #fi
    #auto-append-line linsysfs "linsysfs\t/compat/linux/sys\tlinsysfs\trw\t0\t0" /etc/fstab $0
    #if ! df | fgrep -q /compat/linux/sys; then
    #    mount /compat/linux/sys
    #fi
    
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
