#!/bin/rc

#
#	Webm is a file management tool via Pegasus.
#	Typical usage is
#	*/index.html	text/html	1	/bin/webm -r /~alice $target
#	in /usr/alice/web/etc/handler
#


root=/
if(~ $1 -r){
	root=$2/	# top page
	shift 2
}

TITLE=webm

#
#	general function definitions
#

fn xcp {
	switch($code){
	case no
		cp $1 $2
	case *
		tcs -f $code $1 | tr -d '\xd' >$2
	}
}

fn list {
	ls -l |\
	hfmt '%s %s %s %10s %s %2s %7s <a href="%U">%H</a>\n' 1 4 5 6 7 8 9 10 10
}


#
#	do_xxx
#

# ------ do_index ------
fn do_index {
	echo $prolog
	echo  '
	<h2>Directory Contents</h2>
	<pre>'
	pwd
	echo '</pre>Mouse click a file name to get.
	<pre>'
	ls -l |\
	hfmt '%s %s %s %10s %s %2s %s <a href="%U">%H</a>\n' 1 4 5 6 7 8 9 10 10
	echo '</pre>'
	w=`{pwd}
	if(! ~ $w /doc)
		echo '<a href="../">Return to parent directory</a><br>'
	echo '
	<a href="index.html?mkdir"> Make a directory</a><br>
	<a href="index.html?delete"> Delete files and/or directories</a><br>
	<a href="index.html?edit"> Edit a file</a><br>
	<a href="index.html?send"> Send a file</a><br>
	'
	echo $epilog
}

# ------ do_mkdir ------
fn do_mkdir {
	echo $prolog
	echo '
	<h2>Make a directory</h2>
	<pre>'
	list
	echo '
	</pre>
	<form method="POST" action="index.html?mkdir_action">
	directory name:  <input type="text" name="dirname">
	<input type="reset" value="reset"> <input type="submit" value="submit">
	</form>
	<a href="./">Return to index</a><br>
	'
	echo $epilog
}

# ------ do_mkdir_action ------
fn do_mkdir_action {
	qsparse
	dirname=`{cat /env/QS_dirname}
	mkdir $dirname
	exec webm index.html
}

# ------ do_send ------
fn do_send {
	echo $prolog
	echo '
	<h2>File transmission</h2>
	<pre>'
	list
	echo '</pre>'
	echo '
	<form enctype="multipart/form-data" method="POST" action="index.html?send_action"><br>
	<input type="radio" name="code" value="no" checked> send as it is<br>
	<input type="radio" name="code" value="ms-kanji"> SJIS to UTF-8<br>
	<input type="radio" name="code" value="ujis"> EUC to UTF-8<br>
	<input type="radio" name="code" value="jis-kanji"> JIS to UTF-8<br>
	Rename if you hope <input type="text" name="name" value=""><br>
	File to send. Please brows. <input type="file" name="file" size="60"><br>
	<input type="reset" value="RESET"><input type="submit" value="OK">
	</form>
	<a href="./">Return to index</a><br>
	'
	echo $epilog
}

