Commit Diff


commit - 15169311c502349645c62e8b2961ee8eab25a48e
commit + 4465e20ce7fc0029dc886e2f5748ac9ef2ed0f59
blob - c65b07d9eebb9a884306d10e4972ceb7a235685c (mode 755)
blob + /dev/null
--- nwpg.sh
+++ /dev/null
@@ -1,86 +0,0 @@
-#!/bin/zsh
-
-#		nwpg (NeW ProGram)
-#	Simple script that initiates a new project
-#	in the user's $HOME/hack directory.
-#	Such a project consists of a parent folder
-#	named after the project	and three files:
-#	the algorithm, the code, the documentation,
-#	i.e. main.algo, main.c, main.doc 
-#
-#	syntax: nwpg [-h] [-d directory] [-l language] project_title
-#	see EOF for TODO
-
-DEFAULT_DIR="$HOME/hack/" ;
-DEFAULT_LANG="C" ;
-USAGE="usage: nwpg [-h] [-l language] [-d directory] project_title" ;
-
-case $# in
-	1)
-		if [ $1 == '-h' ] || [ $1 == '--help' ]
-		then
-			 error=1
-		fi
-		language="$DEFAULT_LANG" ;
-		project_title="$1" ;
-		directory="$DEFAULT_DIR$project_title" ;;
-	3 | 5)
-		#which_shell= ps -p $$ | awk '{if(NR>1)print}' | awk '$0=$NF' | tr -d - ;
-		#echo $which_shell ;
-		#case $which_shell in
-		#	"zsh" )
-		#		echo "using zsh" ;
-		project_title="${@[-1]}" ;
-		#	"ksh" | "bash" )
-		#		echo "using ksh or bash" ;
-		#		project_title="$!#" ;#;
-		#esac	
-		#echo "Project title: $project_title" ;
-		case $1 in
-			-l )
-				language="$2" ;
-				directory="$DEFAULT_DIR$project_title" ;;
-			-d )
-				language="$DEFAULT_LANG" ;
-				directory="$2/$project_title" ;;
-		esac
-		case $3 in
-			-l )
-				language="$4" ;;
-			-d )
-				directory="$4/$project_title" ;;
-		esac ;;
-	*)
-		error=1 ;;
-esac
-
-if [ -n $error ]
-then
-	mkdir -p $directory ;
-	#echo "Arguments:\n\t\$1 $1\n\t\$2 $2\n\t\$3 $3" ;
-	case $language in 
-		C | c )
-			#echo "programming in C" ;
-			file_ext="c" ;;
-		Python | Py | py )
-			file_ext="py" ;;
-		Rust )
-			#echo "Rust project" ;
-			file_ext="" ;;
-		* )
-			file_ext="" ;
-			echo $USAGE ;;
-		esac
-	touch $directory/main.{$file_ext,doc,algo} ; #&& echo "files created : $directory/main.{$file_ext,doc,algo}" ;
-else
-	echo $USAGE ;
-fi
-
-#
-#		TODO
-#
-#	Use getopts for a cleaner ksh script
-#	Separate linux, macos and openbsd versions
-#	Clean commented debugging features
-#
-
blob - /dev/null
blob + c2ac497b406d22a09221f8ff7fd6ccc39818664d (mode 644)
--- /dev/null
+++ nwpg.ksh
@@ -0,0 +1,85 @@
+#!/bin/ksh
+
+PROGRAM_NAME="nwpg" ;
+PROGRAM_VERSION="0.2" ;
+USAGE="usage: nwpg [-l language] [-d directory] project_title" ;
+
+#		nwpg (NeW ProGram) - pdksh/ksh88 version
+#	Simple script that initiates a new project
+#	in the user's $HOME/hax directory.
+#	Such a project consists of a parent folder
+#	named after the project	and three files:
+#	the algorithm, the code, the documentation,
+#	i.e. main.algo, main.c, main.md 
+# 
+
+DEFAULT_DIR="$HOME/hax/" ;
+DEFAULT_LANGUAGE="C" ;
+COUNT_ARGS="$#";
+integer dir_flag=0 ;
+integer lang_flag=0 ;
+
+if [[ $COUNT_ARGS -eq 1 || $COUNT_ARGS -eq 3 || $COUNT_ARGS -eq 5  ]]
+then
+	eval project_title=\${$#} ;
+else
+	echo $USAGE;
+	exit 2 ;
+fi
+
+while getopts 'l:d:' opt ;
+do
+	case $opt in
+		d)
+			integer dir_flag=1 ;
+			directory="$OPTARG" ;;
+		l)
+			integer lang_flag=1 ;
+			language="$OPTARG" ;;
+		?)
+			echo $USAGE ;
+			exit 2 ;;
+	esac
+done
+shift $(($OPTIND - 1))
+
+if [ $dir_flag -eq 1 ]
+then
+	PROJECT_FULL_PATH="$directory/$project_title" ;
+else
+	PROJECT_FULL_PATH="$DEFAULT_DIR/$project_title" ;
+fi
+
+if [ $lang_flag -eq 0 ]
+then
+	language="$DEFAULT_LANGUAGE" ;
+fi
+
+case "$language" in
+	C | c )
+		FILE_EXT="c" ;;
+	Python | PY | Py | python | py )
+		SHEBANG='#!/usr/local/bin/python' ;
+		FILE_EXT="py" ;;
+	Rust | RS | Rs | rust | rs )
+		FILE_EXT="rs" ;;
+	Ruby | RB | Rb | ruby | rb )
+		FILE_EXT="rb" ;;
+	Shell | sh )
+		SHEBANG="#!/bin/sh" ;
+		FILE_EXT="sh" ;;
+	PYTHON | RUST | RUBY )
+		echo "Don't shout !\n" ;
+		exit 2 ;;
+	* )
+		echo "unsupported language: $language" ;
+		exit 2 ;;
+esac
+
+mkdir -p "$PROJECT_FULL_PATH" ;
+touch "$PROJECT_FULL_PATH/"main.{$FILE_EXT,algo,md} ;
+if [ -n "$SHEBANG" ]
+then
+	echo "$SHEBANG\n" >> "$PROJECT_FULL_PATH/main.$FILE_EXT" ;
+fi
+echo "$FILE_EXT files created in $PROJECT_FULL_PATH" ;
\ No newline at end of file