#!/bin/sh
. /usr/lib/news/lib/innshellvars

test -n "$UID" || UID="$(id -ru)"
test "x$UID" = x0 && {
    echo "Please run me as user 'news'!"
    exit 1
}

fresh=
if test X--fresh = "X$1"; then
    fresh=true
    shift
fi

if test -n "$1"; then
    echo "usage: convertspool [--fresh]" 1>&2
    exit 1
fi

cd ${PATHSPOOL} || exit 1
m="moving old spool directory to /var/spool/news/oldspool"
mkdir -p oldspool
for i in *; do
    case $i in
	*.*) ;;
	archive|articles|incoming|innfeed|outgoing|overview|tmp|oldspool) ;;
	*)
	    test -n "$m" && echo "$m"
	    m=
	    mv $i oldspool
	    ;;
    esac
done

m="deleting old directories"
for i in *; do
    case $i in
	tradspool.map) ;;
	*.*)
	    test -n "$m" && echo "$m"
	    m=
	    echo " - $i"
	    rm -rf $i
	    ;;
    esac
done

histhead=`head -n 1 $HISTORY 2>/dev/null`
case X"$histhead" in
X\<*)
    echo "creating new history database"
    test -f $HISTORY.convertspool || mv $HISTORY $HISTORY.convertspool
    :> $HISTORY
    rm -f $HISTORY.dir $HISTORY.pag $HISTORY.index $HISTORY.hash
    if test -f $HISTORY.convertspool ; then
	makedbz -f history -i -s `wc -l <$HISTORY.convertspool`
    else
	makedbz -f history -i
    fi
    ;;
esac

if test -n "$fresh"; then
    rmdir --ignore-fail-on-non-empty oldspool
    test -d oldspool && echo "you can now delete $PATHSPOOL/oldspool"
    echo "done."
    exit 0
fi

#it's not easy to feed innd another config file...
mkdir -p $PATHSPOOL/oldspool/inn.conf
cat >$PATHSPOOL/oldspool/inn.conf/inn.conf <<EOF
xrefslave: true
artcutoff: 0
EOF
cat ${NEWSETC}/inn.conf >> $PATHSPOOL/oldspool/inn.conf/inn.conf
echo "pathbin: $PATHSPOOL/oldspool/inn.conf" > $PATHSPOOL/oldspool/inn.conf/inn.conf2
cat ${NEWSETC}/inn.conf >> $PATHSPOOL/oldspool/inn.conf/inn.conf2
cat > $PATHSPOOL/oldspool/inn.conf/innd <<EOF
#! /bin/sh
export INNCONF=$PATHSPOOL/oldspool/inn.conf/inn.conf
exec $PATHBIN/innd "\$@"
EOF
chmod 755 $PATHSPOOL/oldspool/inn.conf/innd

echo "starting news server"
INNCONF="$PATHSPOOL/oldspool/inn.conf/inn.conf2" INND_BIND_ADDRESS=127.0.0.1 rc.news || {
    echo "Couldn't start server. Fix the problem and run me again!"
    exit 1;
}
sleep 1

mode=`ctlinnd mode`
case $mode in
*running*) ;;
*)
    echo "$mode"
    echo "Server is not running. Fix the problem and run me again!"
    exit 1;
esac

echo "building article list"
cd $PATHSPOOL/oldspool || exit 1
rm -rf spool.batch
mkdir spool.batch
find $PATHSPOOL/oldspool -name spool.batch -prune -o -name inn.conf -prune -o -type f -print > spool.batch/all
cd spool.batch || exit 1
if test -s all ; then
    count=`wc -l <all`
    echo "feeding" $count "articles into the server..."
    n=0
    split -a 5 -l 1000 all
    for b in x*; do
	cp $b y$b
	$PATHBIN/innxmit -s -c 127.0.0.1 $PATHSPOOL/oldspool/spool.batch/y$b || {
	    echo "innxmit error. goodbye."
	    echo "stopping news server"
	    rc.news stop
	    exit 1
      }
      touch y$b
      cat $b y$b | sort | uniq -u > z$b
      cat z$b | xargs rm
      t=`wc -l <z$b`
      count=`expr $count - $t`
      echo "transmitted" $t "articles," $count "to go"
      rm -f $b
      test -s y$b && mv y$b $b
      rm -f y$b z$b
    done
fi
echo "stopping news server"
rc.news stop
echo "deleting empty directories"
cd $PATHSPOOL/oldspool || exit 1
find $PATHSPOOL/oldspool -depth -type d -print | xargs rmdir --ignore-fail-on-non-empty
echo "cleaning up"
rm -rf spool.batch inn.conf
cd $PATHSPOOL
rmdir --ignore-fail-on-non-empty oldspool
echo "done."
