22set -e
33
44AUTHOR=" Hao Chen"
5- LEETCODE_URL=https://oj.leetcode.com/problems/
5+ LEETCODE_URL=https://leetcode.com/problems/
6+ LEETCODE_NEW_URL=https://leetcode.com/problems/
7+ LEETCODE_OLD_URL=https://oj.leetcode.com/problems/
8+ COMMENT_TAG=" //"
9+ FILE_EXT=" .cpp"
610
711function usage()
812{
@@ -46,11 +50,21 @@ function install_xidel()
4650 echo " Install xidel successfullly !"
4751}
4852
49- if [ $# -lt 1 ] || [[ " ${1} " != ${LEETCODE_URL } * ]]; then
53+ if [ $# -lt 1 ] || [[ " ${1} " != ${LEETCODE_NEW_URL} * ]] && [[ " ${1} " != ${LEETCODE_OLD_URL }* ]]; then
5054 usage
5155 exit 255
5256fi
5357
58+ if [[ " ${1} " == ${LEETCODE_OLD_URL} * ]]; then
59+ LEETCODE_URL=${LEETCODE_OLD_URL}
60+ fi
61+
62+ IS_SHELL=` curl ${1} 2> /dev/null | grep Bash | wc -l`
63+ if [ ${IS_SHELL} -gt 0 ]; then
64+ COMMENT_TAG=' #'
65+ FILE_EXT=' .sh'
66+ fi
67+
5468
5569leetcode_url=$1
5670current_time=` date +%Y-%m-%d`
@@ -61,7 +75,7 @@ if [ $# -gt 1 ] && [ -f $2 ]; then
6175else
6276 source_file=${1# ${LEETCODE_URL} }
6377 source_file=${source_file:: ${# source_file} -1}
64- source_file=` echo $source_file | awk -F ' -' ' {for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }' ` .cpp
78+ source_file=` echo $source_file | awk -F ' -' ' {for (i=1; i<=NF; i++) printf("%s", toupper(substr($i,1,1)) substr($i,2)) }' ` ${FILE_EXT}
6579
6680 if [ ! -f ${source_file} ]; then
6781 echo " Create a new file - ${source_file} ."
7387fi
7488
7589# adding the Copyright Comments
76- if ! grep -Fq " // Author :" $source_file ; then
77- sed -i ' 1i\' " // Source : ${leetcode_url} " $source_file
78- sed -i ' 2i\' " // Author : ${AUTHOR} " $source_file
79- sed -i ' 3i\' " // Date : ${current_time} \n" $source_file
90+ if ! grep -Fq " ${COMMENT_TAG} Author :" $source_file ; then
91+ sed -i ' 1i\' " ${COMMENT_TAG} Source : ${leetcode_url} " $source_file
92+ sed -i ' 2i\' " ${COMMENT_TAG} Author : ${AUTHOR} " $source_file
93+ sed -i ' 3i\' " ${COMMENT_TAG} Date : ${current_time} \n" $source_file
8094fi
8195
8296# grab the problem description and add the comments
@@ -86,14 +100,27 @@ if [ -z "${xidel}" ]; then
86100 install_xidel
87101fi
88102
89- xidel ${leetcode_url} -q -e " css('div.question-content')" | \
90- grep -v ' ' | sed ' /^$/N;/^\n$/D' | \
91- sed ' s/^/ * /' | sed " 1i/*$( printf ' %.0s*' {0..80}) \n * " | \
92- sed " \$ a \ $( printf ' %.0s*' {0..80}) */\n" > /tmp/tmp.txt
103+ case $FILE_EXT in
104+ .cpp ) xidel ${leetcode_url} -q -e " css('div.question-content')" | \
105+ grep -v ' ' | sed ' /^$/N;/^\n$/D' | \
106+ sed ' s/^/ * /' | sed " 1i/*$( printf ' %.0s*' {0..80}) \n * " | \
107+ sed " \$ a \ $( printf ' %.0s*' {0..80}) */\n" > /tmp/tmp.txt
108+ ;;
109+ .sh ) xidel ${leetcode_url} -q -e " css('div.question-content')" | \
110+ grep -v ' ' | sed ' /^$/N;/^\n$/D' | \
111+ sed ' s/^/# /' | sed " 1i#$( printf ' %.0s#' {0..80}) \n# " | \
112+ sed " \$ a \#$( printf ' %.0s#' {0..80}) \n" > /tmp/tmp.txt
113+ ;;
114+ * ) echo " Bad file extension!"
115+ exit 1;
116+
117+ esac
93118
94119sed -i ' 4 r /tmp/tmp.txt' ${source_file}
95120
96121rm -f /tmp/tmp.txt
97122
98123echo " ${source_file} updated !"
99124
125+
126+
0 commit comments