#!/bin/sh

PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH

BLOCKSIZE=10240

if [ $# -ne 1 ]; then
  devs=`camcontrol devlist | sed -nEe 's,<(.*)> +at scbus.*\((.*)\,(.*)\),\1|\2 \3,p' | sed -Ee 's,pass[0-9]+,,' | sed -Ee 's,(.*)\| ?(.*),/dev/\2 \\\"\1\\\",' | xargs echo`

  eval dialog --backtitle \"FreeBSD Installer\" --title \"Device selection\" --menu \"Select device to install onto\" 20 50 40 $devs 2>/tmp/devselection
  if [ $? -ne 0 ]; then
    echo
    echo "Aborting installation"
    exit 1
  fi

  DEV=`cat /tmp/devselection`
  rm -f /tmp/devselection
else
  DEV="${1}"
fi

if [ ! -c "${DEV}" ]; then
  echo "${DEV} isn't a device"
  exit 1
fi

set -e

echo "Installing to ${DEV}"
echo
echo "Please enter the fully qualified hostname"
echo "If you just press enter default values for networking will be used"
read hostname

if [ -z "$hostname" ]; then
  hostname="new.local"
  ifconfig="DHCP"
else
  echo "Please enter the ifconfig line (DHCP or a static IP)"
  read ifconfig 

  if [ "$ifconfig" != "DHCP" ]; then
    echo "Please enter the default router"
    read defrouter
    echo "Please enter the DNS search domain"
    read dnssearch
    echo "Please enter the DNS server"
    read dnsserver
  fi
fi

echo "WARNING WARNING WARNING WARNING WARNING WARNING"
echo "Continuing will destroy all data on ${DEV}"
echo "WARNING WARNING WARNING WARNING WARNING WARNING"
echo "Do you wish to continue? (y/n)"
read answer
if [ $answer != "y" ]; then
  echo "Aborting"
  exit 0
fi

TMPDIR=`mktemp -d -q /tmp/installos.XXXXXX`
if [ $? -ne 0 ]; then
  echo "Unable to create temporary directory"
  exit 1
fi

mkdir "${TMPDIR}/mnt"

gpart destroy -F ${DEV}
gpart create -s GPT ${DEV}
gpart add -t freebsd-boot -s 64K ${DEV}
gpart bootcode -b ${SRC}/boot/pmbr -p ${SRC}/boot/gptboot -i 1 ${DEV}
gpart add -t freebsd-ufs -s 1G -l slash ${DEV}
gpart add -t freebsd-swap -s 4G -l swap ${DEV}
gpart add -t freebsd-ufs -s 1G -l var ${DEV}
gpart add -t freebsd-ufs -s 20G -l usr ${DEV}
gpart add -t freebsd-ufs -l local0 ${DEV}

for d in 2 4 5 6; do
    newfs -j "${DEV}p${d}"
done

mount "${DEV}p2" "${TMPDIR}/mnt"
mkdir "${TMPDIR}/mnt/var" "${TMPDIR}/mnt/usr" "${TMPDIR}/mnt/local0"
mount "${DEV}p4" "${TMPDIR}/mnt/var"
mount "${DEV}p5" "${TMPDIR}/mnt/usr"
mount "${DEV}p6" "${TMPDIR}/mnt/local0"

for d in base doc games kernel lib32 src; do
    echo "Extracting $d"
    tar -C "${TMPDIR}/mnt" -pxf /usr/freebsd-dist/${d}.txz
done

echo "Extracting port image"
# Disable exit on error because tar will complain about chflag'd stuff
set +e
cat /image.txz.* | tar -C "${TMPDIR}/mnt" -pUxf -
if [ $? -ne 0 ]; then
  echo "Warning unpack return an error"
fi
set -e

echo "# Device		Mountpoint	FStype	Options	Dump	Pass#" >${TMPDIR}/mnt/etc/fstab
echo '/dev/gpt/slash	/		ufs	rw	1	1' >>${TMPDIR}/mnt/etc/fstab
echo '/dev/gpt/swap	none		swap	sw	0	0' >>${TMPDIR}/mnt/etc/fstab
echo '/dev/gpt/var	/var		ufs	rw	2	2' >>${TMPDIR}/mnt/etc/fstab
echo '/dev/gpt/usr	/usr		ufs	rw	2	2' >>${TMPDIR}/mnt/etc/fstab
echo '/dev/gpt/local0	/local0		ufs	rw	2	2' >>${TMPDIR}/mnt/etc/fstab
echo '/dev/cd0		/cdrom		cd9660	ro,noauto 0	0' >>${TMPDIR}/mnt/etc/fstab
echo 'linprocfs		/compat/linux/proc linprocfs rw	0	0' >>${TMPDIR}/mnt/etc/fstab
echo 'procfs		/proc 		procfs	rw,noauto 0	0' >>${TMPDIR}/mnt/etc/fstab

cat >>${TMPDIR}/mnt/boot/loader.conf <<EOF
ahci_load="YES"
coretemp_load="YES"
pefs_load="YES"
autoboot_delay="2"

vboxdrv_load="YES"
vboxnetadp_enable="YES"
vboxnetflt_enable="YES"

debug.debugger_on_panic="0"

ipmi_load="YES"

#kern.smp.disabled="1"
EOF

cat >>${TMPDIR}/mnt/etc/sysctl.conf <<EOF
vfs.usermount=1
EOF

cat >${TMPDIR}/mnt/etc/rc.conf <<EOF
hostname="$hostname"
keymap="us.iso.kbd"
ifconfig_em0="$ifconfig"
$defrouter

fsck_y_enable="YES"
sshd_enable="YES"
linux_enable="YES"
rcshutdown_timeout="90"
inetd_enable="YES"
dumpdev="AUTO"
smartd_enable="YES"
twdm2_enable="YES"
devfs_system_ruleset="root"
ntpd_enable="YES"
ntpd_sync_on_start="YES"
hald_enable="YES"
dbus_enable="YES"
nut_enable="NO"
nut_upslog_enable="NO"
nut_upslog_format="%TIME @b @d @T% %UPSHOST% upslog[%PID%] load: %VAR ups.load% temp: %VAR ups.temperature% voltage: %VAR input.voltage% recharge: %VAR battery.charge% frequency: %VAR input.frequency% status: %VAR ups.status%"
nut_upslog_logfile="/var/log/upsd.log"
nut_upslog_ups="ups@chef"
nut_upsmon_enable="NO"

postgresql_enable="NO"
postgresql_data="/local0/pgsql/data"

# KDE4
local_startup="\${local_startup} /usr/local/kde4/etc/rc.d"
kdm4_enable="YES"
EOF

cat >>${TMPDIR}/mnt/etc/devfs.rules <<EOF
[root=100]
 
add path 'cd*' group operator mode 660
add path 'pass*' group operator mode 660
add path 'xpt*' group operator mode 660
add path 'uscanner*' group operator mode 660
add path 'ppi*' group operator mode 660
 
add path 'lpt*' group cups mode 660
add path 'ulpt*' group cups mode 660
add path 'unlpt*' group cups mode 660
 
# Post-8.x USB stuff
add path 'usb/*' group operator mode 660
 
add path 'bpf*' group wheel mode 660
EOF

sed -Ee 's,#PermitRootLogin no,PermitRootLogin yes,' -i "" ${TMPDIR}/mnt/etc/ssh/sshd_config

cat >>${TMPDIR}/mnt/etc/csh.cshrc <<EOF
set nobeep
set correct=cmd
set autolist
EOF

if [ "$ifconfig" != "DHCP" ]; then
    cat >${TMPDIR}/mnt/etc/resolv.conf <<EOF
search $dnssearch
nameserver $dnsserver
EOF
fi

cat >>${TMPDIR}/mnt/etc/aliases <<EOF
root:	sysmail@gsoft.com.au
radar:	gs_radar@gsoft.com.au
EOF

mkdir -p "${TMPDIR}/mnt/compat/linux/proc"

echo "Enter root password:"
chroot "${TMPDIR}/mnt" passwd

rm -rf "${TMPDIR}/mnt/tmp" "${TMPDIR}/mnt/var/tmp" "${TMPDIR}/mnt/usr/tmp"
mkdir "${TMPDIR}/mnt/local0/tmp"
chmod 1777 "${TMPDIR}/mnt/local0/tmp"
ln -s "/local0/tmp" "${TMPDIR}/mnt/var/tmp"
ln -s "/local0/tmp" "${TMPDIR}/mnt/usr/tmp"
ln -s "/local0/tmp" "${TMPDIR}/mnt/tmp"

sync

umount "${TMPDIR}/mnt/local0"
umount "${TMPDIR}/mnt/usr"
umount "${TMPDIR}/mnt/var"
umount "${TMPDIR}/mnt"

rm -rf "${TMPDIR}"

echo "Reboot now"
