#!/bin/sh

WEBPATH=http://files.directadmin.com/services/customapache/jail
WEBPATH_BACKUP=http://www.directadmin.com/services/customapache/jail

CUST_APACHE=/usr/local/directadmin/customapache
BUILD=${CUST_APACHE}/build
DIR=${CUST_APACHE}/jail
CWD=`pwd`
SHELLS=/etc/shells

OS=`uname`

APACHE_VER=`grep 'APACHE_VER=' $BUILD | cut -d= -f2`;
SH_UTILS_VER=2.0.15
NB_SMTP_VER=1.00

####################################################

showHelp() {

        echo "***********************************";
        echo "*                                 *";
        echo "* DirectAdmin User Jail Installer *";
        echo "*                                 *";
        echo "***********************************";
	echo "";
	echo "  To install all parts, run:";
	echo "     $0 all";
	echo "";
	echo "  Other options:";
	echo "     $0 suexec";
	echo "     $0 sh_utils";
	echo "     $0 shell";
	echo "     $0 smtp_mail";
	echo "";
	echo "  Remove old build data:";
	echo "     $0 clean";
	echo "";
	echo "  Get lastest build script and data";
	echo "     $0 update";
	echo "";
	echo "  Get data for current build script";
	echo "     $0 update_data";
}

####################################################

patch_suexec() {
	
	cd $CWD;

	#check for OS and copy the correct suexec.c (if they differ)

	SU_FILE=suexec.c
	if [ $OS = "FreeBSD" ]; then
		SU_FILE=suexec.c.freebsd
	fi

	if [ ! -e $DIR/$SU_FILE ]; then echo "cannot find $SU_FILE.  Run './build update_data'"; return; fi
	
	FILE=$CUST_APACHE/apache_$APACHE_VER/src/support/suexec.c
	cp -f $DIR/$SU_FILE $FILE
	chmod 1755 $FILE

	#add just a touch of nothing to increase the datestamp so that 'make' finds it.
	echo -n "" >> $FILE;

	cd $CUST_APACHE/apache_$APACHE_VER

	make

	if [ $? -ne 0 ]; then
		echo -e "\n*** The make has failed ***";
		return;
	fi

	make install

	if [ $OS = "FreeBSD" ]; then
		/usr/local/etc/rc.d/httpd restart
	else
		/sbin/service httpd restart
	fi

}
####################################################

compile_sh_utils() {

	cd $CWD;

	if [ $OS = "FreeBSD" ]; then
		cp -f /usr/bin/su ./su
		return;
	fi

	NAME=sh-utils-${SH_UTILS_VER}
	FILE=$DIR/${NAME}.tar.gz
	
	if [ ! -e $FILE ]; then echo "cannot find $FILE.  Run './build update_data'"; return; fi

	tar xvzf $FILE;

	cd $NAME;
	./configure
	
	if [ $? -ne 0 ]; then
		echo -e "\n*** The configure has failed ***";
		return;
	fi

	make
	
	if [ $? -ne 0 ]; then
		echo -e "\n*** The make has failed ***";
		return;
	fi

	cp -f ./src/su ../su
}

####################################################

compile_shell() {

	cd $CWD;

	gcc -o jail chrootshell.c
	
	if [ $? -ne 0 ]; then
		echo -e "\n*** The make has failed ***";
		return;
	fi

	cp -f jail /bin/jail
	chmod 6755 /bin/jail

	COUNT=`grep -c '/bin/jail' $SHELLS`;
	if [ $COUNT -eq 0 ]; then
		echo "/bin/jail" >> $SHELLS;
	fi
}

####################################################

compile_nbsmtp() {

	cd $CWD;

	FILE=nbsmtp-${NB_SMTP_VER}.tar.gz
	#mkdir -p nbsmtp_build
	#cd nbsmtp_build
	#tar xvzf ../${FILE}
	#make linux
	#if [ $? -ne 0 ]; then
	#	echo -e "\n*** The make has failed ***";
	#	return;
	#fi
	#cp nbsmtp ../nbsmtp

	tar xvzf $FILE
	cd nbsmtp-${NB_SMTP_VER}
	./configure
	make
        if [ $? -ne 0 ]; then
               echo -e "\n*** The make has failed ***";
               return;
        fi

	cp nbsmtp ../nbsmtp
	cd ..
}

####################################################

do_all() {

	patch_suexec;
	compile_sh_utils;
	compile_shell;
	compile_nbsmtp;
}

####################################################

do_clean() {
	rm -rf sh-utils-${SH_UTILS_VER}
	rm -f $DIR/su
	rm -f jail
	rm -rf nbsmtp_build
	rm -f nbsmtp
}

####################################################

getFile() {
        if [ ! -e $1 ] || [ "$2" = "force" ];
        then
                echo -e "Downloading\t\t$1...";
                if [ $OS = "FreeBSD" ]; then
                        fetch ${WEBPATH}/${1};
                else
                        wget ${WEBPATH}/${1} -O ${CWD}/${1};
                fi
                if [ ! -e $1 ]
                then
                        echo "Fileserver is down, using the backup file server..";
                        if [ $OS = "FreeBSD" ]; then
                                fetch ${WEBPATH_BACKUP}/${1};
                        else
                                wget ${WEBPATH_BACKUP}/${1} -O ${CWD}/${1};
                        fi
                fi

        else
                echo -e "File already exists:\t${1}";
        fi

}

####################################################

do_update() {

	cd $CWD;
	if [ $OS = "FreeBSD" ]; then
		fetch -o ${DIR}/build.new ${WEBPATH}/build
	else
		wget -O ${DIR}/build.new ${WEBPATH}/build
	fi
	if [ ! -e ${DIR}/build.new ]; then
		echo "Fileserver is down, using the backup file server..";
		if [ $OS = "FreeBSD" ]; then
			fetch -o ${DIR}/build.new ${WEBPATH_BACKUP}/build
		else
			wget -O ${DIR}/build.new ${WEBPATH_BACKUP}/build
		fi
	fi

	mv -f build.new build
	chmod 755 build

	./build update_data
}

####################################################

do_update_data() {

	getFile suexec.c force
	getFile suexec.c.freebsd force
	getFile chrootshell.c force
	getFile files.list force
	getFile jail_user.sh force
	chmod 755 jail_user.sh
	getFile nbsmtp-${NB_SMTP_VER}.tar.gz
	getFile sendmail force
	chmod 755 sendmail
	getFile sh-utils-${SH_UTILS_VER}.tar.gz

}

####################################################

case "$1" in
	all)
		do_all;
		;;
	clean)
		do_clean;
		;;
        suexec)
                patch_suexec;
                ;;
	sh_utils)
		compile_sh_utils;
		;;
	shell)
		compile_shell;
		;;
	smtp_mail)
		compile_nbsmtp;
		;;
	update)
		do_update;
		;;
	update_data)
		do_update_data;
		;;
        *)
                showHelp;
                exit 1;
                ;;
esac

exit 0;

