#!/bin/sh
#
# Script to set up Cnews on a 2.11BSD system.
# (c) 2026, Warren Toomey, GPL3
#

# Ensure that we are running as root
if [ `id -u` -ne 0 ]
then echo "This script must be run as root; exiting"; exit 1
fi

# Check that we have our own hostname
if [ `hostname` = 'pdp11' ]
then echo "You need to set your own hostname in /etc/netstart and also"
     echo "in /etc/hosts. Run /etc/netstart after editing both files".
     echo
     echo "You can also run /tmp/set_hostname to change the hostname."
     exit 1
fi

# Ensure that there is a news user
grep -s '^news:' /etc/passwd
if [ "$?" -ne 0 ]
then echo
     echo "You need to make a news user and news group."
     echo "Use the vipw command and add this to the password file:"
     echo "news:*:67:67::0:0:News System:/usr/lib/news:/bin/csh"
     echo
     echo "Then edit /etc/group and add this line"
     echo "news:*:67:"
     exit 1
fi

# Do the same wih the /etc/group file
grep -s '^news:' /etc/group
if [ "$?" -ne 0 ]
then echo
     echo "You need to make a news group."
     echo "Edit /etc/group and add this line"
     echo "news:*:67:"
     exit 1
fi

# Set the $HOME for the news user
mkdir -p /usr/lib/news; chown news.news /usr/lib/news

# Load the Cnews configuration file from the src area
if [ ! -f /usr/local/src/cnews/conf/config ]
then echo "Panic: there is no /usr/local/src/cnews/conf/config file!"
     exit 1
fi
. /usr/local/src/cnews/conf/config

# Make some directories
for i in $NEWSARTS $NEWSOV $NEWSCTL $NEWSBIN \
	/usr/spool/news/in.coming/bad \
	/usr/spool/news/in.coming \
	/usr/spool/news/out.master
do echo Making directory $i
   mkdir -p $i 2> /dev/null; chown news.news $i
done

# Make the UUCP spool directories
hostname=`hostname`
mkdir /usr/spool/uucp/D.$hostname
mkdir /usr/spool/uucp/D."$hostname"X
chown uucp.daemon /usr/spool/uucp/D.$hostname /usr/spool/uucp/D."$hostname"X

# Ensure the news user can access the source directory
chmod -R ugo+rX /usr/local/src

# Now install Cnews in stages
echo "Doing the Cnews make install as user news"
echo "cd /usr/local/src/cnews; make install" | su news

# Set the setuid and setgid permissions on newsspool
chmod 6755 /etc/news/bin/input/newsspool

echo "Doing the Cnews make setup as user news"
echo "cd /usr/local/src/cnews; make setup" | su news

echo "Doing the Cnews make ui as user root"
(cd /usr/local/src/cnews; make ui)

echo "Doing the Cnews make readpostcheck as user root"
(cd /usr/local/src/cnews; make readpostcheck)

# Add pdp11.test to the active newsgroups
grep -v 'pidp11.test' /etc/news/active > /tmp/$$ &&
echo "pidp11.test 0000000000 00001 y" >> /tmp/$$ &&
cat /tmp/$$ > /etc/news/active

echo "Adding newsgroup directories"
echo "/etc/news/bin/maint/adddirs" | su news

# Set up our name for Cnews
hostname=`hostname`
echo $hostname > /etc/news/mailname
echo $hostname > /etc/news/whoami

# Set up some cron jobs
grep -v news /etc/crontab > /tmp/$$ &&
echo "0-55/5     * * * *      news    /etc/news/bin/input/newsrun" >> /tmp/$$ &&
echo "2-52/5     * * * *      news    /etc/news/bin/batch/sendbatches" >> /tmp/$$ &&
echo "59         0 * * *      news    /etc/news/bin/expire/doexpire" >> /tmp/$$ &&
echo "10         8 * * *      news    /etc/news/bin/maint/newsdaily" >> /tmp/$$ &&
echo "05,35      * * * *      news    /etc/news/bin/maint/newswatch 3000 300 100" >> /tmp/$$ &&
cat /tmp/$$ > /etc/crontab

# Fix up L.cmds
cat >/etc/uucp/L.cmds<<EOF
PATH=/etc/news/bin:/bin:/usr/bin
rmail
rnews
who
uucp
uucico
EOF

# Mark that we get all newsgroups
grep -v ME /etc/news/sys > /tmp/$$ &&
echo "ME:all/all:f:" >> /tmp/$$ &&
cat /tmp/$$ > /etc/news/sys

# Build /etc/news/explist
cat >/etc/news/explist<<EOF
# sample explist file that does no archiving
#
# note that the order of lines in this file is significant

# hold onto history lines 14 days, nobody gets >90 days
/expired/                       x       14      -
/bounds/                        x       0-1-90  -

# keep local groups a while, with some exceptions
sirius.trivia                   x       7       -
sirius.announce                 x       never   -
sirius                          x       30      -

# override later defaults for some groups of note
sci.space.shuttle,rec.birds     x       7       -

# big non-tech unmoderated groups held long enough for a long weekend
sci,rec,talk,soc,misc,alt       u       4       -

# real noise gets thrown away fast
news.groups                     x       2       -
junk,tor.news.stats             x       1.5     -
EOF

exit 0
