/ startup.sh
startup.sh
  1  #! /usr/bin/env sh
  2  
  3  ###############################################################################
  4  #!  \brief  
  5  #   \author Shylock Hg
  6  #   \date 2018-11-27
  7  #   \email tcath2s@gmail.com
  8  ###############################################################################
  9  
 10  ###############################################################################
 11  # prerequisites
 12  # - git
 13  # - curl
 14  # - mksource
 15  ###############################################################################
 16  target=''
 17  
 18  readonly dest=$PWD  # target repo directory
 19  
 20  readonly target_c='c'
 21  readonly url_c_gitignore='https://raw.githubusercontent.com/github/gitignore/master/C.gitignore'
 22  
 23  readonly target_cc='cc'
 24  readonly url_cc_gitignore='https://raw.githubusercontent.com/github/gitignore/master/C%2B%2B.gitignore'
 25  
 26  readonly source_main='int main(int argc, char * argv[]) {\n        return 0;\n}'
 27  
 28  check_prerequisites() {
 29          # git
 30          if ! command -v git >/dev/null; then
 31                  echo 'Err: Require git!'
 32                  return 1;
 33          fi
 34          # curl
 35          if ! command -v curl >/dev/null; then
 36                  echo 'Err: Require curl!'
 37                  return 1;
 38          fi
 39          # mksource
 40          if ! command -v mksource >/dev/null; then
 41                  echo 'Err: require mksource!'
 42                  return 1;
 43          fi
 44          return 0;
 45  }
 46  
 47  common() {  # common dir
 48          git init $1
 49  }
 50  
 51  # check prerequisites
 52  if ! check_prerequisites; then
 53          exit 1
 54  fi
 55  
 56  # handle input parameters
 57  while getopts 't:h' args
 58  do
 59  	case $args in
 60  		t)  #!< target type
 61                          target=$OPTARG
 62  			;;
 63  		h)
 64  			printf "usage: $0 -t <target> name...\neg: $0 -t cc hello echo"
 65                          exit 0
 66  			;;
 67  		?)
 68  			echo "Err: Unknown argument $OPTARG!"
 69  			;;
 70  	esac
 71  done
 72  
 73  shift $((OPTIND - 1))
 74  
 75  # check target input
 76  if [ -z "$target" ]; then
 77          echo 'Err: Empty target!'
 78          exit 1
 79  fi
 80  
 81  # check names input
 82  if [ -z "$*" ]; then
 83          echo 'Err: Empty names!'
 84          exit 1
 85  fi
 86  
 87  # handle sub directories input
 88  for dir in $*
 89  do
 90          if [ $target = $target_c ]; then
 91                  common $dest/$dir
 92                  if ! curl $url_c_gitignore > $dest/$dir/.gitignore; then
 93                          exit 1
 94                  fi
 95                  pwd=$PWD
 96                  cd $dest/$dir
 97                  if ! mksource -n main.c; then
 98                          exit 1
 99                  fi
100                  if ! mksource -n Makefile; then
101                          exit 1
102                  fi
103                  printf "$source_main" >> main.c
104                  mkdir -p 'source'
105                  mkdir -p 'include'
106                  cd $pwd
107          elif [ $target = $target_cc ]; then
108                  common $dest/$dir
109                  if ! curl $url_cc_gitignore > $dest/$dir/.gitignore; then
110                          exit 1
111                  fi
112                  pwd=$PWD
113                  cd $dest/$dir
114                  if ! mksource -n main.cc; then
115                          exit 1
116                  fi
117                  if ! mksource -n Makefile; then  # modify CC manually
118                          exit 1
119                  fi
120                  printf "$source_main" >> main.cc
121                  mkdir -p 'source'
122                  mkdir -p 'include'
123                  cd $pwd
124          else
125                  echo "Err: Unkonwn target $target!"
126          fi
127  done