Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/ash
- [ -z "$alias_file" ] && alias_file="$(realpath ~/cat_test_alias.in)"
- PKG_FIX_CATEGORIES_in_REPO_DB=true
- PKG_use_ALIASES_in_DB_CAT_Lookup=true
- CATEGORIES_INIT_BLOCK='FILENAME==CATFILE {parse_cats($0)
- next} #https://www3.physnet.uni-hamburg.de/physnet/Tru64-Unix/HTML/APS32DTE/WKXXXXXX.HTM'
- [ -z "$PKG_FIX_ALIASES" ] && PKG_FIX_ALIASES=true
- function cat_macro_fn(){
- if [ "$PKG_FIX_ALIASES" = true ]; then
- echo "$1"
- else
- echo ""
- fi
- }
- function alias_macro_fn(){
- if [ "$PKG_use_ALIASES_in_DB_CAT_Lookup" = true ]; then
- echo "$1"
- else
- echo ""
- fi
- }
- function comment_macro(){
- echo ""
- }
- ALIAS_FNS="$(awk -v ALIASFILE="$alias_file" '
- function add_get_canonical_hdr(){
- print "function get_canonical_name(s,v){ "
- print " if (s in CANONICAL_ARY){ "
- print " return CANONICAL_ARY[s] "
- print " } else { "
- print " switch(s \"-\" v){ "
- }
- function add_get_canonical_footer(){
- print " } "
- print " } "
- print " return s "
- print "} "
- }
- function add_canonical_case(s,name){
- #TODO: Do this untill there is no astrixs left to replace.
- s=gensub(/^(.*[^.*])(\*)(.*)$/,"\\1.*\\3","G",s) # replace "*" with ".* "
- s=gensub(/^(.*[^\\+])(\+)(.*)$/,"\\1\\\\+\\3","G",s) # replace "+" with "\+"
- print " case /" s "/: "
- print " return \"" name "\" "
- }
- #function write_CANONICAL_ARY_init_hdr(){
- # print "function init_CANONICAL_ARY(){ "
- #}
- function write__init_CANONICAL_ARY(){
- print "function init_CANONICAL_ARY(){ "
- for (p in CANONICAL_ARY){
- print " CANONICAL_ARY[\"" p "\"]=\"" CANONICAL_ARY[p] "\""
- }
- print "}"
- }
- #This writes the start of the function get_canonical_name()
- #this function will return a single name for all equivalent alias values.
- BEGIN { FS = ","; add_get_canonical_hdr()}
- #If wild cards are used in an alias get_canonical_name() uses regX case statments to do the match.
- #Otherwise get_cannonical_name() looks up the value in an associative array (AKA dictonary or hashmap)
- {
- canonical_NAME=$1
- sub(/\*/,"",canonical_NAME) #This isn'"'"'t necissary but makes the key slightly nicer looking
- for(i=NF;i>=1;i--){
- if(index($i,"*")>0){
- add_canonical_case($i,canonical_NAME) #alias has a wild card so wirite case statement to match alias
- }
- else{
- CANONICAL_ARY[$i]=canonical_NAME #alias has no wild card so add to lookup array)
- }
- }
- }
- END {add_get_canonical_footer()
- write__init_CANONICAL_ARY()
- } ' "$alias_file")"
- #echo "$ALIAS_FNS"
- CATEGORY_FNS=\
- 'function cat_fallback(s){
- if (length(s)>0){
- switch(s){
- case /vcs.*/:
- return "Utility;development"
- break
- case /doc.*/:
- return "Help"
- break
- case /admin.*/:
- return "Setup"
- break
- case /games.*/:
- return "Fun"
- break
- case /.{7,}/:
- return "science"
- break
- default:
- return "BuildingBlock"
- break
- }
- }
- else
- {
- return "BuildingBlock"
- }
- }
- function get_category(s1,v1,c1, out,s2,sc1,sc2){
- if ( s1 in pkg_categories){
- return pkg_categories[s1]
- } '"
- $(alias_macro_fn \
- ' sc1=get_canonical_name(s1,v1)
- if (sc1 in pkg_categories_canonical){
- return pkg_categories_canonical[sc1]
- }
- s2=gensub(/^(.+[^0-9])([0-9]*)$/,"\\1","g",s1)
- if ( s2 in pkg_categories){
- return pkg_categories[s2]
- }
- sc2=get_canonical_name(s2,v1)
- if (sc2 in pkg_categories_canonical){
- return pkg_categories_canonica[sc2]
- }')""$(comment_macro "****End: alias_macro_fn()"****)"'
- #TODO: consider also trying to look up category based on the full name.
- return cat_fallback(c1)
- }
- function parse_cats(s){
- i=index(s,"=")
- if (i>0){
- #catagory=( package1 package2 ... packageN)
- category=substr(s,0,i-1)
- sub(/PKGCAT_/,"",category) #e.g. category=PKGCAT_Utility => category=Utility
- sub(/_/,";",category) #e.g. category=Filesystem_filemanager => category=Filesystem;filemanager
- #e.g catagory=( package1 package2 ... packageN) => package1 package2 ... packageN
- s=gensub(/^([^=]+="\s*)([^\s].*[^\s])(\s*"\s*)$/,"\\2","g",s)
- split(s,pkg_names," ")
- for (pkg_i in pkg_names){
- pkg=pkg_names[pkg_i]
- pkg_categories[pkg]=category
- pkg_categories_canonical[pkg]=get_canonical_name(category)
- }
- }
- }'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement