File:  [Coherent Logic Development] / freem / scripts / _fmd_common.bash
Revision 1.4: download - view: text, annotated - select for diffs
Mon Apr 7 05:19:15 2025 UTC (8 days, 15 hours ago) by snw
Branches: MAIN
CVS tags: HEAD
Initial work on moving rpm packaging to fmd

#
#   $Id: _fmd_common.bash,v 1.4 2025/04/07 05:19:15 snw Exp $
#    fmd common functions
#
#  
#   Author: Serena Willis <snw@coherent-logic.com>
#    Copyright (C) 2025 Coherent Logic Development LLC
#
#
#   This file is part of FreeM.
#
#   FreeM is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   FreeM is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero Public License for more details.
#
#   You should have received a copy of the GNU Affero Public License
#   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
#
#   $Log: _fmd_common.bash,v $
#   Revision 1.4  2025/04/07 05:19:15  snw
#   Initial work on moving rpm packaging to fmd
#
#   Revision 1.3  2025/04/07 01:23:10  snw
#   Add OS detection for Raspbian
#
#   Revision 1.2  2025/04/07 00:45:52  snw
#   Enhance fmd-pkg-dpkg to auto-generate the debian/changelog file from the FreeM ChangeLog
#
#   Revision 1.1  2025/04/06 03:38:05  snw
#   Prepare for adding packaging functionality to fmd
#
#
# SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
# SPDX-License-Identifier: AGPL-3.0-or-later
#

function _fmd_os_detect()
{
    export _fmd_uname=$(uname -a)
    export _fmd_kernel=$(uname | tr '[:upper:]' '[:lower:]')

    _fmd_can_package=0
    
    case $_fmd_kernel in

        sunos)
            export _fmd_arch=$(isainfo | cut -d" " -f2)
            export _fmd_subarch=$(isainfo | cut -d" " -f1)
            export _fmd_osversion=$(echo "${_fmd_uname}" | cut -d" " -f3)
            
            local distname=$(cat /etc/release | head -1 | awk '{print $1}')
            case $distname in
                Solaris)
                    export _fmd_distribution="solaris"
                    ;;
                Oracle)
                    export _fmd_distribution="solaris"
                    ;;
                OpenIndiana)
                    export _fmd_distribution="openindiana"
                    export _fmd_distfamily="illumos"
                    ;;
                *)
                    export _fmd_distribution="unknown"                   
                    ;;
            esac

            ##
            # account for systems like Oracle Solaris 11
            # that do not have the traditional pkgadd infrastructure
            #
            which pkgtrans &> /dev/null
            if [[ $? == 0 ]]
            then
                export _fmd_can_package=1
                export _fmd_pkg_mechanism="pkgadd"
            else
                export _fmd_can_package=0
            fi            
            ;;

        netbsd)
            export _fmd_arch=$(echo "{_fmd_uname}" | awk '{ print $NF }')
            export _fmd_subarch=${_fmd_arch}
            export _fmd_osversion=$(echo $"{_fmd_uname}" | awk '{ print $3 }')
            export _fmd_distribution="netbsd"
            export _fmd_distfamily="bsd"
            export _fmd_can_package=1
            export _fmd_pkg_mechanism="pkgsrc"
            ;;

        freebsd)
            export _fmd_arch=$(echo "${_fmd_uname}" | awk '{ print $NF }')
            export _fmd_subarch=${_fmd_arch}
            export _fmd_osversion=$(echo "${_fmd_uname}" | awk '{ print $3 }')
            export _fmd_distribution="freebsd"
            export _fmd_distfamily="bsd"
            export _fmd_can_package=0
            ;;        

        openbsd)
            export _fmd_arch=$(echo "${_fmd_uname}" | awk '{ print $NF }')
            export _fmd_subarch=${_fmd_arch}
            export _fmd_osversion=$(echo "${_fmd_uname}" | awk '{ print $3 }')
            export _fmd_distribution="openbsd"
            export _fmd_distfamily="bsd"
            export _fmd_can_package=0
            ;;
        
        osf1)
            export _fmd_arch="alpha"
            export _fmd_subarch="alpha"
            export _fmd_osversion=$(echo "${_fmd_uname}" | awk '{ print $3 }')
            export _fmd_distribution="tru64"
            export _fmd_distfamily="osf1"
            export _fmd_can_package=0
            ;;

        sco_sv)
            export _fmd_arch="i386"
            export _fmd_subarch="i386"
            export _fmd_osversion=$(echo "${_fmd_uname}" | awk '{ print $4 }')
            export _fmd_distribution="sco"
            ;;        
        
        linux)
            export _fmd_subarch=$(echo "${_fmd_uname}" | awk '{ print $(NF - 1) }')
            export _fmd_arch="${_fmd_subarch}"                                  
                                  
            which lsb_release &> /dev/null;
            if [[ $? == 0 ]]
            then
                local distname=$(lsb_release -i | awk '{print $3}' | tr '[:upper:]' '[:lower:]')
                export _fmd_osversion=$(lsb_release -r | awk '{print $2}' | tr '[:upper:]' '[:lower:]')
            else
                local distfound=0
                
                if [[ -f /etc/redhat-release ]]
                then
                    distfound=1
                    export _fmd_osversion=$(cat /etc/redhat-release | cut -d'(' -f1 | awk '{ print $NF }')
                    local distname=$(cat /etc/redhat-release | awk '{ print $1 }' | tr '[:upper]' '[:lower:]')
                fi

                if [[ -f /etc/debian_version ]]
                then
                    ##
                    # note that /etc/debian_version exists on Ubuntu
                    # as well, but Ubuntu always has `lsb_release` AFAIK,
                    # so Ubuntu systems should never reach this block
                    #
                    distfound=1
                    export _fmd_osversion=$(cat /etc/debian_version)
                    local distname="debian"                    
                fi

                if [[ -f /etc/slackware-version ]]
                then
                    distfound=1
                    local distname="slackware"
                fi

                if [[ $distfound == 0 ]]
                then
                    local distname="unknown"
                fi
            fi

            case $distname in
                arch)
                    export _fmd_distribution="arch"
                    export _fmd_distfamily="arch"
                    export _fmd_can_package=0
                    export _fmd_pkg_mechanism="pacman"
                    ;;
                
                debian)
                    export _fmd_distribution="debian"
                    export _fmd_distfamily="debian"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="dpkg"
                    ;;

                raspbian)
                    export _fmd_distribution="raspbian"
                    export _fmd_distfamily="debian"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="dpkg"
                    ;;
                
                ubuntu)
                    export _fmd_distribution="ubuntu"
                    export _fmd_distfamily="debian"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="dpkg"
                    ;;
                
                linuxmint)
                    export _fmd_distribution="mint"
                    export _fmd_distfamily="debian"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="dpkg"
                    ;;
                
                slackware)
                    export _fmd_distribution="slackware"
                    export _fmd_distfamily="slackware"
                    export _fmd_can_package=0
                    export _fmd_pkg_mechanism="txz"
                    ;;

                opensuse)
                    export _fmd_distribution="opensuse"
                    export _fmd_distfamily="suse"
                    export _fmd_can_package=0
                    export _fmd_pkg_mechanism="rpm"
                    ;;
                
                rockylinux)
                    export _fmd_distribution="rocky"
                    export _fmd_distfamily="redhat"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="rpm"
                    ;;                
                
                fedora)
                    export _fmd_distribution="fedora"
                    export _fmd_distfamily="redhat"
                    export _fmd_can_package=1
                    export _fmd_pkg_mechanism="rpm"
                    ;;

                unknown)
                    export _fmd_distribution="unknown"
                    export _fmd_distfamily="unknown"
                    export _fmd_can_package=0                    
                    ;;
                
            esac
            ;;
    esac
    
}

