#!/bin/sh

set -- $1

fs=$1
mp=$2
type=$3
options=$4
dump=$5
pass=$6

case $type in
    btrfs)
	options="${options%,subvol=*}"
	#for removing the option subvol,when thats the only option
	#eg: options=="subvol=@", no comma present
	options="${options%subvol=*}"
	mount -t btrfs ${options:+-o "$options"} $fs /target$mp || exit 1
	case $mp in
	    /)
		btrfs subvolume create /target$mp/@
		chmod 755 /target$mp/@
		umount /target$mp
		options="${options:+$options,}subvol=@"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /.snapshots)
		btrfs subvolume create /target$mp/@.snapshots
		chmod 755 /target$mp/@.snapshots
		umount /target$mp
		options="${options:+$options,}subvol=@.snapshots"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /home)
		btrfs subvolume create /target$mp/@home
		chmod 755 /target$mp/@home
		umount /target$mp
		options="${options:+$options,}subvol=@home"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /root)
		btrfs subvolume create /target$mp/@root
		chmod 755 /target$mp/@root
		umount /target$mp
		options="${options:+$options,}subvol=@root"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /tmp)
		btrfs subvolume create /target$mp/@tmp
		chmod 755 /target$mp/@tmp
		umount /target$mp
		options="${options:+$options,}subvol=@tmp"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /srv)
		btrfs subvolume create /target$mp/@srv
		chmod 755 /target$mp/@srv
		umount /target$mp
		options="${options:+$options,}subvol=@srv"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /usr)
		btrfs subvolume create /target$mp/@usr
		chmod 755 /target$mp/@usr
		umount /target$mp
		options="${options:+$options,}subvol=@usr"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /usr/local)
		btrfs subvolume create /target$mp/@usr@local
		chmod 755 /target$mp/@usr@local
		umount /target$mp
		options="${options:+$options,}subvol=@usr@local"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /var)
		btrfs subvolume create /target$mp/@var
		chmod 755 /target$mp/@var
		umount /target$mp
		options="${options:+$options,}subvol=@var"
		mount -t btrfs -o $options $fs /target$mp
		;;
	    /var/log)
		btrfs subvolume create /target$mp/@var@log
		chmod 755 /target$mp/@var@log
		umount /target$mp
		options="${options:+$options,}subvol=@var@log"
		mount -t btrfs -o $options $fs /target$mp
		;;
	esac
	echo "umount /target$mp"
	exit 0
	;;
esac

exit 1
