#!/bin/sh
#Glorified Install Script
echo "Welcome to the PhearD Installation Script"
echo "This will guide you through the entire installation process"
echo
echo "YOU MUST RUN THIS AS ROOT!"
echo
echo "NOTE: This is the utils/games version of PhearD."
echo "If you wish to use dated dirs format, you should get that package"
echo
echo
echo "Things you need to know:"
echo "  A free group ID"
echo "  The location of your ftp root (this must be created before install)"
echo "  The location of your ftp incoming directory (under your ftp root)"
echo "  Your default information for the site"
echo -n "Are you ready to proceed (y/N)? "
read CONT
if [ "$CONT" = "y" ] ; then
echo "Proceeding with installation"
echo -n "Enter a valid, free group ID (ie 255): "
read GID
echo -n "Enter the directory of your ftp root: "
read FTPROOT
echo -n "Enter the incoming directory of your site (including ftp root): "
read INCOMING

#Start into the goodies

#Set Conf file
echo -n "Enter the name of your site: "
read SITE
echo "sitename $SITE" > /etc/phear.conf
echo "datapath /usr/.phear" >> /etc/phear.conf
echo -n "Enter the maximum number of simultaneous users: "
read MAXUSERS
echo "max_users $MAXUSERS" >> /etc/phear.conf
echo "welcome_msg /usr/.phear/welcome.msg" >> /etc/phear.conf
echo "show_diz 1" >> /etc/phear.conf
echo "dupe_check_days 20" >> /etc/phear.conf
echo "caps_first_letter 1" >> /etc/phear.conf
echo "banner /usr/.phear/banner.msg" >> /etc/phear.conf
echo "incomingdir $INCOMING" >> /etc/phear.conf
echo "requestdir $FTPROOT/Requests" >> /etc/phear.conf

#Create Directory Structure
mkdir /usr/.phear
mkdir /usr/.phear/logs
mkdir /usr/.phear/users
mkdir /usr/.phear/tops
mkdir /usr/.phear/text
mkdir /usr/.phear/pids
mkdir /usr/.phear/dupes
mkdir /usr/.phear/help

#Verify FTP Root
mkdir $FTPROOT 1&2>/dev/null
mkdir $INCOMING 1&2>/dev/null
mkdir $INCOMING/Utils 1&2>/dev/null
mkdir $INCOMING/Games 1&2>/dev/null
mkdir $FTPROOT/Requests 1&2>/dev/null
chmod 655 $FTPROOT
chmod 655 $INCOMING
chmod 777 $INCOMING/Utils $INCOMING/Games
chmod 777 $FTPROOT/Requests
chgrp Phear $FTPROOT $INCOMING $FTPROOT/Requests $INCOMING/Utils $INCOMING/Games

#Move Default Text Files
cp text.defaults/* /usr/.phear/text

#Move Root Template
cp roottempl/* /usr/.phear

#Move Help Files
cp help/* /usr/.phear/help
#Install Binaries
install -m700 in.pheard /usr/sbin
install -m700 daemontools/ftpwho /usr/sbin
install -m700 daemontools/ftpghost /usr/sbin
install -m700 daemontools/stats /usr/sbin
install -m700 daemontools/grouptop /usr/sbin
install -m700 daemontools/useradd-ftp /usr/sbin

echo -n "Enter your login on the site (*Don't enter root): "
read LOGIN

#Create Default User File and Initial User File
echo -n "Enter the default ratio for users: "
read RATIO
echo -n "Enter free credits for user (in k): "
read CREDITS
echo -n "Enter the default max logins per user: "
read MAXPERUSER

touch /usr/.phear/users/default
echo "ratio $RATIO" >> /usr/.phear/users/default
echo "level 1" >> /usr/.phear/users/default
echo "restrict $FTPROOT" >> /usr/.phear/users/default
echo "tagline Set your unfo" >> /usr/.phear/users/default  
echo "num_logins $MAXPERUSER" >> /usr/.phear/users/default  
echo "exempt_from_limit 0" >> /usr/.phear/users/default  
echo "credits $CREDITS" >> /usr/.phear/users/default

echo "ratio 0" >> /usr/.phear/users/$LOGIN
echo "level 20" >> /usr/.phear/users/$LOGIN
echo "restrict $FTPROOT" >> /usr/.phear/users/$LOGIN
echo "tagline Site Admin" >> /usr/.phear/users/$LOGIN
echo "num_logins 5" >> /usr/.phear/users/$LOGIN
echo "exempt_from_limit 1" >> /usr/.phear/users/$LOGIN
echo "ip 127.0.0.1" >> /usr/.phear/users/$LOGIN

#Add Phear Group
echo "Phear::$GID:" >> /etc/group

echo "usr/sbin/useradd-ftp -g $GID -p $1 -c `date +%d/%m/%y` -d /ftpserv -s /etc/ftponly $2" > /usr/.phear/ua

#Touch Customizeable Files
touch /usr/.phear/welcome.msg
touch /usr/.phear/banner.msg
touch /usr/.phear/goodbye.msg

#PhearTag
echo -n "Do you wish to use PhearTag (Y/n): "
read PHEARTAG
if [ "$PHEARTAG" != "n" ] ; then
 echo "/usr/.phear/pheartag/pheartag -q \$1" > /usr/.phear/zipscript
 mkdir /usr/.phear/pheartag
 cp pheartag/* /usr/.phear/pheartag
else
 touch /usr/.phear/zipscript
fi

#Verify Important Modes
chmod 655 /usr/.phear/zip
chmod 655 /usr/.phear/unzip
chmod 655 /usr/.phear/makestats

#Add Sitekill
mkdir /usr/.phear/sitekill
cp maint/* /usr/.phear/sitekill

#Create msgpath file
echo "$INCOMING /usr/.phear/msg.uploads" > /usr/.phear/msgpath
echo "$FTPROOT/Requests /usr/.phear/msgs.requests" >> /usr/.phear/msgpath
echo "$INCOMING/Utils /usr/.phear/msg.utils" >> /usr/.phear/msgpath
echo "$INCOMING/Games /usr/.phear/msg.games" >> /usr/.phear/msgpath

#Create games and utils displays
echo "%!/usr/.phear/text/last5.head" > /usr/.phear/msg.games
echo "%NEW10$INCOMING/Games" >> /usr/.phear/msg.games
echo "%!/usr/.phear/text/last5.foot" >> /usr/.phear/msg.games

echo "%!/usr/.phear/text/last5.head" > /usr/.phear/msg.utils
echo "%NEW10$INCOMING/Utils" >> /usr/.phear/msg.utils
echo "%!/usr/.phear/text/last5.foot" >> /usr/.phear/msg.utils

#Tell them how it is
echo "Installation Completed!"
echo "What's left to do:"
echo " Add this line to your /etc/inetd.conf:"
echo "  pheard  stream  tcp     nowait  root    /usr/sbin/tcpd /usr/sbin/in.pheard"
echo " And add this line to your /etc/services:"
echo "  pheard	$PORT			#PhearD"
echo " Type 'killall -HUP inetd' to restart your services"
echo " Modify any files/displays you wish to change"
echo " Add /usr/.phear/makestats to your crontab"
echo "Note - if you selected to use pheartag, please be sure to edit the configuration files located in /usr/.phear/pheartag"
echo
echo "Now, you should be able to log on to the daemon (using the login you provided)"
echo "Be sure to login from the local machine and then add ips from there."
echo "If you have any questions/complaints, email cris@magiccarpet.com"
 
fi
