#!./vipsi

var as_root = 1
try
	if   globals[1]=="-r" || globals[1]=="--root" as_root = 1
	elif globals[1]=="-u" || globals[1]=="--user" as_root = 0
	else log "unknown option",nl end 1
	then
then

if as_root
	var d = dir "/usr/bin*"
	if count(d)==0 log «"/usr/bin/" not found. fix me.»,nl end 1 then
	if !find(d[1].flags,"w")
		log nl, «root privileges required for standard installation.»,
		 	nl, «use "su" or "sudo" to gain root privileges.»,
			nl, «use "./install.vs -u" to install into "~/bin/".»
		put nl, nl
		end 1
	then
then


put«
  *********************************************
  *                hello vipsi                *
  *********************************************

        I'm about to install myself.»

if as_root put«
        standard installation in "/usr/bin/"
        and libraries in "/usr/lib/vipsi/".
»
else put«
        user installation in "~/bin/"
        and libraries in "~/.vipsi/".
»
then


proc Delete ( Target )
{
	try
		if islink Target del link Target else sys "rm", "-Rf", fullpath(Target) then
		return 0
	else
		log nl, «  delete »#Target#« failed: »
		log nl, "  ", errorstr
		return 1
	then
}


proc ReplaceFile ( Target, Source, Owner, Mode )
{
	put nl, «  create file », Target
	if fullpath Target == fullpath Source put " (skipped)" return 0 then	// target == link -> source

	try
		file Target = file Source
		sys "chown", Owner,fullpath(Target)
		sys "chmod", Mode, fullpath(Target)
		return 0
	else
		log nl, «  create file »#Target#« failed:»
		log nl, "  ", errorstr
		return 1
	then
}


proc CreateFile ( Target, Source, Owner, Mode )
{
	if isfile Target return 0 then
	return ReplaceFile(Target,Source,Owner,Mode)
}


proc CreateDir ( Target, Owner, Mode )
{
	proc createdir(path)
	{
		var ppath = path[to rfind(path,"/",count(path)-1)]
		if !exists dir ppath createdir(ppath) then
		new dir path
		sys "chown", Owner,fullpath(path)
		sys "chmod", Mode, fullpath(path)
	}

	if !exists dir Target
		put nl, «  create dir », Target
		try
			createdir(Target)
		else
			log nl, «  create dir »#Target#« failed:»
			log nl, "  ", errorstr
			return 1
		then
	then
	return 0
}


proc ReplaceDir ( Target, Source, Owner, Mode )
{
	put nl, «  copy dir », Target
	if fullpath Target == fullpath Source put " (skipped)" return 0 then	// target == link -> source

	try
		if Delete(Target) return 1 then
		sys "cp",   "-Rf", fullpath(Source),fullpath(Target)
		sys "chown","-Rf", Owner, fullpath(Target)
		sys "chmod","-Rf", Mode,  fullpath(Target)
		return 0
	else
		log nl, «  copy dir »#Target#« failed:»
		log nl, "  ", errorstr
		return 1
	then
}


proc CreateLink ( Target, Path, Owner )
{
	if !islink Target
		put nl, «  create symlink », Target
		try
			link Target = Path
		//	sys "chown",Owner,fullpath(Target)
		else
			log nl, errorstr
			return 1
		then
	then
	return 0
}

proc ReplaceLink ( Target, Path, Owner )
{
	put nl, «  create symlink », Target
	var Source = Path[1]=="/" ? Path : Target[to rfind(Target,"/",count Target-1)] # Path
	try
		if fullpath Target == fullpath Source put " (skipped)" return 0  then		// target == link -> source
	else	// fullpath bummered!
		log nl, errorstr
		return 1
	then

	return Delete(Target) || CreateLink(Target,Path,Owner)
}



var errors = 0

if as_root
	var User = sys("whoami") replace User, nl, ""
	var libs = "/usr/lib/vipsi/"

	errors += ReplaceFile( "/usr/bin/vipsi", "./vipsi", User, "a+rx")

	errors += CreateFile ( "/etc/vipsi.cgi.conf", "../libs/vipsi.cgi.conf", User, "a+r"  )

	errors += CreateDir  ( "/usr/share/apps/katepart/syntax/", User, "go-w")
	errors += ReplaceFile( "/usr/share/apps/katepart/syntax/vip.xml", "../libs/vip.xml", User, "a+r" )

	errors += CreateDir  ( libs, User, "o-rwx")
	errors += ReplaceFile( libs # "script.prefs",  "../libs/script.prefs",  User, "a+r"  )
	errors += ReplaceFile( libs # "shell.prefs",   "../libs/shell.prefs",   User, "a+r"  )

else
	var User = sys("logname") replace User, nl, ""
	var libs = "~/.vipsi/"

	errors += CreateDir  ( "~/bin/", User, "o-rw" )
	errors += ReplaceFile( "~/bin/vipsi", "./vipsi", User, "a+rx" )

	errors += CreateDir  ( libs, User, "o-rwx")
	errors += CreateFile ( libs # "script.prefs",  "../libs/script.prefs",  User, "o-rwx"  )
	errors += CreateFile ( libs # "shell.prefs",   "../libs/shell.prefs",   User, "o-rwx"  )

then
	errors += ReplaceDir ( libs # "test_suite",    "../libs/test_suite",    User, "a+rx" )
	errors += ReplaceFile( libs # "TEXT.vl",       "../libs/TEXT.vl",       User, "a+rx" )
	errors += ReplaceFile( libs # "HTML.vl",       "../libs/HTML.vl",       User, "a+rx" )
	errors += ReplaceFile( libs # "BOOK.vl",       "../libs/BOOK.vl",       User, "a+rx" )
	errors += ReplaceFile( libs # "DIR.vl",        "../libs/DIR.vl",        User, "a+rx" )
//	errors += ReplaceDir ( libs # "documentation", "../documentation",      User, "a+rX" )
//	errors += ReplaceLink( libs # "documentation/libs", "..", User )


if errors
	log nl,nl, "some errors occured. check output and try to fix it.",nl,nl
	end 1
else
	put nl,nl, "all seemed to install fine. you are done.",nl,nl
	end 0
then









