#!/bin/bash


SIZE=$1

if [ "$(id -u)" != "0" ]; then
	echo This script must be run as root
	echo Use: sudo $0
	echo .
	exit 1
fi

if [ -z "$SIZE" ]; then
	echo .
	echo "Syntax: $0 SIZE (in GB)"
	echo .
	echo E.g. to create a 100GB backup volume use: $0 100
	echo .
	echo Latest version of this script available at www.nervous.it
	exit 1
fi
	
if [ $SIZE -eq $SIZE 2> /dev/null ]; then
	echo About to create a ${SIZE}GB backup volume
	echo .
else
	echo $SIZE is not a number
	echo E.g. to create a 100GB backup volume use: $0 100
	exit 1
fi

VOLS=$(mount|grep smbfs|cut -d ' ' -f 3|sed -e 's/\/Volumes\///g')
echo Currently available shares: $VOLS
echo If the desired Volume does not appear in the list above
echo quit this script and open Finder 
echo then press COMMAND+K to mount additional shares 
echo WARNING: your computer name MUST NOT have spaces or apostrophes in it
echo .
select opt in $VOLS quit; do
  case $opt in
        "quit")
          echo "Exiting."
          break
          ;;
        *)
          echo "You picked $opt"
          break
          ;;
  esac
done
	
if [ "$opt" == "quit" ]; then
	echo Bye.
	exit
fi

DEST="/Volumes/$opt"
if [ ! -d "$DEST" ]; then
	echo Volume $DEST not found. 
	echo Make sure that $DEST is mounted.
	echo .
	exit 1
fi

echo -n Please wait while I collect information regarding your system...
HW_UUID=$(system_profiler |grep 'Hardware UUID'|cut -d ':' -f 2|sed -e 's/ *//g')
echo -n .
MACADDR=$(ifconfig en0|grep ether|cut -d ' ' -f 2|sed -e 's/://g')
NAME=$(uname -n|sed -e 's/\..*$//')
BKPVOL="${NAME}_${MACADDR}.sparsebundle"
TEMP="/tmp"
echo .

echo .
echo I am ready to create the volume $BKPVOL 
echo in the temporary path $TEMP
echo Later we will move this volume to the network shared disk.
echo .
echo WARNING: this script comes with absolutely no warranty. 
echo Press CTRL+C now if you do not want to take any risk or ENTER to continue.
read

echo -n Please wait while I create $TEMP/$BKPVOL ...
hdiutil create -size 100G -fs HFS+J \
	-volname 'Time Machine Backups' \
	-type SPARSEBUNDLE \
	$TEMP/$BKPVOL
echo .

if [ ! -e "$TEMP/$BKPVOL/Info.plist" ]; then
	echo Failed to create volume $TEMP/$BKPVOL
	exit 1
fi

cat >$TEMP/$BKPVOL/com.apple.TimeMachine.MachineID.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>com.apple.backupd.HostUUID</key>
	<string>$HW_UUID</string>
</dict>
</plist>
EOF

if [ ! -e "$TEMP/$BKPVOL/com.apple.TimeMachine.MachineID.plist" ]; then
	echo Failed to create MachineID.plist
	exit 1
fi

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1
touch $DEST/.com.apple.timemachine.supported

echo If there were no errors, press ENTER to move $BKPVOL to destination: $DEST
echo CTRL+C to interrupt.
read 
mv $TEMP/$BKPVOL $DEST/$BKPVOL

echo Now open Time Machine preferences and select the $DEST backup volume
echo .
echo Thanks for using Universal TimeMachine.
echo Latest version of this script available at www.nervous.it
echo .