function _fmd_repo_detect()
{
    if [[ ! -f sem.ver ]]
    then
        echo "fmd:  $(pwd) is not a FreeM repository"
        exit 1
    fi

    export _fmd_freem_version=$(cat sem.ver)
}

function _fmd_loadconfig()
{
    export _fmd_cvsuser=$(cat "${_fmd_rc}" | grep "user=" | cut -d= -f2)
    export _fmd_email=$(cat "${_fmd_rc}" | grep "email=" | cut -d= -f2)
    export _fmd_fullname=$(cat "${_fmd_rc}" | grep "fullname=" | cut -d= -f2)    
}

function _fmd_reconfig()
{
    echo
    echo
    echo -n "What is your username on cvs.coherent-logic.com? "
    read _fmd_cvsuser   
    echo -n "What e-mail address do you want to use for FreeM contributions? "
    read _fmd_email
    echo -n "What is your full name? "
    read _fmd_fullname
    echo "user=${_fmd_cvsuser}" > "${_fmd_rc}"
    echo "email=${_fmd_email}" >> "${_fmd_rc}"
    echo "fullname=${_fmd_fullname}" >> "${_fmd_rc}"
    echo
    echo "fmd:  wrote ${_fmd_rc}"
}

function _fmd_chkconfig()
{
    export _fmd_rc="${HOME}/.fmdrc"
    
    if [[ ! -f "${_fmd_rc}" ]]
    then
        echo "fmd:  not configured"
        _fmd_reconfig
    fi

    _fmd_loadconfig
}

function _fmd_next_dpkg()
{
    local URL="https://freem.coherent-logic.com/nextdpkg.cfm?release=${_fmd_freem_version}&arch=${_fmd_arch}"
    local RESULT=$(curl "${URL}" 2>/dev/null)

    echo "${RESULT}"
}

function _fmd_commands()
{
    ls -1 ${_fmd_script_dir}/fmd-* | cut -d'-' -f2 | grep -v "~"
}

SCRIPT_DIR=$(dirname "$0")
export _fmd_script_dir="${SCRIPT_DIR}"

_fmd_chkconfig
_fmd_repo_detect
_fmd_os_detect


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>