#!/bin/sh

# hiki-setup    setup your hiki environment

setup_symlink () {
    target=$1
    echo "make symlink of hiki files to $target."

    [ -d $target ] || mkdir -p $target
    ln -sf `find /usr/share/hiki -mindepth 1 -maxdepth 1 | \
      grep -v '/data$\|/plugin$'` $target/
    mkdir -p $target/plugin
    ln -sf /usr/share/hiki/plugin/* $target/plugin/
}

setup_symlink2 () {
    target=$1
    cd $target
    cp hikiconf.rb /tmp/hikiconf.rb.sed
    sed -e "/^#\$plugin_path/s|^#||;/^\$plugin_path/s|^.*$|\$plugin_path = '$target/plugin'|" /tmp/hikiconf.rb.sed > /tmp/hikiconf.rb
    cp /tmp/hikiconf.rb .

    eval `grep '\$data_path' hikiconf.rb | sed -e "s/[\$ ]//g"`    
    if [ -e $data_path/hikiconf.rb ]; then
      cd $data_path
    fi
    if ! grep -q "\$plugin_path" hikiconf.rb ; then
      echo 'You had better add'
      echo "\$plugin_path = '$target/plugin'"
      echo "  to $data_path/hikiconf.rb"
    fi
}

setup_copy () {
    target=$1

    echo "make copy of hiki files to $target."

    [ -d $target ] || mkdir -p $target
    cp -dRf `find /usr/share/hiki -mindepth 1 -maxdepth 1 | \
      grep -v '/data$'` $target/
}

setup_main () {
    target=$1
    update=$2

    sed -e 's/#Options /Options +/g' -e "s/foo/$USER/g" $target/dot.htaccess > $target/.htaccess

    if [ "$update" = "" ]; then
	done=
	while [ -z "$done" ]; do
	echo "input data_path (ex: /home/$USER/hiki)"
	echo -n ": "
	read datapath
	if [ "$datapath" = "" ]; then
            datapath="/home/$USER/hiki"
	fi

	echo
	echo "input smtp_server (ex: localhost, smtp.hogefuga.net)"
	echo -n ": "
	read smtpserver
	if [ "$smtpserver" = "" ]; then
            smtpserver="localhost"
	fi

	echo
	echo "data_path: $datapath"
	echo "smtp_server: $smtpserver"
	echo -n "Is it ok? [(O)k/(e)dit]"
	read ok
	if [ -z "$ok" -o "$ok" = "o" -o "$ok" = "O" ]; then
		done=y
	fi
	done
	echo
	(
	    cd $target
	    sed -e "/^\$data_path/s|^.*$|\$data_path = '$datapath'|;/^\$smtp_server/s|^.*$|\$smtp_server = '$smtpserver'|" hikiconf.rb.sample > hikiconf.rb
	)

	[ ! -d $datapath ] && mkdir $datapath
	cp -dRf /usr/share/hiki/data/* $datapath
	echo "NOTICE: Please fix permission of $datapath in accordance with httpd configuration (a.k.a suEXEC or not)"
    fi
}

update () {
    done=
    while [ -z "$done" ]; do
    echo "input data_path (ex: /home/$USER/hiki)"
    echo -n ": "
    read datapath
    if [ "$datapath" = "" ]; then
	datapath="/home/$USER/hiki"
    fi
    echo
    echo "data_path: $datapath"
    echo -n "Is it ok? [(O)k/(e)dit]"
    read ok
    if [ -z "$ok" -o "$ok" = "o" -o "$ok" = "O" ]; then
	done=y
    fi
    done
    echo
    (
	cp -f /usr/share/hiki/data/text/TextFormattingRules $datapath/text/
    )

    target=$1
    if [ -h $target/hiki.cgi ]; then
	#symlink
	setup_symlink $target
	setup_symlink2 $target
	return $?
    elif [ -f $target/hiki.cgi ]; then
	setup_copy $target
	return $?
    else
	echo "Update is failure"
	return 1
    fi
}

usage () {
    echo "Usage: $0 (symlink/copy/update) directory"
    echo "ex) $0 copy /home/$USER/public_html/hiki"
}


RETVAL=0

# See how we were called.

if [ "$2" = "" ]; then
    usage
    exit
fi

case "$1" in
    symlink)
        echo "Setup hiki by symbolic link: "
        setup_symlink $2
	setup_main $2
	setup_symlink2 $2
        RETVAL=$?
        ;;
    copy)
        echo "Setup hiki by copy: "
        setup_copy $2
	setup_main $2
        RETVAL=$?
        ;;
    update)
        echo "Update hiki: "
        update $2
        RETVAL=$?
        echo "Please remove cache in \$data_path"
        ;;
    *)
        usage
        exit 1
esac

exit $RETVAL