# ------ do_send_action ------
fn do_send_action {
	echo $prolog
	echo '
	<h2>Result Information</h2>
	We have received your file.<br>
	The result:
	<pre>'
	
	formparse  -o /tmp
	if(~ $status ?*) echo $status
	code=`{cat /tmp/code/0}
	name=`{cat /tmp/name/0}
	file=`{ls -p /tmp/file/*}
	if(! ~ $name ?*)
		name=$file
	
	t=`{cleanname $name}
	if(~ $t */*)
		echo $t': Illegal name. Not accepted'
	if not
		xcp /tmp/file/$file $t
	ls -l
	echo '</pre>
	<a href="./">Return to index</a><br>
	'$epilog
}

# ------ do_edit ------
fn do_edit {
	filename=''
	if(test -e /env/QS_filename)
		filename=`{cat /env/QS_filename}
	filename=$"filename
	echo '
	<html>
	<head>
	<title>Edit</title>
	</head>
	<frameset rows="30%,70%">
	<frame name=topframe src="index.html?edit_topframe" scrolling=auto>'
	if(~ $filename ?*)
		hfmt -a '<frame name=botframe src="index.html?edit_botframe&filename=%U" scrolling="auto">\n' $filename
	if not
		echo '<frame name="botframe" src="index.html?edit_botframe" scrolling="auto">'
	
	echo '</frameset>
	</html>
	'
}

# ------ do_edit_topframe ------
fn do_edit_topframe {
	echo $prolog
	echo '
	<h2>Files to Edit</h2>
	Click the file to edit.
	<pre>'
	ls -l |\
	hfmt '%s %s %s %10s %s %2s %7s <a href="index.html?edit_botframe&filename=%U" scrolling="auto" target="botframe">%H</a>\n' 1 4 5 6 7 8 9 10 10
	echo '</pre>
	<a href="./" target="_parent">Return to Index</a>'
	echo $epilog
}

# ------ do_edit_botframe ------
fn do_edit_botframe {
	echo $prolog
	echo '<h2>Edit</h2>
	<form method="POST" action="index.html?edit_post"> '
	filename=''
	if(test -e /env/QS_filename)
		filename=`{cat /env/QS_filename}
	filename=$"filename
	# sanity check for the file name
	if(~ $filename */*){
		echo $filename': Illegal name. Not accepted'
		filename=''
	}
	if(test -d $filename){
		echo 'we cannot edit directory'
		filename=''
	}
	echo 'File name:  <input type="text" name="filename" value="'$filename'">'
	# I would like to expand textarea width as much as possible,
	# but I don't know how to do.
	echo '<p>
	<textarea name="text" rows=30 cols=80>'
	# Be careful not to add extra lines
	if(~ $filename ?*) cat $filename
	echo '</textarea>
	<p>
	<input type="reset" value="reset"> 	<input type="submit" name="action" value="save">
	</form>
	<a href="./" target="_parent">Return to Index</a>'
	echo $epilog
}

# ------ do_edit_post ------
fn do_edit_post {
	echo $prolog
	echo '<h2> Result Information</h2>
	<pre>'
	formparse -o /tmp; err=0
	if(~ $status ?*) echo $status
	filename=`{cat /tmp/filename/0}
	filename=$"filename   # guard for a filename with space
	action=`{cat /tmp/action/0}
	if(! ~ $filename ?*) {
		echo 'no filename</body></html>'
		exit
	}
	if(~ $filename */* ){
		echo 'wrong file name</body></html>'
		exit
	}
	if(test -d $filename){
		echo 'we cannot edit directory</body></html>'
		exit
	}
	if(~ $action save) cp /tmp/text/0 $filename
	if(~ $status ?*) echo $status
	if not {echo done}
	echo '</pre>'
	echo '<a href="index.html?edit_topframe" target="topframe">Renew the list</a><br>'
	echo '<a href="./" target="_parent">Return to Index</a><br>'
	echo $epilog
}

# ------ do_delete ------
fn do_delete {
	echo $prolog
	echo '<h2> Delete fiiles</h2>
	check marks to delete<br>
	<form method="POST" action="index.html?delete_action"><pre>'
	ls -l |\
	hfmt '%s %s %s %10s %s %2s %7s <input type="checkbox" name="file" value="%U">%H</a>\n' 1 4 5 6 7 8 9 10 10
	echo '<input type="reset" value="reset"> 	<input type="submit" name="action" value="delete">'
	echo '</pre></form>'
	echo '<a href="./">Return to Index</a>'
	echo $epilog
}

# ------ do_delete_action ------
fn do_delete_action {
	echo $prolog
	echo '<h2>Result Information</h2>'
	echo '<pre>'
	formparse -o/tmp
	#ls -l /env/QS_*
	#ls -l /tmp/file
	for(f in /tmp/file/*){
		if(! ~ $f /tmp/file/clone){
			n=`{cat $f}
			switch($n){
			case */*
				echo $n': Illegal name. Not accepted'
			case ?*
				rm -rf $n
			case *
				echo 'No name. Not accepted'
			}
		}
	}
	list
	echo '</pre>'
	echo '<a href="./">Return to Index</a>'
	echo $epilog
}

#
#	main
#

prolog='
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>'$TITLE'</title>
</head>
<body>
<h1>'$TITLE'</h1>'

epilog='
<hr>
<a href="'$root'" target="_parent">Return to top page</a><br>
</body>
</html>
'

a=$"QS_

switch($a){
case mkdir
	do_mkdir
case mkdir_action
	do_mkdir_action
case send
	do_send
case send_action
	do_send_action
case edit
	do_edit
case edit_topframe
	do_edit_topframe
case edit_botframe
	do_edit_botframe
case edit_post
	do_edit_post
case delete
	do_delete
case delete_action
	do_delete_action
case *
	if(~ $request */)
		do_index
	if not{
		if(test -e $1)
			cat $1
		if not
			do_index
	}
}
