#!/bin/bash

set -u

# 20260714 bkw: Populate the boneyard.

# local paths
OUTDIR=~/boneyard
STATEDIR=~/boneyard-state
TREE=$STATEDIR/slackbuilds
OUTFILE=$OUTDIR/index.html

# git stuff
CLONEURL=git://git.slackbuilds.org/slackbuilds.git
# this is the first commit of the 15.0 series:
START_COMMIT=15.0-20220311.1
SLACKVER="$( echo $START_COMMIT | cut -d- -f1 )"

# sshfs URL for web server
SSHFS_REMOTE=sluk:/home/urchlay/public_html/html/boneyard/
# local sshfs mountpoint
SSHFS_MNTPNT=/home/urchlay/boneyard-remote

# during script development/testing, set this to make
# test runs faster (but very incomplete of course):
#DEBUG_LIMIT="-n 10"
# for production, set this:
DEBUG_LIMIT=""

# global variables:
catprg="" # e.g. "system/someapp"
categ=""  # e.g. "system"
prg=""    # e.g. "someapp"
tarball=""
gitlog=""
rm_log=""
last_log=""
tarcommit=""
descfile=""
desc=""
logfile=""

# sets global variables. sorry, purists.
get_log_lines() {
  catprg="$@"
  prg="$( basename $catprg )"
  categ="$( dirname $catprg )"
  tarball=$OUTDIR/$prg.tar.gz
  logfile=$STATEDIR/$categ-$prg.log

  if [ ! -e "$logfile" ]; then
    git log -n2 --date=format:%Y-%m-%d --pretty=format:"%h%x09%ad%x09%s" -- "$catprg/$prg.SlackBuild" > $logfile
  fi

  gitlog="$( cat $logfile )"

  rm_log="$( echo "$gitlog" | head -n 1 )"
  last_log="$( echo "$gitlog" | tail -n 1 )"
  tarcommit="$( echo $last_log | cut -d' ' -f1 )"
  descfile=$STATEDIR/$categ-$prg.desc

  echo "  $catprg last commit before removal: $tarcommit"
}

mktarball() {
  if [ -e "$tarball" -a -e "$descfile" ]; then
    echo " - tarball and .desc already exist"
    desc="$( cat $descfile )"
  else
    new_tarballs+="$tarball "
    git checkout $tarcommit
    ( cd $categ ; tar cvfz $tarball $prg )
    desc="$( grep "^$prg: " $catprg/slack-desc | head -n 1 | cut -d: -f2 )"
    echo "$desc" > "$descfile"
    git checkout master
  fi
}

p() {
  echo "<p />" >> $OUTFILE.new
}

br() {
  echo "<br />" >> $OUTFILE.new
}

htmlprint() {
  echo "$@" | sed -e 's,<,%lt,g' -e 's,>,&gt,g' >> $OUTFILE.new
}

print_report_line() {
  local tarfile="$( basename $tarball )"
  size="$( stat -c '%s' $tarball )"
  if [ "$size" -ge 1024 ]; then
    size="$(( size / 1024 ))K"
  fi
  echo "<a href=\"$tarfile\">$catprg</a> $size" >> $OUTFILE.new
  br
  htmlprint "$desc" ; br
  htmlprint "$rm_log" ; br
  htmlprint "$last_log" ; br
  p
}

get_all_removed() {
  git log $DEBUG_LIMIT --pretty=oneline --grep="Removed" $START_COMMIT.. | \
    cut -d' ' -f2 | cut -d: -f1 | sort
}

# main()
mkdir -p $OUTDIR $STATEDIR

if [ -d "$TREE" ]; then
  cd $TREE
  git checkout master
  git pull
else
  rm -rf $TREE
  git clone $CLONEURL $TREE
  cd $TREE
fi

removed=$( get_all_removed )
total=$( echo $removed | wc -w )
skipped=0

cat <<EOF >$OUTFILE.new
<html><head><title>SlackBuilds.org Removed Builds</title></head>
<!-- start of run: $( date ) -->
<body>
These are all the builds that have been removed from SlackBuilds.org
since the $SLACKVER repository was started (git tag $START_COMMIT).
<p />
Note that this is purely a personal project of B. Watson
<a href="mailto:urchlay@slackware.uk">&lt;urchlay@slackware.uk&gt;</a>.
It is NOT part of the SlackBuilds.org project or website.
<p />
To search for a build, use your browser's "Search within page" feature
(commonly control-F or F3).
<p />
Some of these builds were removed because they were broken or
nonfunctional (e.g. a client for a service that no longer exists). Others
were removed because they no longer had a maintainer, and got outdated.
If you find anything here that's useful, you can submit it to
SBo under your own name, after fixing any problems with it and/or
version-updating it.
<p />
EOF

count=0
for i in $removed; do
  : $(( count ++ ))
  echo $count/$total: $i
  get_log_lines $i
  if [ -d "$catprg" ]; then
    # If the dir exists, the build got re-added, skip it
    : $(( skipped ++ ))
    echo " -> skipped because $catprg exists"
	 continue
  fi
  mktarball
  print_report_line
done

cat <<EOF >>$OUTFILE.new
<p />
Boneyard populated $( date ) with $(( total - skipped )) builds.
</body></html>
EOF

mv $OUTFILE.new $OUTFILE

if [ ! -e $SSHFS_MNTPNT/.sshfs.mounted ]; then
  mkdir -p $SSHFS_MNTPNT
  sshfs $SSHFS_REMOTE $SSHFS_MNTPNT
  touch $SSHFS_MNTPNT/.sshfs.mounted
fi

# unconditionally copy the index.html
echo
echo "copying index.html"
cp $OUTDIR/index.html $SSHFS_MNTPNT

if [ "$new_tarballs" = "" ]; then
  echo "no newly created tarballs to copy"
  exit 0
fi

echo "copying $( echo $new_tarballs | wc -w ) new tarballs"
cd $OUTDIR
cp -v $new_tarballs $SSHFS_MNTPNT
