#!/usr/local/bin/perl
#  createdir.pl
#   &check_path(UPcol_name, create_flag)
#      checks to see if the UPcol_name collection exists, and if
#      the current user has write permission here.
#      if create_flag is true and user has write permission in closest existing
#      parent, check_path will create any missing components: 
#          for intermediate directories: forms
#          for the final directory: forms, mdata/hi-res, tags, images
#   returns 0 if the directory exists and the user has write permission 
#         ( evrything OK )
#   three different types of errors :
#   - returns 1 if the collection does not exist and the user does not 
#         want to create it..
#   - returns 2 if the creation of the collection or of missing components 
#         has failed because of a system error.
#   - returns 3 if the user is not allowed to edit or create this collection.
#   
#   Called by: edform, addimages, create_tags

require 'definitions.pl';
require 'wrhtaccess.pl';

$makeindex = "$URLrootdir$SCRIPTdir/makeindex";

sub check_path{
    local ($UPcol_name, $create) = @_;
    local( $curdir, $formsdir, @dirs, $parentdir, $acc_ok );

    # make files created by httpd group writeable 
    if ($create ) { system(umask 2); }

    @dirs = split(/\//,$UPcol_name);
    $curdir = "$URLbasedir$COLLdir";
    $formsdir = "$curdir/forms";
    foreach $dir (@dirs) {
	$parentdir = $curdir;
	$curdir .= "/".$dir;
	if ( ! -d $curdir ){
	    if (! $create) { return 1 ;} 
	    # check that this user has access to create the collection
	    if ($acc_ok || &chk_access("$formsdir")) {
		$acc_ok = 1;
		if (mkdir($curdir,$mode775) && mkdir("$curdir/forms",$mode775)) {
		    # If there are read restrictions on parent, copy them down
		    if ( -r "$parentdir$HTACCESS" ) {
			`cp "$parentdir$HTACCESS" "$curdir$HTACCESS"`;
			`ln "$curdir$HTACCESS" "$curdir/.htaccess"`;
			`ln -s $makeindex "$curdir/makeindex"`;
		    }

		    `cp -p "$formsdir$HTACCESS" "$curdir/forms"`;
		    `cp -p "$formsdir/tagfile" "$curdir/forms"`;
		    `cp -p "$formsdir/description" "$curdir/forms"`;
		}
		else 
		{
		    if ($! ne "File exists") 
		    { 
			return 2;
		    }
		    
		    else  # ( if the error message is "file exists", wait till 
                          #everything is created by the other process ...)
		    {
			while (! -e "$curdir/forms/description") { sleep (1); }
		    }
		}
	    }
	    else {   
		return 3;
	    }
	}
        elsif ( -d "$curdir/forms") {
	    $formsdir = "$curdir/forms";
	    while (! -e "$curdir/forms/description") { sleep (1); }
	}
	
    }
    
    # We now have everything up through $UPcol_name
    $acc_ok = ($acc_ok || &chk_access("$formsdir"));
    if (! $acc_ok ) { return 3;} 
    
    # Check for missing components, we know we have write permission since
    #   we have check access in forms dir

    if ($create) {  
	if ( ! -d "$curdir/tags")  {
	    if ( (!mkdir("$curdir/tags",$mode775)) && ($! ne "File exists")) {
		return 2;
	    }
	}
	
	if ( ! -d "$curdir/images")  {
 	    if ( (!mkdir("$curdir/images",$mode775)) && ($! ne "File exists")) {
		return 2;
	    }
	}
	
	if ( ! -d "$curdir/mdata")  {
	    if ( -d "$curdir/hi-res") {
		`mv "$curdir/hi-res" "$curdir/mdata"`;
		`ln -s "mdata" "$curdir/hi-res"`;
	    } else {
		if (mkdir("$curdir/mdata",$mode775)){
		    # copy any restrictions on accessing the high-resolution data
		    if ( -r "$formsdir/../mdata$HTACCESS") {
			`cp "$formsdir/../mdata$HTACCESS" "$curdir/mdata"`;
			`ln "$curdir/mdata$HTACCESS" "$curdir/mdata/.htaccess"`;
		    }
		    `ln -s "mdata" "$curdir/hi-res"`;
		}
		else { 
		    if ($! ne "File exists") {
			return 2;
		    } else { sleep (1); }  #wait for the process that created mdata
		}
	    }
	}
    }
    return 0;
}
1;

