#!/bin/sh

if [ -z "$1" ]; then
	echo Usage: cfssh directory
	exit 1
fi

CONFIG_FILE=/etc/cfs.conf

# Config defaults
CRYPT_ROOT=/var/cfs

# Read config file
if test -f $CONFIG_FILE; then . $CONFIG_FILE; fi

if test "$CRYPT_ROOT" = ""; then
	echo "Cannot determine CRYPT_ROOT"
	exit 1
fi

# Attach the directory and change into it
D=.$RANDOM.$RANDOM
if cattach $1 $D; then :; else
	echo "cattach failed"
	exit 1
fi
echo "Directory is $CRYPT_ROOT/$D"
cd $CRYPT_ROOT/$D

# Clear variables
D="                "
CWD=`/bin/pwd`
D=`basename $CWD`
PWD=$CWD
export RANDOM=0

# Set the shell up
exec /bin/sh -c "$SHELL ; cdetach $D"
