#!/bin/bash
function initnssdb() {
	dir=`echo "$1" | cut -f2- -d:`
	if ! test -d "$dir"/rosubdir ; then
		mkdir -m 500 "$dir"/rosubdir
	fi
	if test -d "$dir"/rwsubdir ; then
		chmod u+w "$dir"/rwsubdir/* || true
		rm -f "$dir"/rwsubdir/*
	else
		mkdir -m 700 "$dir"/rwsubdir
	fi
	echo ""   >  "$dir"/oldpin
	echo ""   >  "$dir"/oldpin2
	echo ""   >> "$dir"/oldpin2
	echo "$2" >  "$dir"/newpin
	echo "$2" >  "$dir"/newpin2
	echo "$2" >> "$dir"/newpin2
	certutil -d "$1" -W -f "$dir"/oldpin -@ "$dir"/oldpin2 > /dev/null
	certutil -d "$1" -W -f "$dir"/oldpin -@ "$dir"/newpin2 > /dev/null
	certutil -d "$1" -W -f "$dir"/newpin -@ "$dir"/newpin2 > /dev/null
	certutil -d "$1"/rwsubdir -W -f "$dir"/oldpin -@ "$dir"/oldpin2 > /dev/null
	certutil -d "$1"/rwsubdir -W -f "$dir"/oldpin -@ "$dir"/newpin2 > /dev/null
	certutil -d "$1"/rwsubdir -W -f "$dir"/newpin -@ "$dir"/newpin2 > /dev/null
	chmod u-w "$dir"/rwsubdir/*
}
function run_certutil() {
	dd if=/dev/urandom of="$dir"/noise bs=1024 count=1 > /dev/null 2> /dev/null
	certutil "$@" -z "$dir"/noise
}
function run_dos2unix() {
	dos2unix "$@" 2>&1 | sed -e s,Unix,unix,g -e s,UNIX,unix,g -e s,'format \.\.\.','format...',g
}
function run_unix2dos() {
	unix2dos "$@" 2>&1 | sed -e s,Unix,unix,g -e s,UNIX,unix,g -e s,'format \.\.\.','format...',g
}
