Guest User

Untitled

a guest
Feb 18th, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function folder_dialog()
  4. {
  5. local result=$(osascript << EOT
  6. tell application "Finder"
  7. activate
  8. set fpath to POSIX path of (choose folder)
  9. set thePassword to display dialog "Enter your password:" default answer "" with hidden answer buttons {"Cancel", "OK"} default button "OK" with icon 1
  10. set thePasswordText to text returned of thePassword
  11. return fpath
  12. end tell
  13. EOT)
  14.  
  15. echo "$result"
  16. }
  17.  
  18. # Ask for folder
  19. lib=$(folder_dialog)
  20.  
  21. if [ ! -d "$lib" ]; then
  22. echo "🥁 Invalid Kontakt library path"
  23. read -p "🎹 Press Enter to continue..."
  24. exit 1
  25. fi
  26.  
  27. # Path must begin with "/Volumes"
  28. if [[ "$lib" != /Volumes/* ]]; then
  29. vol=$(ls -l /Volumes | grep ' -> /' | awk '{$1=""; $2=""; $3=""; $4=""; $5=""; $6=""; $7=""; $8=""; print $0}' | awk '{$1=$1};1')
  30. vol=${vol/ -> \//}
  31. lib=/Volumes/$vol$lib
  32. fi
  33.  
  34. xml=/var/tmp/kontaktLibraryHints.xml
  35.  
  36. find "$lib" -iname "*.nicnt" -o -iname "*_info.nkx" -type f | while read file
  37. do
  38. # Extract library version (`.nicnt` only)
  39. cver=
  40.  
  41. if [[ "$file" = *.nicnt ]]; then
  42. cver=$(dd skip=66 count=10 bs=1 if="$file" 2> /dev/null | sed 's/\x00//g')
  43. echo $cver | grep '\d\.\d\.\d' > /dev/null
  44.  
  45. if [ $? -ne 0 ]; then
  46. cver=$(dd skip=66 count=6 bs=1 if="$file" 2> /dev/null | sed 's/\x00//g')
  47. echo $cver | grep '\d\.\d' > /dev/null
  48. fi
  49.  
  50. [ $? -ne 0 ] && cver=
  51. else
  52. # Skip `_info.nkx` if `.nicnt` is present
  53. ldir=$(dirname "$file")
  54. hasnicnt=$(ls "$ldir" | grep -i '.nicnt' | wc -l)
  55. if [ $hasnicnt -ne 0 ]; then
  56. continue
  57. fi
  58. fi
  59.  
  60. # Extract library installation hints XML tree
  61. # and remove old `.plist` and `.xml` (if present) while getting its name
  62. awk '/<ProductHints[ >]/, $NF ~ /<\/ProductHints>/' "$file" | LC_ALL=C sed 's/<\/ProductHints>.*/<\/ProductHints>/' | xmllint --format --recover --encode "UTF-8" - > "$xml"
  63. name=$(xmllint --xpath "string(//Name)" "$xml")
  64. regkey=$(xmllint --xpath "string(//RegKey)" "$xml")
  65. plist="/Library/Preferences/com.native-instruments.$regkey.plist"
  66. xmldist="/Library/Application Support/Native Instruments/Service Center/$name.xml"
  67.  
  68. # Check for bad `.nicnt` (improperly hand-made, unofficial)
  69. if grep -iq '<HU>' "$xml"; then
  70. nohu=0
  71. else
  72. nohu=1
  73. fi
  74. if grep -iq '<JDX>' "$xml"; then
  75. nojdx=0
  76. else
  77. nojdx=1
  78. fi
  79. if grep -iq '<ProductSpecific>' "$xml"; then
  80. nops=0
  81. else
  82. nops=1
  83. fi
  84.  
  85. # These encryption keys fool Kontakt into believing
  86. # that library is legit
  87. if (( nohu != 0 )) && (( nojdx != 0 )); then
  88. cp "$xml" "$xml.tmp"
  89. if (( nops != 0 )); then
  90. sed 's/<\/SNPID>/<\/SNPID>| <ProductSpecific>| <HU>6C70AC13E02414D1A552685A1301D859<\/HU>| <JDX>023733942B73318EAEAD914E3981EC68BE72519A2F5738F828A6A028C4E1DBAC<\/JDX>| <Visibility type="Number">3<\/Visibility>| <\/ProductSpecific>/' "$xml.tmp" | tr '|' '\n' > "$xml"
  91. else
  92. sed 's/ <Visibility type="Number">/ <HU>6C70AC13E02414D1A552685A1301D859<\/HU>| <JDX>023733942B73318EAEAD914E3981EC68BE72519A2F5738F828A6A028C4E1DBAC<\/JDX>| <Visibility type="Number">/' "$xml.tmp" | tr '|' '\n' > "$xml"
  93. fi
  94. rm -f "$xml.tmp"
  95. fi
  96.  
  97. # Integrate into Service Center
  98. echo "$thePassword" | sudo -S mkdir -p "/Library/Application Support/Native Instruments/Service Center"
  99. echo "$thePassword" | sudo -S chmod 755 "/Library/Application Support/Native Instruments/Service Center"
  100. echo "$thePassword" | sudo -S cp "$xml" "$xmldist"
  101. echo "$thePassword" | sudo -S chmod 755 "$xmldist"
  102.  
  103. # Set `ContentDir`
  104. echo "$thePassword" | sudo -S rm -f "$plist"
  105. ContentDir=$(dirname "$file" | tr / :)
  106. ContentDir=${ContentDir//::/:}
  107. ContentDir=${ContentDir//::/:}
  108. echo "$thePassword" | sudo -S defaults write "$plist" ContentDir "${ContentDir#:*:}:"
  109.  
  110. # Obtain rest of parameters from extracted XML
  111. for key in RegKey SNPID Name HU JDX UPID AuthSystem ; do
  112. val=$(xmllint --xpath "string(//$key)" "$xml")
  113. if [[ -n "$val" ]]
  114. then
  115. if ! echo "$thePassword" | sudo -S defaults write "$plist" "$key" "$val"; then
  116. echo "Error: Failed to write $key to $plist" >&2
  117. exit 1
  118. fi
  119. fi
  120. done
  121.  
  122. # Write `ContentVersion`
  123. if [ -n "$cver" ]; then
  124. if ! echo "$thePassword" | sudo -S defaults write "$plist" ContentVersion "$cver"; then
  125. echo "Error: Failed to write ContentVersion to $plist" >&2
  126. exit 1
  127. fi
  128. fi
  129.  
  130. # Get `Visibility`
  131. vis=$(xmllint --xpath "string(//ProductSpecific/Visibility)" "$xml")
  132. if ! echo "$thePassword" | sudo -S defaults write "$plist" Visibility -int "$vis"; then
  133. echo "Error: Failed to write Visibility to $plist" >&2
  134. exit 1
  135. fi
  136.  
  137. # Review
  138. cat "$xml"
  139. defaults read "$plist"
  140. rm -f "$xml"
  141.  
  142. # display dialog box
  143. osascript -e 'tell app "System Events" to display dialog "🎸 Have fun! 🎻" buttons {"OK"} default button 1'
  144.  
Advertisement
Add Comment
Please, Sign In to add comment