# Build recipe for mksh (and lksh).
#
# Copyright (c) 2017-2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=mksh
version=R59c
release=2

# Define a category for the output of the package name
pkgcategory=shells

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=https://www.mirbsd.org/MirOS/dist/mir/mksh/$tarname

description="
The MirBSD Korn Shell.

mksh is the MirBSD enhanced version of the Public Domain Korn shell
(pdksh), a Bourne-compatible shell which is largely similar to the
original AT&T Korn shell; mksh is the only pdksh derivate currently
being actively developed.  It includes bug fixes and feature
improvements, in order to produce a modern, robust shell good for
interactive and especially script use.
"

homepage=https://www.mirbsd.org/mksh.htm
license="The MirOS Licence, ISC, special terms for use the BSD daemon mascot"

# Limit package name to the program name
full_pkgname=$program

# Source documentation
docsdir="${docdir}/${program}"

# Source directory name
srcdir=mksh

build()
{
    # Create package structure
    mkdir -p "${destdir}/etc/skel" \
             "${destdir}/usr/bin"  \
             "${destdir}/${mandir}/man1"

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Create a separate build directory
    mkdir mksh-build lksh-build

    # Build and install mksh

    cd mksh-build
    (
        # Build and ignore tests for now in this version of mksh-r57
        # (../mksh/check.t:xxx-stat-1 needs further work)
        #sh ../Build.sh -r && ./test.sh

        env CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" \
            LDFLAGS="$QILDFLAGS" LDSTATIC=-static \
        sh ../Build.sh -r
    )

    cp -p mksh "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/mksh"

    #cp -p ../${srcdir}/dot.mkshrc "${destdir}/etc/skel/.mkshrc"
    cp -p "${archive}/etc/skel/.mkshrc" "${destdir}/etc/skel/"
    chmod 644 "${destdir}/etc/skel/.mkshrc"

    # To manage (dot new) config file
    touch "${destdir}/etc/skel/.graft-config"

    cd ..

    cp -p mksh.1 "${destdir}/${mandir}/man1/"
    chmod 644 "${destdir}/${mandir}/man1/mksh.1"

    # Build and install lksh (legacy)

    cd lksh-build
    (
        # Build and ignore tests for now in this version of mksh-r57
        # (../mksh/check.t:xxx-stat-1 needs further work)
        #sh ../Build.sh -L -r && ./test.sh

        env CPPFLAGS="$QICPPFLAGS -DMKSH_BINSHPOSIX" \
            CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" LDSTATIC=-static \
        sh ../Build.sh -L -r
    )

    cp -p lksh "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/lksh"

    cd ..

    cp -p lksh.1 "${destdir}/${mandir}/man1/"
    chmod 644 "${destdir}/${mandir}/man1/lksh.1"

    # Make symlink to compose the default shell
    ( cd "${destdir}/usr/bin" && ln -sf lksh sh )

    # Compress manual pages
    if test -d "${destdir}/$mandir"
    then
        lzip -9 "${destdir}/${mandir}"/man?/*
    fi

    # Build and copy documentation
    sh FAQ2HTML.sh
    mkdir -p "${destdir}${docsdir}"
    cp -p FAQ.htm "${destdir}${docsdir}"
}

