#!/bin/bash

# create the user
dscl localhost -create /Local/Default/Users/amanda
dscl localhost -create /Local/Default/Users/amanda RecordName amanda
dscl localhost -create /Local/Default/Users/amanda UserShell /bin/bash
dscl localhost -create /Local/Default/Users/amanda RealName "Amanda backup user" 
dscl localhost -create /Local/Default/Users/amanda UniqueID 5000
dscl localhost -create /Local/Default/Users/amanda PrimaryGroupID 0
dscl localhost -append /Local/Default/Groups/admin GroupMembership amanda
dscl localhost -create /Local/Default/Users/amanda NFSHomeDirectory /Users/amanda

echo "Next passwords are for passwd"
passwd amanda

# create the users home directory
ditto -rsrcFork '/System/Library/User Template/English.lproj/' /Users/amanda
chown -R amanda:wheel /Users/amanda

# set up the .amandahosts file
sh -c "echo 'indexserver.your.domain indexserveramandauser' > /Users/amanda/.amandahosts"
chown amanda:wheel /Users/amanda/.amandahosts
chmod 600 /Users/amanda/.amandahosts

# download amanda, extract and remove tar.gz
wget http://downloads.sourceforge.net/project/amanda/amanda%20-%20stable/2.6.1p2/amanda-2.6.1p2.tar.gz?use_mirror=hivelocity
tar xzf amanda-2.6.1p2.tar.gz
rm amanda-2.6.1p2.tar.gz

cd amanda-2.6.1p2
./configure \
    --prefix=/Users/amanda/amanda \
    --with-user=amanda \
    --with-group=admin \
    --with-bsdtcp-security \
    --without-server \
    --disable-s3-device \
    --without-restore \
    --with-config=daily \
    --with-index-server=indexserver.your.domain
make
make install
cd ..

# create various paths needed by amanda
cd /Users/amanda/amanda
mkdir -p var/amanda/gnutar-lists etc/amanda
touch var/amanda/amandates
chown -R amanda:wheel var/amanda etc/amanda
chmod -R o-rwx var/amanda etc/amanda

# setup launchd
cat > /Library/LaunchDaemons/org.amanda.amandad.bsdudp.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>Label</key>
     <string>org.amanda.amandad.bsdudp</string>
     <key>UserName</key>
     <string>backup</string>
     <key>GroupName</key>
     <string>wheel</string>
     <key>InitGroups</key>
     <true/>
     <key>ProgramArguments</key>
     <array>
          <string>/Users/backup/amanda/libexec/amandad</string>
          <string>-auth=bsdudp amdump amindexd amidxtaped</string>
     </array>
     <key>Sockets</key>
     <dict>  
          <key>ipv4</key>
          <dict>
               <key>SockFamily</key>
               <string>IPv4</string>
               <key>SockServiceName</key>
               <string>amanda</string>
               <key>SockType</key>
               <string>dgram</string>
          </dict>
          <key>ipv6</key>
          <dict>
               <key>SockFamily</key>
               <string>IPv6</string>
               <key>SockServiceName</key>
               <string>amanda</string>
               <key>SockType</key>
               <string>dgram</string>
          </dict>
     </dict>
     <key>inetdCompatibility</key>
     <dict>
          <key>Wait</key>
          <false/>
     </dict>
</dict>
</plist>
EOF

chown root:wheel /Library/LaunchDaemons/org.amanda.amandad.bsdudp.plist
chmod 644 /Library/LaunchDaemons/org.amanda.amandad.bsdudp.plist
launchctl load -w /Library/LaunchDaemons/org.amanda.amandad.bsdudp.plist

