#!/bin/bash

msg="
#######################################################################
##  For Programmers: C, C++, Perl, Bash.                             ##
##  Using Vim, you can open new program's file with some header.     ##
##  F.E.  vimext -A my_progr.c  - will open C-file with nine         ##
##        including statements and line with declaration of main     ##
##        program.                                                   ##
##        Using options you can include library - separatly.         ##
##  F.E.  vimext -i -s (or -is) file.cpp  - will open C++-file       ##
##        with included <iomanip>, <string> statements. Default  ##
##        include is: <stdio.h> for C and <iostream> for C++.    ##
##        vimext -h  - help-information about using options.         ##
##                                                                   ##
##  You should remember that this script works in nested shell!      ##
#######################################################################
"

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
c_mysql_file()
{
cat > $filename << c_mysql
#include <stdio.h>
#include <mysql.h>
#include <password.hh>

int main (int argc, char *argv[])
{
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;

	conn = mysql_init(NULL);
	mysql_real_connect(
			conn,"","",password(),
			"",3306,NULL,
			0);

c_mysql
vim + $filename;
exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
c_file()
{
	if [ "$flag_M" ]; then
		c_mysql_file;
	fi

	b="#include <stdio.h>\n"

	if [ "$flag_A" ]; then
		A="#include <stdlib.h>\n#include <string.h>\n#include \
<unistd.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include \
<ctype.h>\n#include <time.h>#include <fcntl.h>\n#include <math.h>\n"
	echo -e "$b$A\nint main ( int argc, char *argv[] )\n{" > $filename;
	vim + $filename;
	exit 0;
	fi

	if [ "$flag_l" ]; then
		l="#include <stdlib.h>\n"
	else l=""; fi

	if [ "$flag_s" ]; then
		s="#include <string.h>\n"
	else s=""; fi

	if [ "$flag_m" ]; then
		m="#include <math.h>\n"
	else m=""; fi

	if [ "$flag_c" ]; then
		c="#include <ctype.h>\n"
	else c=""; fi

	if [ "$flag_t" ]; then
		t="#include <time.h>\n"
	else t=""; fi

	if [ "$flag_f" ]; then
		f="#include <fcntl.h>\n"
	else f=""; fi

	if [ "$flag_u" ]; then
		u="#include <unistd.h>\n"
	else u=""; fi

	if [ "$flag_g" ]; then
		g="#include <libgen.h>\n"
	else g=""; fi

	if [ "$flag_p" ]; then
		p="#include <publib.h>\n"
	else p=""; fi

	echo -e "$b$l$s$m$t$c$f$g$p$u\nint main ( int argc, char *argv[] )\n{" > $filename;
	vim + $filename;
	exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cpp_mysql_file()
{
cat > $filename <<cpp_mysql;
#include <sqlplus.hh>
#include <password.hh>
#include <strstream>
#include <string>
#include <iomanip>

int main ( int argc, char *argv[] )
{
	Connection conn("","","",password());
	Query q = conn.query();

cpp_mysql
vim + $filename;
exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cpp_file()
{
	if [ "$flag_M" ]; then
		cpp_mysql_file;
	fi

	b="#include <iostream>\n"

	if [ "$flag_A" ]; then
		A="#include <iomanip>\n#include <string>\n#include <algorithm>\n\
#include <vector>\n#include <fstream>\n#include <strsteam>\n\
#include <iterator>\n#include <numeric>\n"
	echo -e "$b$A\nint main ( int argc, char *argv[] )\n{" > $filename;
	vim + $filename;
	exit 0; fi

	if [ "$flag_i" ]; then
		i="#include <iomanip>\n"
	else i=""; fi

	if [ "$flag_s" ]; then
		s="#include <string>\n"
	else s=""; fi

	if [ "$flag_a" ]; then
		a="#include <algorithm>\n"
	else a=""; fi

	if [ "$flag_v" ]; then
		v="#include <vector>\n"
	else v=""; fi

	if [ "$flag_f" ]; then
		f="#include <fstream>\n"
	else f=""; fi

	if [ "$flag_c" ]; then
		c="#include <cctype>\n"
	else c=""; fi

	if [ "$flag_g" ]; then
		g="#include <iterator>\n"
	else g=""; fi

	if [ "$flag_n" ]; then
		n="#include <numeric>\n"
	else n=""; fi

	if [ "$flag_m" ]; then
		m="#include <cmath>\n"
	else m=""; fi

	if [ "$flag_l" ]; then
		l="#include <cstdlib>\n"
	else l=""; fi

	if [ "$flag_e" ]; then
		e="#include <strstream>\n"
	else e=""; fi

	if [ "$flag_t" ]; then
		t="#include <ctime>\n"
	else t=""; fi

	if [ "$flag_u" ]; then
		u="#include <cstdio>\n"
	else u=""; fi

	echo -e "$b$i$s$a$e$g$v$f$n$c$l$m${t}using namespace std;\n\n\
int main ( int argc, char *argv[] )\n{" > $filename;
	vim + $filename;
	exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
perl_mysql_file()
{
cat > $filename <<perl_filling;
#!/usr/bin/perl -w

use DBI;
use strict;

my ($dsn) = "DBI:mysql:test;mysql_read_default_file=$ENV{HOME}/.my.cnf";
my ($user_n) = "";
my ($pass)  = "";
my ($dbh,$sth);
my (@ary);
my ($query);

$dbh = DBI->connect($dsn,$user_n,$pass,{ RaiseError => 1 });

perl_filling
vim + $filename;
exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
usage()
{
	echo "^[[1;41;37mUSAGE:^[[0m `basename $0` [-slvmifecgtuah] filename.extention";
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
help_message()
{
	wh="^[[1;37m";
	gr="^[[1;32m";
	bl="^[[1;34m";
	en="^[[0m";

	echo
	echo "^[[1;41;37mUSAGE:^[[0m `basename $0` [-AMacefghilmnpstuv] filename.extention";
cat << help_msg
$wh***************************************************************$en
Creating some C, C++, Perl, Bash file with header.
   $wh extention$en -$bl sh$en  - $gr Bash script$en
              -$bl pl$en  - $gr Perl file$en
                       $wh[ -M ]$en header for connection with MySQL.
              -$bl c$en   - $gr C file$en
                       $wh[ -A ]$en including many common lib.
                       $wh[ -M ]$en header for connection with MySQL.
                       $wh[ -c ]$en include <ctype.h>
                       $wh[ -m ]$en include <math.h>
                       $wh[ -f ]$en include <fcntl.h>
                       $wh[ -g ]$en include <libgen.h>
                       $wh[ -l ]$en include <stdlib.h>
                       $wh[ -p ]$en include <publib.h>
                       $wh[ -s ]$en include <string.h>
                       $wh[ -t ]$en include <time.h>
                       $wh[ -u ]$en include <unistd.h>
              -$bl cpp$en - $gr C++ file$en
                       $wh[ -A ]$en including many common lib.
                       $wh[ -M ]$en header for connection with MySQL.
                       $wh[ -a ]$en include <algorithm>
                       $wh[ -c ]$en include <cctype>
                       $wh[ -e ]$en include <strstream>
                       $wh[ -f ]$en include <fstream>
                       $wh[ -i ]$en include <iomanip>
                       $wh[ -l ]$en include <cstdlib>
                       $wh[ -m ]$en include <cmath>
                       $wh[ -s ]$en include <string>
                       $wh[ -t ]$en include <ctime>
                       $wh[ -g ]$en include <iterator>
                       $wh[ -n ]$en include <numeric>
                       $wh[ -v ]$en include <vector>
                       $wh[ -u ]$en include <cstdio>
   $wh [ -h$en |$wh -help ]$en to see this help message.
Without any arguments,$wh vimext$en will message about itself.
$wh***************************************************************$en
help_msg
exit 0;
}
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

BADARG=65
BADOPTION=33
NA="$#"

if [ "$#" -lt "1" ]
then
	echo
	usage;
	echo -e "$msg";
	exit $BADARG
fi

while getopts ":AMslvmnifecpgtauh" Opt
do
	case $Opt in
		s  ) flag_s=TRUE;;
		l  ) flag_l=TRUE;;
		v  ) flag_v=TRUE;;
		M  ) flag_M=TRUE;;
		m  ) flag_m=TRUE;;
		n  ) flag_n=TRUE;;
		i  ) flag_i=TRUE;;
		f  ) flag_f=TRUE;;
		e  ) flag_e=TRUE;;
		c  ) flag_c=TRUE;;
		g  ) flag_g=TRUE;;
		p  ) flag_p=TRUE;;
		t  ) flag_t=TRUE;;
		u  ) flag_u=TRUE;;
		A  ) flag_A=TRUE;;
		a  ) flag_a=TRUE;;
		h  ) help_message;;
		*  ) echo -e "Unknown option!\n"; usage; exit $BADOPTION;;
	esac
done
shift $(($OPTIND - 1))

filename="$1"
filebase=${filename%.*}
fileext=${filename#$filebase.}

if [ -e "$filename" ]; then
	echo
	echo "file $filename exists!"
	exit 1;
fi

# create bash file
if [ "$fileext" = "sh" ]; then
	echo -e "#!/bin/bash\n\n" > $filename;
	vim + $filename; exit 0;

# create perl file
elif [ "$fileext" = "pl" ]; then # perl file
	if [ "$NA" -eq "1" ]; then # number arguments
		echo -e "#!/usr/bin/perl -w\n\n" > $filename;
		vim + $filename; exit 0;
	fi
	if [ "$flag_M" = "TRUE" ]; then
		perl_mysql_file;
	fi

# create c file
elif [ "$fileext" = "c" ]; then
	c_file;
elif [ "$fileext" = "cpp" ]; then
	cpp_file;
else
	echo "OK"
fi