#!/bin/bash

# Slackware build script for typescript

# Copyright 2025-2026 Vijay Marcel
# All rights reserved.
#
# This Script is  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.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=typescript
VERSION=${VERSION:-6.0.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
SRCNAM=${SRCNAM:-TypeScript}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
else
 echo "This slackbuild will not run on $ARCH" && exit 1
fi

set -e
trap 'echo "$0 FAILED at line $LINENO!" | tee -a $OUTPUT/error-${PRGNAM}.log' ERR

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \+

mkdir -pv $PKG/usr/bin
mkdir -pv $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/$PRGNAM

# make npm to download the deps to $TMP/$SRCNAM-$VERSION/cache

mkdir $TMP/$SRCNAM-$VERSION/cache
mkdir $TMP/$SRCNAM-$VERSION/node-compile-cache
export npm_config_cache=$TMP/$SRCNAM-$VERSION/cache
export NODE_COMPILE_CACHE=$TMP/$SRCNAM-$VERSION/node-compile-cache

npm ci
npx hereby LKG

cd built/local
find . \( -name "*.js.map" -o -name "*.d.ts.map" \) -exec rm -vf {} \;
find . \( -name "*.tsbuildinfo" \) -exec rm -vf {} \;

cd ..

cp -av local/* $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/$PRGNAM

cat > tsc << EOF
#!/usr/bin/env node
require("/usr/lib${LIBDIRSUFFIX}/node_modules/$PRGNAM/tsc.js")
EOF

cat > tsserver << EOF
#!/usr/bin/env node
require("/usr/lib${LIBDIRSUFFIX}/node_modules/$PRGNAM/tsserver.js")
EOF

install -Dvm 0755 tsc -t $PKG/usr/bin
install -Dvm 0755 tsserver -t $PKG/usr/bin

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded --remove-section=.comment --remove-section=.note 2> /dev/null || true

cd ..
mkdir -pv $PKG/usr/doc/$PRGNAM-$VERSION
cp -av LICENSE.txt README.md ThirdPartyNoticeText.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n --remove-rpaths --remove-tmp-rpaths $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE

