#!/bin/bash
#
# for installation of OS centos5, fedora7, freebsd, ubuntu
# linux partitioning:
# swap - 2G (twice memory)
# /boot - 120MB
# / - 4G
# /tmp - 4G
# /usr/local - 4G
# /home - 24G
# /var - all the rest
#
#   (bsd -- use default partitioning)
#
# turn off pxe boot
# record mac addrs for dhcp
# eth0: public address dhcp
# eth1: private address dhcp
#
# get these by wget http://songbird.com/sysfiles/sys-init
# 
#1) install /bin/me
#2) add users zero and kent
#3) copy keys for zero and kent and root
#4) set up some environment stuff
#5) run ntsysv to turn off unnecessary stuff (could script via chkconfig)
#6) set up ntp service

if [ -e /etc/redhat-release ] ; then
    INSTALL="/usr/bin/yum install"
    RC_LOCAL="/etc/rc.d/rc.local"
    NTPD_RESTART="/etc/init.d/ntpd restart"
fi

if [ -e /etc/debian_version ] ; then
    INSTALL="/usr/bin/apt-get install"
    RC_LOCAL="/etc/rc.local"
    NTPD_RESTART="/etc/init.d/ntp restart"
fi

[ -e /root/etc ] || mkdir /root/etc

#if /sbin/route -n |grep  '^0.0.0.0 '|grep eth0 >/dev/null ; then
##   default route is set to eth0; shut down other interface
#    /sbin/ifdown eth1
#fi
#if /sbin/route -n |grep  '^0.0.0.0 '|grep eth1 >/dev/null ; then
##   default route is set to eth1; shut down other interface
#    /sbin/ifdown eth0
#fi
## 
if  /bin/hostname |grep songbird  >/dev/null ; then
#    /sbin/route -n |grep '^0.0.0.0 ' >/dev/null || route add default gw 192.168.1.17
#    # set up resolv.conf
#    mv -f /etc/resolv.conf /etc/resolv.conf.dist
#    cat <<EOF >/etc/resolv.conf
#search songbird.com
#nameserver 72.52.113.2
#nameserver 72.52.113.3
#nameserver 67.114.146.49
#nameserver 208.184.79.7
#EOF
    SONGBIRD=/bin/true
fi

echo "songbird is $SONGBIRD"

if [ ! -e /bin/me ] ; then
    cd /bin
    wget http://208.184.79.7/sysfiles/me
    chmod 755 me
fi


if ! grep -q clear /etc/skel/.bash_logout ; then
    perl -pi.bak -e 's/^([^#].*clear)/#$1/;' /etc/skel/.bash_logout
fi

if [ ! -e /home/zero ]; then
    echo "creating zero's home directory"
    /bin/mkdir /home/zero
    /bin/cp /etc/skel/.??* /home/zero
    echo "zero:x:0:0::/home/zero:/bin/bash" >>/etc/passwd
    echo "zero:x:13756:0:99999:7:::" >>/etc/shadow
    pwck
fi

if [ ! -d /home/zero/.ssh ] ; then
    echo "setting up ssh for zero"
    /bin/mkdir /home/zero/.ssh
    cat <<EOF >>/home/zero/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzngktm1oLfBK+B6jzaoBdtZ4Ndpx/aFfvDmsQcfrUtypElmR9cPbP3GqDdcivQwjnePs/DXDTeiqbnLMzkx4+yCqBg0HvkzP/aJTSJEAc+ftAMvnyYAUqP2pIxvBloOwCY7ksOkuxeI8CaGdMSaiH3JcEigeuv8mdtpYc4IQ32M= kent@raven.songbird.com
EOF
    /bin/chmod 700 /home/zero/.ssh
    /bin/chmod 600 /home/zero/.ssh/authorized_keys
fi

if [ ! -e /home/kent ] ; then
    echo "adding 'kent'"
    useradd kent
fi

if [ ! -e /home/kent/.ssh ] ; then
    echo "setting up ssh for 'kent'"
    /bin/mkdir /home/kent/.ssh
    cat <<EOF >>/home/kent/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzngktm1oLfBK+B6jzaoBdtZ4Ndpx/aFfvDmsQcfrUtypElmR9cPbP3GqDdcivQwjnePs/DXDTeiqbnLMzkx4+yCqBg0HvkzP/aJTSJEAc+ftAMvnyYAUqP2pIxvBloOwCY7ksOkuxeI8CaGdMSaiH3JcEigeuv8mdtpYc4IQ32M= kent@raven.songbird.com
EOF
    /bin/chmod 700 /home/kent/.ssh
    /bin/chmod 600 /home/kent/.ssh/authorized_keys
    /bin/chown -R kent.kent /home/kent/.ssh
fi

if [ ! -e /root/.ssh ] ; then
    /bin/mkdir /root/.ssh
    /bin/chmod 700 /root/.ssh
    cat <<EOF >>/root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAzngktm1oLfBK+B6jzaoBdtZ4Ndpx/aFfvDmsQcfrUtypElmR9cPbP3GqDdcivQwjnePs/DXDTeiqbnLMzkx4+yCqBg0HvkzP/aJTSJEAc+ftAMvnyYAUqP2pIxvBloOwCY7ksOkuxeI8CaGdMSaiH3JcEigeuv8mdtpYc4IQ32M= kent@raven.songbird.com
EOF
    /bin/chmod 700 /root/.ssh
    /bin/chmod 600 /root/.ssh/authorized_keys
fi

#
#   set up some environment stuff
#

# /etc/profile

echo 'setting up environment stuff'

if ! grep -q RSYNC_RSH /etc/profile >/dev/null ; then 
    echo 'setting up global /etc/profile'
    cat <<EOF >>/etc/profile
LESS='-iwX';export LESS
RSYNC_RSH=/usr/bin/ssh;export RSYNC_RSH
TMOUT=3600
EOF
fi


# zeros profile

if ! grep -q RSYNC_RSH /home/zero/.bash_profile >/dev/null ; then
    echo 'setting up zeros profile'
    cat <<EOF >>/home/zero/.bash_profile
LESS='-iwX';export LESS
RSYNC_RSH=/usr/bin/ssh;export RSYNC_RSH
EDITOR=/bin/me; export EDITOR
TMOUT=3600
PATH=/sbin:/usr/sbin:/usr/local/sbin:\$PATH
export PATH
EOF
fi

# kent's profile

if ! grep -q RSYNC_RSH /home/kent/.bash_profile >/dev/null ; then 
    setting up kents profile
    cat <<EOF >>/home/kent/.bash_profile
LESS='-iwX';export LESS
RSYNC_RSH=/usr/bin/ssh;export RSYNC_RSH
EDITOR=/bin/me; export EDITOR
TMOUT=3600
PATH=/sbin:/usr/sbin:/usr/local/sbin:\$PATH
export PATH
EOF
fi

if [ -e /var/spool/cron/root ] ; then
    echo setting up cron
    if grep -q "#min hour day-of-month month day-of-week" /var/spool/cron/root >>/dev/null ;then
        cat <<EOF >>/var/spool/cron/root
#min hour day-of-month month day-of-week
EOF
    else
        cat <<EOF >>/var/spool/cron/root
#min hour day-of-month month day-of-week
EOF
    fi
fi   
        

#
#   make sure that control key works.  for this case we just map capslock to 
#   control, since it's not that important to have a capslock
#
echo setting up key mapping
if [ ! -e /etc/init.d/keymap.local ] ; then 
    echo 'defining keymap for console'
    dumpkeys >/etc/init.d/keymap.local
    perl -pi.bak -e 's/keycode  58 = Caps_Lock/keycode  58 = Control/' /etc/init.d/keymap.local
    loadkeys /etc/init.d/keymap.local
    cat <<EOF  >>$RC_LOCAL
/bin/loadkeys /etc/init.d/keymap.local
EOF

fi


#
#   be sure certain basic commands are installed
#
if [ ! -e /usr/sbin/ntpd ]; then 
    $INSTALL ntp
fi
if [ ! -e /usr/bin/nmap ]; then
    $INSTALL nmap
fi
if [ ! -e /usr/bin/lynx ]; then
    $INSTALL lynx
fi


#
#   Only for songbird hosts
#

echo Songbird specific setup
if $SONGBIRD ; then

echo "Doing songbird specific setup"
perl -pi.bak -e 's/^server/#server/' /etc/ntp.conf
perl -pi.bak -e 's/^fudge/#fudge/' /etc/ntp.conf
cat <<EOF >>/etc/ntp.conf
server 72.52.113.2
EOF
ntpdate 72.52.113.2
$NTPD_RESTART


if grep -q -s "ForwardAgent yes" /etc/ssh/ssh_config >/dev/null ;then
    perl -pi.bak -e 's/#   ForwardAgent no/#   ForwardAgent no\nForwardAgent yes/' /etc/ssh/ssh_config
fi

cd /etc/ssh
echo setting up ssh for songbird

if grep -s -q \"Host bywater.songbird.com\" /etc/ssh/ssh_config >>/dev/null
then
    cat <<EOF >>/etc/ssh/ssh_config

Host joy
port 922
Host joy.songbird.com
port 922

Host bywater
port 922
Host bywater.songbird.com
port 922

Host jay
port 922
Host jay.songbird.com
port 922

Host clyde
port 922   
Host clyde.songbird.com
port 922

host sbc
port 922
host sbc.songbird.com
port 922
host sbcp
port 922
host sbcp.songbird.com
port 922

host wren
port 922
host wren.songbird.com
port 922
host wrenp
port 922

host sb6
port 922
host sb6.songbird.com
port 922
host sb6p
port 922

EOF
fi

if [ ! -e /etc/ssh/sshd_config.922 ] ; then
    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.922
    perl -pi.bak -e 's/#Port 22/#Port 22\nPort 922/' /etc/ssh/sshd_config.922
    [ -e /etc/sysconfig/iptables ] && perl -pi.bak -e 's/-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT/-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 922 -j ACCEPT/' /etc/sysconfig/iptables
    [ -e /etc/sysconfig/iptables ] && /etc/init.d/iptables restart
fi

fi 
# fi SONGBIRD
